HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_OStream.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_OStream.h (UT Library, C++)
7  *
8  * COMMENTS: Simple analogue to UT_IStream. It's just a thin wrapper on top
9  * of ostream for now, to be expanded as needed.
10  */
11 
12 #ifndef __UT_OSTREAM_H_INCLUDED__
13 #define __UT_OSTREAM_H_INCLUDED__
14 
15 #include "UT_API.h"
16 #include "UT_IOS.h"
17 #include "UT_NTStreamUtil.h"
18 #include "UT_NonCopyable.h"
19 #include <SYS/SYS_Compiler.h>
20 #include <SYS/SYS_Types.h>
21 
22 #include <iosfwd>
23 #include <string>
24 
25 
26 #if defined(MBSD) || (!defined(AMD64) && !defined(WIN32))
27  #define UT_NEED_LONG_OSTREAM_INSERTER
28 #endif
29 
30 
31 class UT_String;
32 class UT_StringRef;
33 
34 
36 {
37 public:
38  typedef char char_type;
39  typedef int64 pos_type;
41 
42  /// Tag for suppressing uninitialization
43  enum Uninitialized { NO_INIT };
44 
45 
46  /// Construct UT_OStream with reference to the given std::ostream and
47  /// streaming mode.
48  // @{
49  UT_OStream(std::ostream &os, UT_IOS_TYPE bin)
50  : myOS(&os)
51  , myIsBinary(bin == UT_IOS_BINARY)
52  {
53  }
54  UT_OStream(std::ostream &os, bool binary)
55  : myOS(&os)
56  , myIsBinary(binary)
57  {
58  }
59  // @}
60  /// Construct uninitialized UT_OStream with a given streaming mode. You
61  /// must call setOStream() before this object can be used.
63  : myOS(NULL)
64  , myIsBinary(bin == UT_IOS_BINARY)
65  {
66  }
67 
68  /// Compatibility methods for std::ostream replacement.
69  // @{
70 
71  UT_OStream & put(char_type ch);
72  UT_OStream & write(const char_type *str, int64 count);
73  UT_OStream & flush();
74 
75  UT_OStream & seekp(pos_type offset, seek_type dir = UT_IOS_SEEK_BEG);
76  pos_type tellp();
77 
78  int precision() const;
79  int precision(int dig);
80  int width() const;
81  int width(int dig);
82 
83  SYS_SAFE_BOOL operator bool() const;
84  bool operator!() const;
85 
86  bool bad() const;
87  bool eof() const;
88  bool fail() const;
89  bool good() const;
90 
91  void setstate(std::ios::iostate state)
92  { myOS->setstate(state); }
93 
102  friend UT_OStream & operator<<(UT_OStream &os, char value);
103  friend UT_OStream & operator<<(UT_OStream &os, unsigned char value);
104  friend UT_OStream & operator<<(UT_OStream &os, const char *value);
105 
106 #ifdef UT_NEED_LONG_OSTREAM_INSERTER
107  friend UT_OStream & operator<<(UT_OStream &os, long value);
108  friend UT_OStream & operator<<(UT_OStream &os, unsigned long value);
109 #endif
110 
111 
112  // @}
113 
114  /// Manipulation of the underlying std::ostream reference
115  // @{
116  std::ostream & getOStream() { return *myOS; }
117  void setOStream(std::ostream &os) { myOS = &os; }
118  // @}
119 
120  /// Set the stream reference to same as the given UT_OStream object.
121  /// @note The binary flag of this stream is not modified.
123  { myOS = &os.getOStream(); }
124 
125  /// Determine if the stream is currently in ASCII or binary mode.
126  // @{
127  bool isAscii() const { return !myIsBinary; }
128  bool isBinary() const { return myIsBinary; }
129  // @}
130 
131  /// Write array data to the stream. It will call bwrite() or awrite()
132  /// depending on the current streaming mode. For binary output, the data
133  /// will be byte-swapped according to the Houdini standard.
134  ///
135  /// @param buf The array to write.
136  /// @param cnt The number of array elements to write.
137  /// @param nl If true, a newline is appended, else a space is
138  /// appended. This parameter is ignored for binary writes.
139  /// @return The number of elements written.
140  // @{
141  int64 write(const bool *buf, int64 cnt=1, bool nl=false);
142  int64 write(const int16 *buf, int64 cnt=1, bool nl=false);
143  int64 write(const uint16 *buf, int64 cnt=1, bool nl=false);
144  int64 write(const int32 *buf, int64 cnt=1, bool nl=false);
145  int64 write(const uint32 *buf, int64 cnt=1, bool nl=false);
146  int64 write(const int64 *buf, int64 cnt=1, bool nl=false);
147  int64 write(const uint64 *buf, int64 cnt=1, bool nl=false);
148  template <typename DEST_TYPE>
149  int64 write(const fpreal32 *buf, int64 cnt=1, bool nl=false);
150  template <typename DEST_TYPE>
151  int64 write(const fpreal64 *buf, int64 cnt=1, bool nl=false);
152  // @}
153 
154  /// Write string data to the stream. It will call bwrite() or awrite()
155  /// depending on the current streaming mode.
156  ///
157  /// @param str The string to write.
158  /// @param nl If true, a newline is appended, else a space is
159  /// appended. This parameter is ignored for binary writes.
160  /// @return @c true if the string was successfully written out.
161  /// @{
162  bool write(const UT_String &str, bool nl=false);
163  bool write(const UT_StringRef &str, bool nl=false);
164  bool write(const std::string &str, bool nl=false);
165  /// @}
166 
167  /// Platform consistent binary writes of data. The data will be
168  /// byte-swapped according to the Houdini standard.
169  ///
170  /// @param buf The array to write.
171  /// @param cnt The number of array elements to write.
172  /// @return The number of elements written.
173  /// @{
174  int64 bwrite(const bool *buf, int64 cnt=1);
175  int64 bwrite(const char *buf, int64 cnt=1);
176  int64 bwrite(const unsigned char *buf, int64 cnt=1);
177  int64 bwrite(const signed char *buf, int64 cnt=1);
178  int64 bwrite(const int16 *buf, int64 cnt=1);
179  int64 bwrite(const uint16 *buf, int64 cnt=1);
180  int64 bwrite(const int32 *buf, int64 cnt=1);
181  int64 bwrite(const uint32 *buf, int64 cnt=1);
182  int64 bwrite(const int64 *buf, int64 cnt=1);
183  int64 bwrite(const uint64 *buf, int64 cnt=1);
184  template <typename DEST_TYPE>
185  int64 bwrite(const fpreal32 *buf, int64 cnt=1);
186  template <typename DEST_TYPE>
187  int64 bwrite(const fpreal64 *buf, int64 cnt=1);
188  /// @}
189 
190  /// Platform consistent binary writes of strings.
191  ///
192  /// @param str The string to write.
193  /// @return @c true if the string was successfully written out.
194  /// @{
195  bool bwrite(const UT_String &str);
196  bool bwrite(const UT_StringRef &str);
197  bool bwrite(const std::string &str);
198  /// @}
199 
200 
201  /// Platform consistent ASCII writes of data.
202  ///
203  /// @param buf The array to write.
204  /// @param cnt The number of array elements to write.
205  /// @param nl If true, a newline is appended, else a space is
206  /// appended.
207  /// @return The number of elements written.
208  // @{
209  int64 awrite(const bool *buf, int64 cnt=1, bool nl=false);
210  int64 awrite(const char *buf, int64 cnt=1, bool nl=false);
211  int64 awrite(const unsigned char *buf, int64 cnt=1,
212  bool nl=false);
213  int64 awrite(const signed char *buf, int64 cnt=1,
214  bool nl=false);
215  int64 awrite(const int16 *buf, int64 cnt=1, bool nl=false);
216  int64 awrite(const uint16 *buf, int64 cnt=1, bool nl=false);
217  int64 awrite(const int32 *buf, int64 cnt=1, bool nl=false);
218  int64 awrite(const uint32 *buf, int64 cnt=1, bool nl=false);
219  int64 awrite(const int64 *buf, int64 cnt=1, bool nl=false);
220  int64 awrite(const uint64 *buf, int64 cnt=1, bool nl=false);
221  int64 awrite(const fpreal32 *buf, int64 cnt=1, bool nl=false);
222  int64 awrite(const fpreal64 *buf, int64 cnt=1, bool nl=false);
223  /// @}
224 
225  /// Platform consistent ASCII writes of strings.
226  ///
227  /// @param str The string to write.
228  /// @param nl If true, a newline is appended, else a space is
229  /// appended. This parameter is ignored for binary writes.
230  /// @return @c true if the string was successfully written out.
231  /// @{
232  bool awrite(const UT_String &str, bool nl=false);
233  bool awrite(const UT_StringRef &str, bool nl=false);
234  bool awrite(const std::string &str, bool nl=false);
235  /// @}
236 
237 private:
238  UT_OStream(); // not implemented
239 
240 private:
241  std::ostream * myOS;
242  bool myIsBinary;
243 
245 };
246 
247 //////////////////////////////////////////////////////////////////////////////
248 
249 // Null stream, for when a stream is required but we don't need the output.
250 class UT_API UT_ONullStream : public std::ostream
251 {
252 public:
253  UT_ONullStream() : std::ios(0), std::ostream(0) {}
254 
255 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
256  // Streams cannot be moved. This results MSVC 2015 failing to build
257  // when trying to export the move constructor, which std::ios_base
258  // does not have.
259  UT_ONullStream(UT_ONullStream &&) = delete;
260 #endif
261 };
262 
263 
264 //////////////////////////////////////////////////////////////////////////////
265 
266 /// Turn the stream into binary or ASCII while in variable scope.
268 {
269 public:
271  : myOS(os)
272  , myOldIsBinary(os.isBinary())
273  {
274  myOS.myIsBinary = (bin == UT_IOS_BINARY);
275  }
276  explicit UT_OStreamAutoBinary(UT_OStream &os, bool bin)
277  : myOS(os)
278  , myOldIsBinary(os.isBinary())
279  {
280  myOS.myIsBinary = bin;
281  }
282 
284  {
285  myOS.myIsBinary = myOldIsBinary; // restore
286  }
287 
288 private:
289  UT_OStream & myOS;
290  bool myOldIsBinary;
291 };
292 
293 
294 //////////////////////////////////////////////////////////////////////////////
295 
296 /// UT_AutoPrecision allows one to temporarily change a stream's precision
297 /// for the life of this object.
299 {
300 public:
301  explicit UT_AutoPrecision(UT_OStream &os, int new_precision)
302  : myStream(os)
303  , myOldPrecision(os.precision(new_precision))
304  {
305  }
306 
308  {
309  (void) myStream.precision(myOldPrecision); // restore
310  }
311 
312 private:
313  UT_OStream & myStream;
314  int myOldPrecision;
315 };
316 
317 
318 //////////////////////////////////////////////////////////////////////////////
319 //
320 // UT_OStream Implementation
321 //
322 
323 //
324 // Manipulators
325 //
326 
327 inline UT_OStream &
329 {
330  return manip(os);
331 }
332 
333 
334 //
335 // Inline function implementations
336 //
337 
338 inline UT_OStream &
340 {
341  myOS->put(ch);
342  return *this;
343 }
344 
345 inline UT_OStream &
347 {
348  myOS->write(str, (std::streamsize)count);
349  return *this;
350 }
351 
352 inline UT_OStream &
354 {
355  myOS->flush();
356  return *this;
357 }
358 
359 inline UT_OStream &
361 {
362  switch (dir)
363  {
364  case UT_IOS_SEEK_BEG:
365  myOS->seekp(offset, std::ios_base::beg);
366  break;
367  case UT_IOS_SEEK_CUR:
368  myOS->seekp(offset, std::ios_base::cur);
369  break;
370  case UT_IOS_SEEK_END:
371  myOS->seekp(offset, std::ios_base::end);
372  break;
373  }
374  return *this;
375 }
376 
379 {
380  return myOS->tellp();
381 }
382 
383 
384 #define UT_OSTREAM_ACCESSOR(FUNCNAME) \
385 inline int UT_OStream::FUNCNAME() const \
386 { \
387  return myOS->FUNCNAME(); \
388 } \
389 inline int UT_OStream::FUNCNAME(int value) \
390 { \
391  return myOS->FUNCNAME(value); \
392 } \
393 /**/
396 #undef UT_OSTREAM_ACCESSOR
397 
398 
399 #define UT_OSTREAM_INSERTER(T) \
400 inline UT_OStream & \
401 operator<<(UT_OStream &os, T value) \
402 { \
403  *os.myOS << value; \
404  return os; \
405 } \
406 /**/
416 UT_OSTREAM_INSERTER(unsigned char)
418 #ifdef UT_NEED_LONG_OSTREAM_INSERTER
419  UT_OSTREAM_INSERTER(long)
420  UT_OSTREAM_INSERTER(unsigned long)
421 #endif
422 #undef UT_OSTREAM_INSERTER
423 
424 
425 inline
426 UT_OStream::operator bool() const
427 {
428  return !myOS->fail();
429 }
430 
431 inline bool
433 {
434  return myOS->fail();
435 }
436 
437 
438 #define UT_OSTREAM_FLAG_GETTOR(FUNCNAME) \
439 inline bool UT_OStream::FUNCNAME() const { return myOS->FUNCNAME(); }
440 /**/
445 #undef UT_OSTREAM_FLAG_GETTOR
446 
447 
448 #define UT_OSTREAM_WRITE(TYPENAME) \
449 inline int64 \
450 UT_OStream::write(const TYPENAME *buf, int64 cnt, bool nl) \
451 { \
452  return myIsBinary ? bwrite(buf, cnt) : awrite(buf, cnt, nl); \
453 } \
454 /**/
455 UT_OSTREAM_WRITE(bool)
462 #undef UT_OSTREAM_WRITE
463 inline bool
464 UT_OStream::write(const UT_String &str, bool nl)
465 {
466  return myIsBinary ? bwrite(str) : awrite(str, nl);
467 }
468 inline bool
469 UT_OStream::write(const UT_StringRef &str, bool nl)
470 {
471  return myIsBinary ? bwrite(str) : awrite(str, nl);
472 }
473 inline bool
474 UT_OStream::write(const std::string &std_str, bool nl)
475 {
476  return myIsBinary ? bwrite(std_str) : awrite(std_str, nl);
477 }
478 
479 #define UT_OSTREAM_BWRITE(TYPENAME) \
480 inline int64 \
481 UT_OStream::bwrite(const TYPENAME *buf, int64 cnt) \
482 { \
483  return UTwrite(*myOS, buf, cnt).bad() ? 0 : cnt; \
484 } \
485 /**/
486 UT_OSTREAM_BWRITE(char)
487 UT_OSTREAM_BWRITE(unsigned char)
488 UT_OSTREAM_BWRITE(signed char)
495 #undef UT_OSTREAM_BWRITE
496 inline int64
497 UT_OStream::bwrite(const bool *buf, int64 cnt)
498 {
499  return UTwrite(*myOS, (char *)buf, cnt).bad() ? 0 : cnt;
500 }
501 
502 
503 #define UT_OSTREAM_AWRITE(TYPENAME) \
504 inline int64 \
505 UT_OStream::awrite(const TYPENAME *buf, int64 cnt, bool nl) \
506 { \
507  if (cnt < 1) \
508  return 0; \
509  *myOS << buf[0]; \
510  for (int64 i = 1; i < cnt; i++) \
511  *myOS << ' ' << buf[i]; \
512  *myOS << (nl ? '\n' : ' '); \
513  return myOS->bad() ? 0 : cnt; \
514 } \
515 /**/
516 UT_OSTREAM_AWRITE(char)
517 UT_OSTREAM_AWRITE(unsigned char)
518 UT_OSTREAM_AWRITE(signed char)
527 #undef UT_OSTREAM_AWRITE
528 inline int64
529 UT_OStream::awrite(const bool *buf, int64 cnt, bool nl)
530 {
531  if (cnt < 1)
532  return 0;
533  *myOS << int32(buf[0] ? 1 : 0);
534  for (int64 i = 1; i < cnt; i++)
535  *myOS << ' ' << int32(buf[i] ? 1 : 0);
536  *myOS << (nl ? '\n' : ' ');
537  return myOS->bad() ? 0 : cnt;
538 }
539 
540 
541 #define UT_OSTREAM_WRITEFLOAT(TYPENAME) \
542 template <typename DEST_TYPE> \
543 inline int64 \
544 UT_OStream::write(const TYPENAME *buf, int64 cnt, bool nl) \
545 { \
546  return myIsBinary ? bwrite<DEST_TYPE>(buf, cnt) : awrite(buf, cnt, nl); \
547 } \
548 template <typename DEST_TYPE> \
549 inline int64 \
550 UT_OStream::bwrite(const TYPENAME *buf, int64 cnt) \
551 { \
552  return UTwrite<DEST_TYPE>(*myOS, buf, cnt).bad() ? 0 : cnt; \
553 } \
554 /**/
557 #undef UT_OSTREAM_WRITEFLOAT
558 
559 
560 #endif // __UT_OSTREAM_H_INCLUDED__
UT_OStream(Uninitialized, UT_IOS_TYPE bin)
Definition: UT_OStream.h:62
void setStreamRef(UT_OStream &os)
Definition: UT_OStream.h:122
#define UT_OSTREAM_INSERTER(T)
Definition: UT_OStream.h:399
UT_OStream & seekp(pos_type offset, seek_type dir=UT_IOS_SEEK_BEG)
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:360
UT_OStream & write(const char_type *str, int64 count)
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:346
UT_AutoPrecision(UT_OStream &os, int new_precision)
Definition: UT_OStream.h:301
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
bool isBinary() const
Determine if the stream is currently in ASCII or binary mode.
Definition: UT_OStream.h:128
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
const GLuint GLenum const void * binary
Definition: glcorearb.h:1923
std::ostream & getOStream()
Manipulation of the underlying std::ostream reference.
Definition: UT_OStream.h:116
#define UT_OSTREAM_ACCESSOR(FUNCNAME)
Definition: UT_OStream.h:384
int64 bwrite(const bool *buf, int64 cnt=1)
Definition: UT_OStream.h:497
#define UT_API
Definition: UT_API.h:12
UT_IOS_TYPE
Definition: UT_IOS.h:15
UT_IOS_SEEK
Definition: UT_IOS.h:21
UT_OStream & operator<<(UT_OStream &os, UT_OStream &(*manip)(UT_OStream &))
Definition: UT_OStream.h:328
#define UT_OSTREAM_FLAG_GETTOR(FUNCNAME)
Definition: UT_OStream.h:438
png_uint_32 i
Definition: png.h:2877
int64 awrite(const bool *buf, int64 cnt=1, bool nl=false)
Definition: UT_OStream.h:529
UT_OStream(std::ostream &os, bool binary)
Definition: UT_OStream.h:54
void setOStream(std::ostream &os)
Manipulation of the underlying std::ostream reference.
Definition: UT_OStream.h:117
UT_OStream & put(char_type ch)
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:339
GLint GLsizei width
Definition: glcorearb.h:102
int64 pos_type
Definition: UT_OStream.h:39
UT_OStream(std::ostream &os, UT_IOS_TYPE bin)
Definition: UT_OStream.h:49
long long int64
Definition: SYS_Types.h:106
unsigned long long uint64
Definition: SYS_Types.h:107
#define SYS_SAFE_BOOL
Definition: SYS_Compiler.h:56
double fpreal64
Definition: SYS_Types.h:191
GLuint GLuint end
Definition: glcorearb.h:474
GLintptr offset
Definition: glcorearb.h:664
#define UT_OSTREAM_BWRITE(TYPENAME)
Definition: UT_OStream.h:479
pos_type tellp()
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:378
bool isAscii() const
Determine if the stream is currently in ASCII or binary mode.
Definition: UT_OStream.h:127
#define UT_OSTREAM_WRITE(TYPENAME)
Definition: UT_OStream.h:448
int int32
Definition: SYS_Types.h:34
UT_OStream & flush()
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:353
#define UT_OSTREAM_WRITEFLOAT(TYPENAME)
Definition: UT_OStream.h:541
GLint GLsizei count
Definition: glcorearb.h:404
Uninitialized
Tag for suppressing uninitialization.
Definition: UT_OStream.h:43
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
GLenum GLint GLint * precision
Definition: glcorearb.h:1924
unsigned short uint16
Definition: SYS_Types.h:33
GLsizei const GLfloat * value
Definition: glcorearb.h:823
UT_OStreamAutoBinary(UT_OStream &os, bool bin)
Definition: UT_OStream.h:276
char char_type
Definition: UT_OStream.h:38
UT_OStreamAutoBinary(UT_OStream &os, UT_IOS_TYPE bin)
Definition: UT_OStream.h:270
bool operator!() const
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:432
#define const
Definition: zconf.h:214
short int16
Definition: SYS_Types.h:32
void write(T &out, bool v)
Definition: ImfXdr.h:332
float fpreal32
Definition: SYS_Types.h:190
Turn the stream into binary or ASCII while in variable scope.
Definition: UT_OStream.h:267
#define UT_OSTREAM_AWRITE(TYPENAME)
Definition: UT_OStream.h:503
UT_IOS_SEEK seek_type
Definition: UT_OStream.h:40
void setstate(std::ios::iostate state)
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:91
unsigned int uint32
Definition: SYS_Types.h:35