message_names.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_IPC_FUZZER_MESSAGE_LIB_MESSAGE_NAMES_H_
  5. #define TOOLS_IPC_FUZZER_MESSAGE_LIB_MESSAGE_NAMES_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "base/check.h"
  10. #include "base/macros.h"
  11. namespace ipc_fuzzer {
  12. class MessageNames {
  13. public:
  14. MessageNames();
  15. ~MessageNames();
  16. static MessageNames* GetInstance();
  17. void Add(uint32_t type, const char* name) {
  18. name_map_[type] = name;
  19. type_map_[name] = type;
  20. }
  21. bool TypeExists(uint32_t type) {
  22. return name_map_.find(type) != name_map_.end();
  23. }
  24. bool NameExists(const std::string& name) {
  25. return type_map_.find(name) != type_map_.end();
  26. }
  27. const std::string& TypeToName(uint32_t type) {
  28. TypeToNameMap::iterator it = name_map_.find(type);
  29. CHECK(it != name_map_.end());
  30. return it->second;
  31. }
  32. uint32_t NameToType(const std::string& name) {
  33. NameToTypeMap::iterator it = type_map_.find(name);
  34. CHECK(it != type_map_.end());
  35. return it->second;
  36. }
  37. private:
  38. typedef std::unordered_map<uint32_t, std::string> TypeToNameMap;
  39. typedef std::unordered_map<std::string, uint32_t> NameToTypeMap;
  40. TypeToNameMap name_map_;
  41. NameToTypeMap type_map_;
  42. static MessageNames* all_names_;
  43. DISALLOW_COPY_AND_ASSIGN(MessageNames);
  44. };
  45. } // namespace ipc_fuzzer
  46. #endif // TOOLS_IPC_FUZZER_MESSAGE_LIB_MESSAGE_NAMES_H_