Dean Michael Berris | f4f861b | 2018-05-02 00:43:17 +0000 | [diff] [blame] | 1 | //===- xray-registry.cpp: Implement a command registry. -------------------===// |
Dean Michael Berris | 5a35548 | 2016-10-26 04:14:34 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implement a simple subcommand registry. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "xray-registry.h" |
| 14 | |
| 15 | #include "llvm/Support/ManagedStatic.h" |
| 16 | #include <unordered_map> |
| 17 | |
| 18 | namespace llvm { |
| 19 | namespace xray { |
| 20 | |
| 21 | using HandlerType = std::function<Error()>; |
| 22 | |
| 23 | ManagedStatic<std::unordered_map<cl::SubCommand *, HandlerType>> Commands; |
| 24 | |
| 25 | CommandRegistration::CommandRegistration(cl::SubCommand *SC, |
| 26 | HandlerType Command) { |
| 27 | assert(Commands->count(SC) == 0 && |
| 28 | "Attempting to overwrite a command handler"); |
| 29 | assert(Command && "Attempting to register an empty std::function<Error()>"); |
| 30 | (*Commands)[SC] = Command; |
| 31 | } |
| 32 | |
| 33 | HandlerType dispatch(cl::SubCommand *SC) { |
| 34 | auto It = Commands->find(SC); |
| 35 | assert(It != Commands->end() && |
| 36 | "Attempting to dispatch on un-registered SubCommand."); |
| 37 | return It->second; |
| 38 | } |
| 39 | |
| 40 | } // namespace xray |
| 41 | } // namespace llvm |