Path: blob/main/contrib/llvm-project/llvm/tools/llvm-xray/xray-registry.cpp
35231 views
//===- xray-registry.cpp: Implement a command registry. -------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Implement a simple subcommand registry.9//10//===----------------------------------------------------------------------===//11#include "xray-registry.h"1213#include <unordered_map>1415namespace llvm {16namespace xray {1718using HandlerType = std::function<Error()>;1920static std::unordered_map<cl::SubCommand *, HandlerType> &getCommands() {21static std::unordered_map<cl::SubCommand *, HandlerType> Commands;22return Commands;23}2425CommandRegistration::CommandRegistration(cl::SubCommand *SC,26HandlerType Command) {27assert(getCommands().count(SC) == 0 &&28"Attempting to overwrite a command handler");29assert(Command && "Attempting to register an empty std::function<Error()>");30getCommands()[SC] = Command;31}3233HandlerType dispatch(cl::SubCommand *SC) {34auto It = getCommands().find(SC);35assert(It != getCommands().end() &&36"Attempting to dispatch on un-registered SubCommand.");37return It->second;38}3940} // namespace xray41} // namespace llvm424344