HDK
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
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>
41
class
UT_VariadicT
42
{
43
public
:
44
45
UT_VariadicT<T>
&
operator<<
(
const
T
&
val
)
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
:
57
UT_Array<const T *>
myItems
;
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
*,
73
const
fpreal64
*>
UT_VariadicPODType
;
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
:
124
typedef
UT_VariadicT<UT_VariadicPODType>
Base
;
125
126
UT_VariadicPOD
()
127
:
Base
()
128
{
129
}
130
131
UT_VariadicPOD
&
operator<<
(
const
UT_VariadicPODType
&
val
)
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
uint16
unsigned short uint16
Definition:
SYS_Types.h:38
UT_VariadicT::item
const T & item(exint arg) const
Definition:
UT_Variadic.h:52
int32
int int32
Definition:
SYS_Types.h:39
fpreal16
Definition:
fpreal16.h:99
v
const GLdouble * v
Definition:
glcorearb.h:837
value
GLsizei const GLfloat * value
Definition:
glcorearb.h:824
exint
int64 exint
Definition:
SYS_Types.h:125
UT_API.h
UT_API
#define UT_API
Definition:
UT_API.h:14
UT_JSONWriter
Class which writes ASCII or binary JSON streams.
Definition:
UT_JSONWriter.h:37
UT_Array.h
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition:
core.h:1736
fpreal32
float fpreal32
Definition:
SYS_Types.h:200
UT_Array< const T * >
UT_VariadicPODType
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
fpreal64
double fpreal64
Definition:
SYS_Types.h:201
uint8
unsigned char uint8
Definition:
SYS_Types.h:36
Alembic::AbcCoreAbstract::ALEMBIC_VERSION_NS::operator<<
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition:
DataType.h:133
UT_VariadicT::myItems
UT_Array< const T * > myItems
Definition:
UT_Variadic.h:57
UT_VariadicT
Definition:
UT_Variadic.h:41
UT_VariadicT::operator<<
UT_VariadicT< T > & operator<<(const T &val)
Definition:
UT_Variadic.h:45
int64
long long int64
Definition:
SYS_Types.h:116
int8
signed char int8
Definition:
SYS_Types.h:35
OBJ_MatchTransform::T
UT_VariadicPOD::UT_VariadicPOD
UT_VariadicPOD()
Definition:
UT_Variadic.h:126
UT_Array::append
exint append()
Definition:
UT_Array.h:142
UT_VariadicPOD::operator<<
UT_VariadicPOD & operator<<(const UT_VariadicPODType &val)
Definition:
UT_Variadic.h:131
UT_Array::entries
exint entries() const
Alias of size(). size() is preferred.
Definition:
UT_Array.h:648
UT_VariadicT::entries
exint entries() const
Definition:
UT_Variadic.h:51
int16
short int16
Definition:
SYS_Types.h:37
UT_VariadicT::append
void append(const T &v)
Definition:
UT_Variadic.h:54
val
GLuint GLfloat * val
Definition:
glcorearb.h:1608
UT_String
Definition:
UT_String.h:73
w
GLubyte GLubyte GLubyte GLubyte w
Definition:
glcorearb.h:857
UT_VariadicPOD
Definition:
UT_Variadic.h:121
UT_VariadicPOD::Base
UT_VariadicT< UT_VariadicPODType > Base
Definition:
UT_Variadic.h:124
UT
UT_Variadic.h
Generated on Sat Dec 21 2024 02:49:22 for HDK by
1.8.6