xray-registry.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. #include "llvm/Support/ManagedStatic.h"
  14. #include <unordered_map>
  15. namespace llvm {
  16. namespace xray {
  17. using HandlerType = std::function<Error()>;
  18. ManagedStatic<std::unordered_map<cl::SubCommand *, HandlerType>> Commands;
  19. CommandRegistration::CommandRegistration(cl::SubCommand *SC,
  20. HandlerType Command) {
  21. assert(Commands->count(SC) == 0 &&
  22. "Attempting to overwrite a command handler");
  23. assert(Command && "Attempting to register an empty std::function<Error()>");
  24. (*Commands)[SC] = Command;
  25. }
  26. HandlerType dispatch(cl::SubCommand *SC) {
  27. auto It = Commands->find(SC);
  28. assert(It != Commands->end() &&
  29. "Attempting to dispatch on un-registered SubCommand.");
  30. return It->second;
  31. }
  32. } // namespace xray
  33. } // namespace llvm