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