Dean Michael Berris | a0e3ae4 | 2018-05-02 00:43:17 +0000 | [diff] [blame] | 1 | //===- xray-registry.cpp: Implement a command registry. -------------------===// |
Dean Michael Berris | c92bfb5 | 2016-10-26 04:14:34 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dean Michael Berris | c92bfb5 | 2016-10-26 04:14:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implement a simple subcommand registry. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | #include "xray-registry.h" |
| 13 | |
| 14 | #include "llvm/Support/ManagedStatic.h" |
| 15 | #include <unordered_map> |
| 16 | |
| 17 | namespace llvm { |
| 18 | namespace xray { |
| 19 | |
| 20 | using HandlerType = std::function<Error()>; |
| 21 | |
| 22 | ManagedStatic<std::unordered_map<cl::SubCommand *, HandlerType>> Commands; |
| 23 | |
| 24 | CommandRegistration::CommandRegistration(cl::SubCommand *SC, |
| 25 | HandlerType Command) { |
| 26 | assert(Commands->count(SC) == 0 && |
| 27 | "Attempting to overwrite a command handler"); |
| 28 | assert(Command && "Attempting to register an empty std::function<Error()>"); |
| 29 | (*Commands)[SC] = Command; |
| 30 | } |
| 31 | |
| 32 | HandlerType dispatch(cl::SubCommand *SC) { |
| 33 | auto It = Commands->find(SC); |
| 34 | assert(It != Commands->end() && |
| 35 | "Attempting to dispatch on un-registered SubCommand."); |
| 36 | return It->second; |
| 37 | } |
| 38 | |
| 39 | } // namespace xray |
| 40 | } // namespace llvm |