您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

130 行
3.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
  5. // © 2013—2015 Guillaume Bittoun <guillaume.bittoun@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. #include <lol/base/utils.h> // has_key()
  15. #include <string>
  16. #include <map>
  17. namespace lol
  18. {
  19. ////MyType --------------------------------------------------------------------
  20. //struct MyTypeBase : public StructSafeEnum
  21. //{
  22. // enum Type
  23. // {
  24. // };
  25. //protected:
  26. // virtual bool BuildEnumMap(std::map<int64_t, std::string>& enum_map)
  27. // {
  28. // enum_map[] = "";
  29. // return true;
  30. // }
  31. //};
  32. //typedef SafeEnum<MyTypeBase> MyType;
  33. //-----------------------------------------------------------------------------
  34. struct StructSafeEnum
  35. {
  36. protected:
  37. /* Convert to string stuff */
  38. virtual bool BuildEnumMap(std::map<int64_t, std::string>&) { return false; }
  39. };
  40. //-----------------------------------------------------------------------------
  41. template<typename BASE, typename T = typename BASE::Type>
  42. class SafeEnum : public BASE
  43. {
  44. typedef T Type;
  45. Type m_value;
  46. public:
  47. inline SafeEnum() : m_value() {}
  48. inline SafeEnum(Type v) : m_value(v) {}
  49. /* Allow conversion from int and to the underlying type */
  50. inline explicit SafeEnum(int i) : m_value(T(i)) {}
  51. inline Type ToScalar() const { return m_value; }
  52. //inline std::string tostring() const { return tostring(); }
  53. /* Convert to string stuff */
  54. inline std::string tostring()
  55. {
  56. /* FIXME: we all know this isn’t thread safe. But is it really
  57. * a big deal? */
  58. static std::map<int64_t, std::string> enum_map;
  59. static bool ready = false;
  60. if (ready || this->BuildEnumMap(enum_map))
  61. {
  62. ready = true;
  63. if (has_key(enum_map, (int64_t)m_value))
  64. return enum_map[(int64_t)m_value];
  65. }
  66. return "<invalid enum>";
  67. }
  68. /* Safe comparisons between enums of the same type */
  69. friend bool operator == (SafeEnum const &a, SafeEnum const &b)
  70. {
  71. return a.m_value == b.m_value;
  72. }
  73. friend bool operator != (SafeEnum const &a, SafeEnum const &b)
  74. {
  75. return a.m_value != b.m_value;
  76. }
  77. friend bool operator < (SafeEnum const &a, SafeEnum const &b)
  78. {
  79. return a.m_value < b.m_value;
  80. }
  81. friend bool operator > (SafeEnum const &a, SafeEnum const &b)
  82. {
  83. return a.m_value > b.m_value;
  84. }
  85. friend bool operator <= (SafeEnum const &a, SafeEnum const &b)
  86. {
  87. return a.m_value <= b.m_value;
  88. }
  89. friend bool operator >= (SafeEnum const &a, SafeEnum const &b)
  90. {
  91. return a.m_value >= b.m_value;
  92. }
  93. };
  94. //-------------------------------------------------------------------------
  95. struct DisplayFlagBase : public StructSafeEnum
  96. {
  97. enum Type
  98. {
  99. On,
  100. Off,
  101. Toggle,
  102. MAX
  103. };
  104. protected:
  105. virtual bool BuildEnumMap(std::map<int64_t, std::string>& enum_map)
  106. {
  107. enum_map[On] = "On";
  108. enum_map[Off] = "Off";
  109. enum_map[Toggle] = "Toggle";
  110. return true;
  111. }
  112. };
  113. typedef SafeEnum<DisplayFlagBase> DisplayFlag;
  114. } /* namespace lol */