OpenImageIO
 All Classes Files Friends Macros Pages
typedesc.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
34 
35 
36 #ifndef OPENIMAGEIO_TYPEDESC_H
37 #define OPENIMAGEIO_TYPEDESC_H
38 
39 #if defined(_MSC_VER)
40 // Ignore warnings about conditional expressions that always evaluate true
41 // on a given platform but may evaluate differently on another. There's
42 // nothing wrong with such conditionals.
43 # pragma warning (disable : 4127)
44 #endif
45 
46 #ifndef NULL
47 #define NULL 0
48 #endif
49 
50 #include <limits>
51 #include <cmath>
52 #include <cstddef>
53 #include <iostream>
54 
55 #include "export.h"
56 #include "version.h"
57 
58 
59 OIIO_NAMESPACE_ENTER
60 {
61 
75 
76 struct OIIO_API TypeDesc {
79  enum BASETYPE { UNKNOWN, NONE,
80  UCHAR, UINT8=UCHAR, CHAR, INT8=CHAR,
81  USHORT, UINT16=USHORT, SHORT, INT16=SHORT,
82  UINT, UINT32=UINT, INT, INT32=INT,
83  ULONGLONG, UINT64=ULONGLONG, LONGLONG, INT64=LONGLONG,
84  HALF, FLOAT, DOUBLE, STRING, PTR, LASTBASE };
87  enum AGGREGATE { SCALAR=1, VEC2=2, VEC3=3, VEC4=4, MATRIX44=16 };
92  enum VECSEMANTICS { NOXFORM=0, COLOR, POINT, VECTOR, NORMAL };
93 
94  unsigned char basetype;
95  unsigned char aggregate;
96  unsigned char vecsemantics;
97  unsigned char reserved;
98  int arraylen;
99 
102  TypeDesc (BASETYPE btype=UNKNOWN, AGGREGATE agg=SCALAR,
103  VECSEMANTICS xform=NOXFORM)
104  : basetype(static_cast<unsigned char>(btype)),
105  aggregate(static_cast<unsigned char>(agg)),
106  vecsemantics(static_cast<unsigned char>(xform)), reserved(0),
107  arraylen(0)
108  { }
109 
112  TypeDesc (BASETYPE btype, int arraylength)
113  : basetype(static_cast<unsigned char>(btype)),
114  aggregate(SCALAR), vecsemantics(NOXFORM),
115  reserved(0), arraylen(arraylength)
116  { }
117 
120  TypeDesc (BASETYPE btype, AGGREGATE agg, int arraylength)
121  : basetype(static_cast<unsigned char>(btype)),
122  aggregate(static_cast<unsigned char>(agg)),
123  vecsemantics(NOXFORM), reserved(0),
124  arraylen(arraylength)
125  { }
126 
129  TypeDesc (BASETYPE btype, AGGREGATE agg,
130  VECSEMANTICS xform, int arraylength)
131  : basetype(static_cast<unsigned char>(btype)),
132  aggregate(static_cast<unsigned char>(agg)),
133  vecsemantics(static_cast<unsigned char>(xform)),
134  reserved(0), arraylen(arraylength)
135  { }
136 
139  TypeDesc (const char *typestring);
140 
143  const char *c_str() const;
144 
145  friend std::ostream& operator<< (std::ostream& o, TypeDesc t) {
146  o << t.c_str(); return o;
147  }
148 
151  size_t numelements () const {
152  return (arraylen >= 1 ? arraylen : 1);
153  }
154 
157  size_t size () const {
158  size_t a = (size_t) (arraylen > 0 ? arraylen : 1);
159  if (sizeof(size_t) > sizeof(int)) {
160  // size_t has plenty of room for this multiplication
161  return a * elementsize();
162  } else {
163  // need overflow protection
164  unsigned long long s = (unsigned long long) a * elementsize();
165  const size_t toobig = std::numeric_limits<size_t>::max();
166  return s < toobig ? (size_t)s : toobig;
167  }
168  }
169 
172  TypeDesc elementtype () const {
173  TypeDesc t (*this); t.arraylen = 0; return t;
174  }
175 
178  size_t elementsize () const { return aggregate * basesize(); }
179 
180  // /// Return just the underlying C scalar type, i.e., strip out the
181  // /// array-ness and the aggregateness.
182 // BASETYPE basetype () const { return TypeDesc(base); }
183 
186  size_t basesize () const;
187 
192  size_t fromstring (const char *typestring);
193 
196  bool operator== (const TypeDesc &t) const {
197  return basetype == t.basetype && aggregate == t.aggregate &&
198  vecsemantics == t.vecsemantics && arraylen == t.arraylen;
199  }
200 
203  bool operator!= (const TypeDesc &t) const { return ! (*this == t); }
204 
207  friend bool operator== (const TypeDesc &t, BASETYPE b) {
208  return (BASETYPE)t.basetype == b && (AGGREGATE)t.aggregate == SCALAR && t.arraylen == 0;
209  }
210  friend bool operator== (BASETYPE b, const TypeDesc &t) {
211  return (BASETYPE)t.basetype == b && (AGGREGATE)t.aggregate == SCALAR && t.arraylen == 0;
212  }
213 
216  friend bool operator!= (const TypeDesc &t, BASETYPE b) {
217  return (BASETYPE)t.basetype != b || (AGGREGATE)t.aggregate != SCALAR || t.arraylen != 0;
218  }
219  friend bool operator!= (BASETYPE b, const TypeDesc &t) {
220  return (BASETYPE)t.basetype != b || (AGGREGATE)t.aggregate != SCALAR || t.arraylen != 0;
221  }
222 
225  friend bool equivalent (TypeDesc a, TypeDesc b) {
226  return a.basetype == b.basetype && a.aggregate == b.aggregate &&
227  a.arraylen == b.arraylen;
228  }
229 
232  void unarray (void) { arraylen = 0; }
233 
234  static const TypeDesc TypeFloat;
235  static const TypeDesc TypeColor;
236  static const TypeDesc TypeString;
237  static const TypeDesc TypeInt;
238  static const TypeDesc TypePoint;
239  static const TypeDesc TypeVector;
240  static const TypeDesc TypeNormal;
241  static const TypeDesc TypeMatrix;
242 };
243 
244 
245 
248 std::string tostring (TypeDesc type, const void *data,
249  const char *float_fmt = "%f", // E.g. "%g"
250  const char *string_fmt = "%s", // E.g. "\"%s\""
251  const char aggregate_delim[2] = "()", // Both sides of vector
252  const char *aggregate_sep = ",", // E.g. ", "
253  const char array_delim[2] = "{}", // Both sides of array
254  const char *array_sep = ","); // E.g. "; "
255 
256 
257 
260 template<typename T> struct BaseTypeFromC {};
261 template<> struct BaseTypeFromC<unsigned char> { static const TypeDesc::BASETYPE value = TypeDesc::UINT8; };
262 template<> struct BaseTypeFromC<char> { static const TypeDesc::BASETYPE value = TypeDesc::INT8; };
263 template<> struct BaseTypeFromC<unsigned short> { static const TypeDesc::BASETYPE value = TypeDesc::UINT16; };
264 template<> struct BaseTypeFromC<short> { static const TypeDesc::BASETYPE value = TypeDesc::INT16; };
265 template<> struct BaseTypeFromC<unsigned int> { static const TypeDesc::BASETYPE value = TypeDesc::UINT; };
266 template<> struct BaseTypeFromC<int> { static const TypeDesc::BASETYPE value = TypeDesc::INT; };
267 template<> struct BaseTypeFromC<unsigned long long> { static const TypeDesc::BASETYPE value = TypeDesc::UINT64; };
268 template<> struct BaseTypeFromC<long long> { static const TypeDesc::BASETYPE value = TypeDesc::INT64; };
269 #ifdef _HALF_H_
270 template<> struct BaseTypeFromC<half> { static const TypeDesc::BASETYPE value = TypeDesc::HALF; };
271 #endif
272 template<> struct BaseTypeFromC<float> { static const TypeDesc::BASETYPE value = TypeDesc::FLOAT; };
273 template<> struct BaseTypeFromC<double> { static const TypeDesc::BASETYPE value = TypeDesc::DOUBLE; };
274 
275 
276 
279 template<int b> struct CType {};
280 template<> struct CType<(int)TypeDesc::UINT8> { typedef unsigned char type; };
281 template<> struct CType<(int)TypeDesc::INT8> { typedef char type; };
282 template<> struct CType<(int)TypeDesc::UINT16> { typedef unsigned short type; };
283 template<> struct CType<(int)TypeDesc::INT16> { typedef short type; };
284 template<> struct CType<(int)TypeDesc::UINT> { typedef unsigned int type; };
285 template<> struct CType<(int)TypeDesc::INT> { typedef int type; };
286 template<> struct CType<(int)TypeDesc::UINT64> { typedef unsigned long long type; };
287 template<> struct CType<(int)TypeDesc::INT64> { typedef long long type; };
288 #ifdef _HALF_H_
289 template<> struct CType<(int)TypeDesc::HALF> { typedef half type; };
290 #endif
291 template<> struct CType<(int)TypeDesc::FLOAT> { typedef float type; };
292 template<> struct CType<(int)TypeDesc::DOUBLE> { typedef double type; };
293 
294 
295 
296 // Deprecated! Some back-compatibility with Gelato
297 typedef TypeDesc ParamType;
298 typedef TypeDesc ParamBaseType;
299 #define PT_FLOAT TypeDesc::FLOAT
300 #define PT_UINT8 TypeDesc::UCHAR
301 #define PT_INT8 TypeDesc::CHAR
302 #define PT_UINT16 TypeDesc::USHORT
303 #define PT_INT16 TypeDesc::SHORT
304 #define PT_UINT TypeDesc::UINT
305 #define PT_INT TypeDesc::INT
306 #define PT_FLOAT TypeDesc::FLOAT
307 #define PT_DOUBLE TypeDesc::DOUBLE
308 #define PT_HALF TypeDesc::HALF
309 #define PT_MATRIX TypeDesc(TypeDesc::FLOAT,TypeDesc::MATRIX44)
310 #define PT_STRING TypeDesc::STRING
311 #define PT_UNKNOWN TypeDesc::UNKNOWN
312 #define PT_COLOR TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::COLOR)
313 #define PT_POINT TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::POINT)
314 #define PT_VECTOR TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::VECTOR)
315 #define PT_NORMAL TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::NORMAL)
316 
317 }
318 OIIO_NAMESPACE_EXIT
319 
320 #endif /* !defined(OPENIMAGEIO_TYPEDESC_H) */