HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Variadic.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  * NAME: UT_Variadic.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_Variadic__
12 #define __UT_Variadic__
13 
14 #include "UT_API.h"
15 #include "UT_Array.h"
16 #include <hboost/variant.hpp>
17 
18 class UT_JSONWriter;
19 class UT_String;
20 
21 /// Build a list of variadic arguments of the given template type
22 ///
23 /// C-Style variadic arguments are error prone for various reasons:
24 /// - No type checking
25 /// - NULL termination issues
26 ///
27 /// This class provides type-safety and a cleaner interface. For example, to
28 /// accept a variable length array of strings: @code
29 /// typedef UT_VariadicT<const char *> VariadicStrings;
30 /// void
31 /// function(const VariadicStrings &strings)
32 /// {
33 /// for (exint i = 0; i < strings.entries(); ++i)
34 /// cerr << i << " " << strings.item(i) << " " strlen(strings.item(i));
35 /// }
36 ///
37 /// // Which can be called using
38 /// function(VariadicStrings() << "hello" << "world");
39 /// @endcode
40 template <typename T>
42 {
43 public:
45  {
46  }
48  : myItems(src.myItems)
49  {
50  }
52  {
53  }
54 
56  {
57  myItems.append(&val);
58  return *this;
59  }
60 
61  exint entries() const { return myItems.entries(); }
62  const T &item(exint arg) const { return *myItems(arg); }
63 
64  void append(const T &v) { myItems.append(&v); }
65 
66 protected:
68 };
69 
70 /// Typedef for a POD (int, real or const char *) variadic argument. This is
71 /// equivalent to the c-style variadic arguments which can be processed.
72 typedef hboost::variant<int32, int64, fpreal64,
73  const char *,
74  const char **,
75  const int8 *,
76  const uint8 *,
77  const int16 *,
78  const uint16 *,
79  const int32 *,
80  const int64 *,
81  const fpreal16 *,
82  const fpreal32 *,
84 
85 /// Specialization of UT_VariadicT for simple int/float/string types. This
86 /// class supports implicit casting from integer to float types.
87 ///
88 /// UT_VariadicPOD allows you to easily create a type-safe variadic argument
89 /// list. For example, a function that takes variadic args might be something
90 /// like: @code
91 /// // Instead of void function(...)
92 /// void function(const UT_Variadic &vargs)
93 /// {
94 /// fpreal fval;
95 /// int64 ival;
96 /// const char *sval;
97 /// for (exint i = 0; i < vargs.entries()-1; i += 2)
98 /// {
99 /// // First argument is expected to be a string
100 /// if (!vargs.import(i, sval)) return error("Expected keyword");
101 /// switch (getType(sval))
102 /// {
103 /// case 0:
104 /// if (!vargs.import(i+1, ival))
105 /// return error("Expected integer value");
106 /// break;
107 /// case 1:
108 /// if (!vargs.import(i+1, fval))
109 /// return error("Expected real value");
110 /// break;
111 /// case 2:
112 /// if (!vargs.import(i+1, sval))
113 /// return error("Expected string value");
114 /// break;
115 /// }
116 /// }
117 /// }
118 /// @endcode
119 ///
120 /// To call a function with variadic arguments is fairly straightforward
121 /// @code
122 /// function(UT_Variadic() << "int" << 3
123 /// << "real << 3.421
124 /// << "string" << "Hello world!");
125 /// @endcode
126 ///
127 /// Of course, in the above example, it might be better to use @code
128 /// typedef UT_Pair<const char *, UT_VariadicPODType> KwPODPair;
129 /// typedef UT_VariadicT<VariadicPODPair> VariadicKwPODPairs;
130 /// @endcode
131 class UT_API UT_VariadicPOD : public UT_VariadicT<UT_VariadicPODType>
132 {
133 public:
135 
137  : Base()
138  {
139  }
141  : Base(src)
142  {
143  }
145  {
146  }
147 
149  {
150  Base::operator<<(val);
151  return *this;
152  }
153 
154  /// @{
155  /// Import the value. This performs type safety checks.
156  /// When importing real values, integer values will be implicitly cast
157  /// unless the strict_value option is set.
158  bool import(int arg, int32 &value) const;
159  bool import(int arg, int64 &value) const;
160  bool import(int arg, fpreal32 &value, bool strict_value=false) const;
161  bool import(int arg, fpreal64 &value, bool strict_value=false) const;
162  bool import(int arg, const char *&value) const;
163  bool import(int arg, const int8 *&value) const;
164  bool import(int arg, const uint8 *&value) const;
165  bool import(int arg, const int16 *&value) const;
166  bool import(int arg, const uint16 *&value) const;
167  bool import(int arg, const int32 *&value) const;
168  bool import(int arg, const int64 *&value) const;
169  bool import(int arg, const fpreal16 *&value) const;
170  bool import(int arg, const fpreal32 *&value) const;
171  bool import(int arg, const fpreal64 *&value) const;
172  /// @}
173 
174  /// @{
175  /// Save argument list or dump to stdout
176  bool save(UT_JSONWriter &w) const;
177  void dump() const;
178  /// @}
179 };
180 
181 #endif
const T & item(exint arg) const
Definition: UT_Variadic.h:62
hboost::variant< int32, int64, fpreal64, const char *, const char **, const int8 *, const uint8 *, const int16 *, const uint16 *, const int32 *, const int64 *, const fpreal16 *, const fpreal32 *, const fpreal64 * > UT_VariadicPODType
Definition: UT_Variadic.h:83
const GLdouble * v
Definition: glcorearb.h:836
UT_VariadicT(const UT_VariadicT< T > &src)
Definition: UT_Variadic.h:47
#define UT_API
Definition: UT_API.h:12
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
signed char int8
Definition: SYS_Types.h:24
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition: DataType.h:133
long long int64
Definition: SYS_Types.h:100
UT_Array< const T * > myItems
Definition: UT_Variadic.h:67
int64 exint
Definition: SYS_Types.h:109
double fpreal64
Definition: SYS_Types.h:185
UT_VariadicT< T > & operator<<(const T &val)
Definition: UT_Variadic.h:55
int int32
Definition: SYS_Types.h:28
UT_VariadicPOD(const UT_VariadicPOD &src)
Definition: UT_Variadic.h:140
UT_VariadicPOD & operator<<(const UT_VariadicPODType &val)
Definition: UT_Variadic.h:148
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:446
unsigned short uint16
Definition: SYS_Types.h:27
GLsizei const GLfloat * value
Definition: glcorearb.h:823
exint entries() const
Definition: UT_Variadic.h:61
unsigned char uint8
Definition: SYS_Types.h:25
void append(const T &v)
Definition: UT_Variadic.h:64
GLuint GLfloat * val
Definition: glcorearb.h:1607
exint append(void)
Definition: UT_Array.h:95
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
short int16
Definition: SYS_Types.h:26
float fpreal32
Definition: SYS_Types.h:184
UT_VariadicT< UT_VariadicPODType > Base
Definition: UT_Variadic.h:134
GLenum src
Definition: glcorearb.h:1792