HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  {
49  }
50 
52  {
53  myItems.append(&val);
54  return *this;
55  }
56 
57  exint entries() const { return myItems.entries(); }
58  const T &item(exint arg) const { return *myItems(arg); }
59 
60  void append(const T &v) { myItems.append(&v); }
61 
62 protected:
64 };
65 
66 /// Typedef for a POD (int, real or const char *) variadic argument. This is
67 /// equivalent to the c-style variadic arguments which can be processed.
68 typedef hboost::variant<int32, int64, fpreal64,
69  const char *,
70  const char **,
71  const int8 *,
72  const uint8 *,
73  const int16 *,
74  const uint16 *,
75  const int32 *,
76  const int64 *,
77  const fpreal16 *,
78  const fpreal32 *,
80 
81 /// Specialization of UT_VariadicT for simple int/float/string types. This
82 /// class supports implicit casting from integer to float types.
83 ///
84 /// UT_VariadicPOD allows you to easily create a type-safe variadic argument
85 /// list. For example, a function that takes variadic args might be something
86 /// like: @code
87 /// // Instead of void function(...)
88 /// void function(const UT_Variadic &vargs)
89 /// {
90 /// fpreal fval;
91 /// int64 ival;
92 /// const char *sval;
93 /// for (exint i = 0; i < vargs.entries()-1; i += 2)
94 /// {
95 /// // First argument is expected to be a string
96 /// if (!vargs.import(i, sval)) return error("Expected keyword");
97 /// switch (getType(sval))
98 /// {
99 /// case 0:
100 /// if (!vargs.import(i+1, ival))
101 /// return error("Expected integer value");
102 /// break;
103 /// case 1:
104 /// if (!vargs.import(i+1, fval))
105 /// return error("Expected real value");
106 /// break;
107 /// case 2:
108 /// if (!vargs.import(i+1, sval))
109 /// return error("Expected string value");
110 /// break;
111 /// }
112 /// }
113 /// }
114 /// @endcode
115 ///
116 /// To call a function with variadic arguments is fairly straightforward
117 /// @code
118 /// function(UT_Variadic() << "int" << 3
119 /// << "real << 3.421
120 /// << "string" << "Hello world!");
121 /// @endcode
122 ///
123 /// Of course, in the above example, it might be better to use @code
124 /// typedef std::pair<const char *, UT_VariadicPODType> KwPODPair;
125 /// typedef UT_VariadicT<VariadicPODPair> VariadicKwPODPairs;
126 /// @endcode
127 class UT_API UT_VariadicPOD : public UT_VariadicT<UT_VariadicPODType>
128 {
129 public:
131 
133  : Base()
134  {
135  }
137  : Base(src)
138  {
139  }
141  {
142  }
143 
145  {
146  Base::operator<<(val);
147  return *this;
148  }
149 
150  /// @{
151  /// Import the value. This performs type safety checks.
152  /// When importing real values, integer values will be implicitly cast
153  /// unless the strict_value option is set.
154  bool import(int arg, int32 &value) const;
155  bool import(int arg, int64 &value) const;
156  bool import(int arg, fpreal32 &value, bool strict_value=false) const;
157  bool import(int arg, fpreal64 &value, bool strict_value=false) const;
158  bool import(int arg, const char *&value) const;
159  bool import(int arg, const int8 *&value) const;
160  bool import(int arg, const uint8 *&value) const;
161  bool import(int arg, const int16 *&value) const;
162  bool import(int arg, const uint16 *&value) const;
163  bool import(int arg, const int32 *&value) const;
164  bool import(int arg, const int64 *&value) const;
165  bool import(int arg, const fpreal16 *&value) const;
166  bool import(int arg, const fpreal32 *&value) const;
167  bool import(int arg, const fpreal64 *&value) const;
168  /// @}
169 
170  /// @{
171  /// Save argument list or dump to stdout
172  bool save(UT_JSONWriter &w) const;
173  void dump() const;
174  /// @}
175 };
176 
177 #endif
unsigned short uint16
Definition: SYS_Types.h:38
const T & item(exint arg) const
Definition: UT_Variadic.h:58
int int32
Definition: SYS_Types.h:39
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:79
int64 exint
Definition: SYS_Types.h:125
#define UT_API
Definition: UT_API.h:14
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:35
GLenum src
Definition: glcorearb.h:1793
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
float fpreal32
Definition: SYS_Types.h:200
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition: DataType.h:133
UT_Array< const T * > myItems
Definition: UT_Variadic.h:63
const GLdouble * v
Definition: glcorearb.h:837
UT_VariadicT< T > & operator<<(const T &val)
Definition: UT_Variadic.h:51
long long int64
Definition: SYS_Types.h:116
signed char int8
Definition: SYS_Types.h:35
exint append()
Definition: UT_Array.h:137
UT_VariadicPOD(const UT_VariadicPOD &src)
Definition: UT_Variadic.h:136
UT_VariadicPOD & operator<<(const UT_VariadicPODType &val)
Definition: UT_Variadic.h:144
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:611
GLuint GLfloat * val
Definition: glcorearb.h:1608
exint entries() const
Definition: UT_Variadic.h:57
short int16
Definition: SYS_Types.h:37
void append(const T &v)
Definition: UT_Variadic.h:60
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UT_VariadicT< UT_VariadicPODType > Base
Definition: UT_Variadic.h:130