xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-xray/xray-registry.cpp (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 //===- xray-registry.cpp: Implement a command registry. -------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implement a simple subcommand registry.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "xray-registry.h"
13 
14 #include <unordered_map>
15 
16 namespace llvm {
17 namespace xray {
18 
19 using HandlerType = std::function<Error()>;
20 
21 static std::unordered_map<cl::SubCommand *, HandlerType> &getCommands() {
22   static std::unordered_map<cl::SubCommand *, HandlerType> Commands;
23   return Commands;
24 }
25 
26 CommandRegistration::CommandRegistration(cl::SubCommand *SC,
27                                          HandlerType Command) {
28   assert(getCommands().count(SC) == 0 &&
29          "Attempting to overwrite a command handler");
30   assert(Command && "Attempting to register an empty std::function<Error()>");
31   getCommands()[SC] = Command;
32 }
33 
34 HandlerType dispatch(cl::SubCommand *SC) {
35   auto It = getCommands().find(SC);
36   assert(It != getCommands().end() &&
37          "Attempting to dispatch on un-registered SubCommand.");
38   return It->second;
39 }
40 
41 } // namespace xray
42 } // namespace llvm
43