A modded EditSaber for making beat saber maps.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
3.3 KiB

  1. #pragma once
  2. //
  3. // ###### To create an enum call MyType with string support: ######
  4. //
  5. //// MyType.h
  6. //struct MyType
  7. //{
  8. // enum _Enum
  9. // {
  10. // Unknown,
  11. // Foo,
  12. // Bar
  13. // };
  14. //
  15. // struct _Definition : public EnumerationDefinition< _Enum, _Definition > { static StringValue Strings[]; };
  16. // typedef EnumerationValue< _Enum, _Definition, Unknown > _Value;
  17. //};
  18. //
  19. //typedef MyType::_Value MyTypeEnum;
  20. //
  21. //
  22. //// MyType.cpp
  23. //#include "MyType.h"
  24. //
  25. //MyType::_Definition::StringValue MyType::_Definition::Strings[] =
  26. //{
  27. // { MyType::Foo, _T( "Foo" ) },
  28. // { MyType::Bar, _T( "Bar" ) },
  29. // { MyType::Unknown, NULL }
  30. //};
  31. //
  32. //
  33. // ###### To create an enum call MyType without string support: ######
  34. //
  35. //// MyType.h
  36. //struct MyType
  37. //{
  38. // enum _Enum
  39. // {
  40. // Unknown,
  41. // Foo,
  42. // Bar
  43. // };
  44. //
  45. // typedef EnumerationDefinitionNoStrings _Definition;
  46. // typedef EnumerationValue< _Enum, _Definition, Unknown > _Value;
  47. //};
  48. //
  49. //typedef MyType::_Value MyTypeEnum;
  50. //
  51. namespace SevenZip
  52. {
  53. namespace intl
  54. {
  55. template < typename TEnum, class DerivedDef >
  56. struct EnumerationDefinition
  57. {
  58. struct StringValue
  59. {
  60. TEnum value;
  61. const TCHAR* string;
  62. };
  63. static TEnum Parse( const TString& string, const TEnum defaultValue )
  64. {
  65. const StringValue* it = DerivedDef::Strings;
  66. for (; it->string != NULL; ++it )
  67. {
  68. if ( string.Compare( it->string ) == 0 )
  69. {
  70. return it->value;
  71. }
  72. }
  73. return defaultValue;
  74. }
  75. static TString Format( const TEnum& value )
  76. {
  77. const StringValue* it = DerivedDef::Strings;
  78. for (; it->string != NULL; ++it )
  79. {
  80. if ( value == it->value )
  81. {
  82. return it->string;
  83. }
  84. }
  85. return TString();
  86. }
  87. };
  88. struct EnumerationDefinitionNoStrings {};
  89. template < typename TEnum, class TEnumClass, TEnum DefaultValue >
  90. class EnumerationValue
  91. {
  92. private:
  93. typedef typename EnumerationValue< TEnum, TEnumClass, DefaultValue > ThisClass;
  94. TEnum m_value;
  95. public:
  96. typedef typename TEnum Enum;
  97. EnumerationValue():
  98. m_value( DefaultValue )
  99. {}
  100. EnumerationValue( const TEnum& value ):
  101. m_value( value )
  102. {}
  103. static ThisClass Parse( const TString& string )
  104. {
  105. return ThisClass( TEnumClass::Parse( string, DefaultValue ) );
  106. }
  107. const TEnum& GetValue() const
  108. {
  109. return m_value;
  110. }
  111. TString GetString() const
  112. {
  113. return TEnumClass::Format( m_value );
  114. }
  115. operator TEnum() const
  116. {
  117. return m_value;
  118. }
  119. ThisClass& operator=( const TEnum& value )
  120. {
  121. m_value = value;
  122. return *this;
  123. }
  124. bool operator==( const ThisClass& that ) const
  125. {
  126. return that.m_value == m_value;
  127. }
  128. bool operator!=( const ThisClass& that ) const
  129. {
  130. return !operator==( that );
  131. }
  132. bool operator==( const TEnum& value ) const
  133. {
  134. return value == m_value;
  135. }
  136. bool operator!=( const TEnum& value ) const
  137. {
  138. return !operator==( value );
  139. }
  140. bool operator< ( const ThisClass& that ) const
  141. {
  142. return m_value < that.m_value;
  143. }
  144. // Bit field members
  145. void AddFlag( const TEnum& value )
  146. {
  147. *reinterpret_cast< int* >( &m_value ) |= static_cast< int >( value );
  148. }
  149. void RemoveFlag( const TEnum& value )
  150. {
  151. *reinterpret_cast< int* >( &m_value ) &= ~static_cast< int >( value );
  152. }
  153. bool HasFlag( const TEnum& value ) const
  154. {
  155. return ( m_value & value ) == value;
  156. }
  157. bool HasAnyFlag( const TEnum& valueCombo ) const
  158. {
  159. return ( m_value & valueCombo ) != 0;
  160. }
  161. };
  162. }
  163. }