1a0e3ae4cSDean Michael Berris //===- xray-registry.cpp: Implement a command registry. -------------------===// 2a0e3ae4cSDean Michael Berris // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a0e3ae4cSDean Michael Berris // 7a0e3ae4cSDean Michael Berris //===----------------------------------------------------------------------===// 8a0e3ae4cSDean Michael Berris // 9a0e3ae4cSDean Michael Berris // Implement a simple subcommand registry. 10a0e3ae4cSDean Michael Berris // 11a0e3ae4cSDean Michael Berris //===----------------------------------------------------------------------===// 12a0e3ae4cSDean Michael Berris #include "xray-registry.h" 13a0e3ae4cSDean Michael Berris 14a0e3ae4cSDean Michael Berris #include <unordered_map> 15a0e3ae4cSDean Michael Berris 16a0e3ae4cSDean Michael Berris namespace llvm { 17a0e3ae4cSDean Michael Berris namespace xray { 18a0e3ae4cSDean Michael Berris 19a0e3ae4cSDean Michael Berris using HandlerType = std::function<Error()>; 20a0e3ae4cSDean Michael Berris getCommands()21*ede60037SNicolai Hähnlestatic std::unordered_map<cl::SubCommand *, HandlerType> &getCommands() { 22*ede60037SNicolai Hähnle static std::unordered_map<cl::SubCommand *, HandlerType> Commands; 23*ede60037SNicolai Hähnle return Commands; 24*ede60037SNicolai Hähnle } 25a0e3ae4cSDean Michael Berris CommandRegistration(cl::SubCommand * SC,HandlerType Command)26a0e3ae4cSDean Michael BerrisCommandRegistration::CommandRegistration(cl::SubCommand *SC, 27a0e3ae4cSDean Michael Berris HandlerType Command) { 28*ede60037SNicolai Hähnle assert(getCommands().count(SC) == 0 && 29a0e3ae4cSDean Michael Berris "Attempting to overwrite a command handler"); 30a0e3ae4cSDean Michael Berris assert(Command && "Attempting to register an empty std::function<Error()>"); 31*ede60037SNicolai Hähnle getCommands()[SC] = Command; 32a0e3ae4cSDean Michael Berris } 33a0e3ae4cSDean Michael Berris dispatch(cl::SubCommand * SC)34a0e3ae4cSDean Michael BerrisHandlerType dispatch(cl::SubCommand *SC) { 35*ede60037SNicolai Hähnle auto It = getCommands().find(SC); 36*ede60037SNicolai Hähnle assert(It != getCommands().end() && 37a0e3ae4cSDean Michael Berris "Attempting to dispatch on un-registered SubCommand."); 38a0e3ae4cSDean Michael Berris return It->second; 39a0e3ae4cSDean Michael Berris } 40a0e3ae4cSDean Michael Berris 41a0e3ae4cSDean Michael Berris } // namespace xray 42a0e3ae4cSDean Michael Berris } // namespace llvm 43