xray-registry.cpp 1.3 KB

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