xray-registry.cpp revision 355940
1169Sohair//===- xray-registry.cpp: Implement a command registry. -------------------===// 25629Sdmocek// 30Sduke// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40Sduke// See https://llvm.org/LICENSE.txt for license information. 50Sduke// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60Sduke// 70Sduke//===----------------------------------------------------------------------===// 80Sduke// 90Sduke// Implement a simple subcommand registry. 100Sduke// 110Sduke//===----------------------------------------------------------------------===// 120Sduke#include "xray-registry.h" 130Sduke 140Sduke#include "llvm/Support/ManagedStatic.h" 150Sduke#include <unordered_map> 160Sduke 170Sdukenamespace llvm { 180Sdukenamespace xray { 192362Sohair 202362Sohairusing HandlerType = std::function<Error()>; 212362Sohair 220SdukeManagedStatic<std::unordered_map<cl::SubCommand *, HandlerType>> Commands; 230Sduke 240SdukeCommandRegistration::CommandRegistration(cl::SubCommand *SC, 250Sduke HandlerType Command) { 260Sduke assert(Commands->count(SC) == 0 && 270Sduke "Attempting to overwrite a command handler"); 280Sduke assert(Command && "Attempting to register an empty std::function<Error()>"); 290Sduke (*Commands)[SC] = Command; 300Sduke} 310Sduke 320SdukeHandlerType dispatch(cl::SubCommand *SC) { 330Sduke auto It = Commands->find(SC); 340Sduke assert(It != Commands->end() && 350Sduke "Attempting to dispatch on un-registered SubCommand."); 360Sduke return It->second; 370Sduke} 380Sduke 390Sduke} // namespace xray 400Sduke} // namespace llvm 410Sduke