HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HOM_EnumValue.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  * An enum value object is an immutable object inside a module containing
8  * other enum values.
9  */
10 
11 #ifndef __HOM_EnumValue_h__
12 #define __HOM_EnumValue_h__
13 
14 #include "HOM_API.h"
15 #include "HOM_Defines.h"
16 #include "HOM_PtrOrNull.h"
17 #include <SYS/SYS_Math.h>
18 #include <string>
19 #include <typeinfo>
20 
21 SWIGOUT(%rename(EnumValue) HOM_EnumValue;)
22 
24 {
25 public:
28 
30  {
31  // This only exists to keep std::map<> happy.
32  myEnumClass = &typeid(void);
33  myRepr = "";
34  myName = "";
35  myId = 0;
36  }
37 
38  // The id is a unique id for this enumeration value with its set of values.
39  // We use the type info to quickly compare two enum values to make sure
40  // they're from the same set of values. We use repr for the __repr__
41  // method (unfortunately, type_info.name() varies between platforms,
42  // contains mangling information, and contains the "HOM_" prefix). We
43  // assume that repr is a string constant (we don't store a deep copy).
44  // We similarly use name to hold the name of this particular enum value.
45  HOM_EnumValue(const std::type_info& enum_class, const char *repr,
46  const char *name, int id)
47  : myEnumClass(&enum_class), myRepr(repr), myName(name), myId(id)
48  {}
49 
50  // Swig doesn't properly let you ignore only one overloaded operator==.
51  // So, the methods wrapped by swig for Python are named __eq__.
53  { return value.myPointer && *this == *value.myPointer; }
54 
56  { return !__eq__(value); }
57 
59  { return myRepr; }
60 
61  int __hash__()
62  { return (int)SYSmultiplicative_inthash(myId); }
63 
64  std::string name() const
65  { return myName; }
66 
67  // These methods are used by the implementation and are not wrapped by
68  // swig.
69  SWIGOUT(%ignore operator==;)
70  bool operator==(const HOM_EnumValue &value) const
71  { return *myEnumClass == *value.myEnumClass && myId == value.myId; }
72 
73  SWIGOUT(%ignore operator!=;)
74  bool operator!=(const HOM_EnumValue &value) const
75  { return *myEnumClass != *value.myEnumClass || myId != value.myId; }
76 
77  SWIGOUT(%ignore enumClass;)
78  const std::type_info &enumClass() const
79  { return *myEnumClass; }
80 
81  SWIGOUT(%ignore id;)
82  int id() const
83  { return myId; }
84 
85  bool operator<(const HOM_EnumValue &value) const
86  {
87  int v = strcmp(myName, value.myName);
88  return v < 0 || ( v == 0 && myId < value.myId );
89  }
90 
91 private:
92  const std::type_info *myEnumClass;
93  const char *myRepr;
94  const char *myName;
95  int myId;
96 };
97 
98 
99 // Use the HOM_DECLARE_ENUM_MODULE macro to declare an enumeration module
100 // class in the header file.
101 // HOM_DEFINE_ENUM_MODULE(enum_module, num_values, values_list)
102 // - enum_module is the name of the module (without the HOM_) prefix.
103 // - values_list is a comma-separated list of the enumeration values
104 // (set this in a define)
105 // - num_values is the number of enumeration values
106 // Use HOM_DEFINE_ENUM_MODULE to define its contents in the .C file. It
107 // takes the same parameters.
108 
109 // This pair of macros is used to build the class definition for the
110 // enumeration module.
111 #define HOM_DECLARE_ENUM_MODULE_START(enum_module) \
112  SWIGOUT(%nodefaultctor HOM_ ## enum_module;) \
113  SWIGOUT(%rename(enum_module) HOM_ ## enum_module;) \
114  class HOM_API HOM_ ## enum_module \
115  { \
116  public:
117 #define HOM_DECLARE_ENUM_MODULE_END \
118  };
119 
120 #ifndef SWIG
121 #include <hboost/preprocessor/cat.hpp>
122 #include <hboost/preprocessor/arithmetic/dec.hpp>
123 #include <hboost/preprocessor/tuple/to_seq.hpp>
124 #include <hboost/preprocessor/tuple/rem.hpp>
125 #include <hboost/preprocessor/seq/transform.hpp>
126 #include <hboost/preprocessor/seq/for_each.hpp>
127 #include <hboost/preprocessor/stringize.hpp>
128 
129 // For an enum value, we declare a static const HOM_EnumValue member.
130 // (It will be initialized with the HOM_DEFINE_ENUM_* macros). For
131 // each "Foo" enum value we also declare a "Foo_Id" constant. This constant
132 // is used so we can use enum values in switch statements in C++. It doesn't
133 // need a definition because it's a static const int that's initialized
134 // inside the class declaration.
135 //
136 // r will be the iteration value (plus 1, for some reason). We use it
137 // to give each enumeration value a unique id number.
138 #define HOM_DECLARE_ENUM_VALUE(r, unused_data, elem) \
139  static HOM_EnumValue elem; \
140  static const int HBOOST_PP_CAT(elem, _Id) = HBOOST_PP_DEC(r);
141 
142 // Call HOM_DECLARE_ENUM_VALUE for each enumeration value in the sequence.
143 // The whole thing is wrapped in the class definition.
144 #define HOM_DECLARE_ENUM_MODULE_FROM_SEQ(enum_module, values_seq) \
145  HOM_DECLARE_ENUM_MODULE_START(enum_module) \
146  HBOOST_PP_SEQ_FOR_EACH(HOM_DECLARE_ENUM_VALUE, _, values_seq) \
147  HOM_DECLARE_ENUM_MODULE_END
148 
149 // We wrap the comma-separated tuples of values in () to build what the boost
150 // preprocessor calls a tuple. We then convert that to a boost sequence
151 // and call the above macro. This macro is more convenient to use than the
152 // one above, though you might need to use the one above if you hit boost's
153 // maximum tuple size.
154 #define HOM_DECLARE_ENUM_MODULE(enum_module, num_values, values_tuple) \
155  HOM_DECLARE_ENUM_MODULE_FROM_SEQ(enum_module, \
156  HBOOST_PP_TUPLE_TO_SEQ(num_values, (values_tuple)))
157 
158 
159 // For each enum value we provide the definition of the static member variable,
160 // passing the enum class's type_info, the enum value's repr string, and
161 // the enum value's id into the constructor.
162 #define HOM_DEFINE_ENUM_VALUE(r, enum_module, elem) \
163  HOM_EnumValue HBOOST_PP_CAT(HOM_, enum_module)::elem \
164  (typeid(HBOOST_PP_CAT(HOM_, enum_module)), \
165  HBOOST_PP_STRINGIZE(enum_module) "." HBOOST_PP_STRINGIZE(elem), \
166  HBOOST_PP_STRINGIZE(elem), \
167  HBOOST_PP_DEC(r));
168 
169 // The .C file contains all the definitions for the static member variables.
170 #define HOM_DEFINE_ENUM_MODULE_FROM_SEQ(enum_module, values_seq) \
171  HBOOST_PP_SEQ_FOR_EACH(HOM_DEFINE_ENUM_VALUE, enum_module, values_seq)
172 
173 #define HOM_DEFINE_ENUM_MODULE(enum_module, num_values, values_tuple) \
174  HOM_DEFINE_ENUM_MODULE_FROM_SEQ(enum_module, \
175  HBOOST_PP_TUPLE_TO_SEQ(num_values, (values_tuple)))
176 
177 // Use HOM_DECLARE_ENUM_VALUE2 to declare an enum with a specific value.
178 // enum_module: enum module name (ignored)
179 // enum_elem: enum element name
180 // ienum_elem: internal enum element (ignored)
181 // ienum_elem_name: internal enum element name (ignored)
182 // enum_value: enum value
183 // E.g. usage:
184 // HOM_DECLARE_ENUM_MODULE_START(myEnumModule)
185 // HOM_DECLARE_ENUM_VALUE2(myEnumModule,myValue,0)
186 // HOM_DECLARE_ENUM_MODULE_END
187 #define HOM_DECLARE_ENUM_VALUE2(enum_module, enum_elem, ienum_elem, ienum_elem_name, enum_value) \
188  static HOM_EnumValue enum_elem; \
189  static const int HBOOST_PP_CAT(enum_elem,_Id) = enum_value;
190 
191 // Use HOM_DEFINE_ENUM_VALUE2 to define an enum with a specific id number.
192 // enum_module: enum module name
193 // enum_elem: enum element
194 // ienum_elem: internal enum element name (ignored)
195 // ienum_elem_name: internal element name
196 // enum_value: enum value
197 #define HOM_DEFINE_ENUM_VALUE2(enum_module, enum_elem, ienum_elem, ienum_elem_name, enum_value) \
198  HOM_EnumValue HBOOST_PP_CAT(HOM_,enum_module)::enum_elem \
199  ((typeid(HBOOST_PP_CAT(HOM_,enum_module))), \
200  HBOOST_PP_STRINGIZE(enum_module) "." HBOOST_PP_STRINGIZE(enum_elem), \
201  HBOOST_PP_STRINGIZE(enum_elem), \
202  enum_value);
203 
204 #else // SWIG
205 
206 // Swig can't handle boost's proprocessor macros, so we create a simple
207 // class for swig to wrap that just contains static HOM_EnumValue members.
208 // Because values_tuple is a comma-separated list, we don't need any boost
209 // preprocessor stuff.
210 #define HOM_DECLARE_ENUM_MODULE(enum_module, num_values, values_tuple) \
211  HOM_DECLARE_ENUM_MODULE_START(enum_module) \
212  static const HOM_EnumValue values_tuple; \
213  HOM_DECLARE_ENUM_MODULE_END
214 
215 #define HOM_DECLARE_ENUM_VALUE2(enum_module, enum_elem, ienum_elem, ienum_elem_name, enum_value) \
216  static const HOM_EnumValue enum_elem;
217 
218 #endif // SWIG
219 
220 #endif
bool operator<(const HOM_EnumValue &value) const
Definition: HOM_EnumValue.h:85
void
Definition: png.h:1083
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
#define SWIGOUT(x)
Definition: HOM_Defines.h:24
bool __eq__(HOM_PtrOrNull< HOM_EnumValue > value)
Definition: HOM_EnumValue.h:52
#define HOM_API
Definition: HOM_API.h:13
HOM_EnumValue(const std::type_info &enum_class, const char *repr, const char *name, int id)
Definition: HOM_EnumValue.h:45
void ignore(T const &) VULKAN_HPP_NOEXCEPT
Definition: vulkan.hpp:6508
bool __ne__(HOM_PtrOrNull< HOM_EnumValue > value)
Definition: HOM_EnumValue.h:55
GLuint const GLchar * name
Definition: glcorearb.h:786
std::string name() const
Definition: HOM_EnumValue.h:64
OIIO_UTIL_API bool rename(string_view from, string_view to, std::string &err)
const std::type_info & enumClass() const
Definition: HOM_EnumValue.h:78
Definition: core.h:1131
std::string __repr__() const
Definition: HOM_EnumValue.h:58