HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_VariadicPOD.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_VariadicPOD.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_VariadicPOD__
12 #define __UT_VariadicPOD__
13 
14 #include "UT_API.h"
15 
16 #include "UT_Variadic.h"
17 #include <variant>
18 
20 
21 /// Typedef for a POD (int, real or const char *) variadic argument. This is
22 /// equivalent to the c-style variadic arguments which can be processed.
23 typedef std::variant<int32, int64, fpreal64,
24  const char *,
25  const char **,
26  const int8 *,
27  const uint8 *,
28  const int16 *,
29  const uint16 *,
30  const int32 *,
31  const int64 *,
32  const fpreal16 *,
33  const fpreal32 *,
35 
36 /// Specialization of UT_VariadicT for simple int/float/string types. This
37 /// class supports implicit casting from integer to float types.
38 ///
39 /// UT_VariadicPOD allows you to easily create a type-safe variadic argument
40 /// list. For example, a function that takes variadic args might be something
41 /// like: @code
42 /// // Instead of void function(...)
43 /// void function(const UT_Variadic &vargs)
44 /// {
45 /// fpreal fval;
46 /// int64 ival;
47 /// const char *sval;
48 /// for (exint i = 0; i < vargs.entries()-1; i += 2)
49 /// {
50 /// // First argument is expected to be a string
51 /// if (!vargs.import(i, sval)) return error("Expected keyword");
52 /// switch (getType(sval))
53 /// {
54 /// case 0:
55 /// if (!vargs.import(i+1, ival))
56 /// return error("Expected integer value");
57 /// break;
58 /// case 1:
59 /// if (!vargs.import(i+1, fval))
60 /// return error("Expected real value");
61 /// break;
62 /// case 2:
63 /// if (!vargs.import(i+1, sval))
64 /// return error("Expected string value");
65 /// break;
66 /// }
67 /// }
68 /// }
69 /// @endcode
70 ///
71 /// To call a function with variadic arguments is fairly straightforward
72 /// @code
73 /// function(UT_Variadic() << "int" << 3
74 /// << "real << 3.421
75 /// << "string" << "Hello world!");
76 /// @endcode
77 ///
78 /// Of course, in the above example, it might be better to use @code
79 /// typedef std::pair<const char *, UT_VariadicPODType> KwPODPair;
80 /// typedef UT_VariadicT<VariadicPODPair> VariadicKwPODPairs;
81 /// @endcode
82 class UT_API UT_VariadicPOD : public UT_VariadicT<UT_VariadicPODType>
83 {
84 public:
86 
88  : Base()
89  {
90  }
91 
93  {
94  Base::operator<<(val);
95  return *this;
96  }
97 
98  /// @{
99  /// Import the value. This performs type safety checks.
100  /// When importing real values, integer values will be implicitly cast
101  /// unless the strict_value option is set.
102  bool import(int arg, int32 &value) const;
103  bool import(int arg, int64 &value) const;
104  bool import(int arg, fpreal32 &value, bool strict_value=false) const;
105  bool import(int arg, fpreal64 &value, bool strict_value=false) const;
106  bool import(int arg, const char *&value) const;
107  bool import(int arg, const int8 *&value) const;
108  bool import(int arg, const uint8 *&value) const;
109  bool import(int arg, const int16 *&value) const;
110  bool import(int arg, const uint16 *&value) const;
111  bool import(int arg, const int32 *&value) const;
112  bool import(int arg, const int64 *&value) const;
113  bool import(int arg, const fpreal16 *&value) const;
114  bool import(int arg, const fpreal32 *&value) const;
115  bool import(int arg, const fpreal64 *&value) const;
116  /// @}
117 
118  /// @{
119  /// Save argument list or dump to stdout
120  bool save(UT_JSONWriter &w) const;
121  void dump() const;
122  /// @}
123 };
124 
125 #endif
unsigned short uint16
Definition: SYS_Types.h:38
int int32
Definition: SYS_Types.h:39
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
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define UT_API
Definition: UT_API.h:14
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:39
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1859
float fpreal32
Definition: SYS_Types.h:200
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
long long int64
Definition: SYS_Types.h:116
signed char int8
Definition: SYS_Types.h:35
UT_VariadicPOD & operator<<(const UT_VariadicPODType &val)
short int16
Definition: SYS_Types.h:37
GLuint GLfloat * val
Definition: glcorearb.h:1608
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
UT_VariadicT< UT_VariadicPODType > Base