HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  // Streams cannot be moved. This results MSVC 2015 failing to build
256  // when trying to export the move constructor, which std::ios_base
257  // does not have.
258  UT_ONullStream(UT_ONullStream &&) = delete;
259 };
260 
261 
262 //////////////////////////////////////////////////////////////////////////////
263 
264 /// Turn the stream into binary or ASCII while in variable scope.
266 {
267 public:
269  : myOS(os)
270  , myOldIsBinary(os.isBinary())
271  {
272  myOS.myIsBinary = (bin == UT_IOS_BINARY);
273  }
274  explicit UT_OStreamAutoBinary(UT_OStream &os, bool bin)
275  : myOS(os)
276  , myOldIsBinary(os.isBinary())
277  {
278  myOS.myIsBinary = bin;
279  }
280 
282  {
283  myOS.myIsBinary = myOldIsBinary; // restore
284  }
285 
286 private:
287  UT_OStream & myOS;
288  bool myOldIsBinary;
289 };
290 
291 
292 //////////////////////////////////////////////////////////////////////////////
293 
294 /// UT_AutoPrecision allows one to temporarily change a stream's precision
295 /// for the life of this object.
297 {
298 public:
299  explicit UT_AutoPrecision(UT_OStream &os, int new_precision)
300  : myStream(os)
301  , myOldPrecision(os.precision(new_precision))
302  {
303  }
304 
306  {
307  (void) myStream.precision(myOldPrecision); // restore
308  }
309 
310 private:
311  UT_OStream & myStream;
312  int myOldPrecision;
313 };
314 
315 
316 //////////////////////////////////////////////////////////////////////////////
317 //
318 // UT_OStream Implementation
319 //
320 
321 //
322 // Manipulators
323 //
324 
325 inline UT_OStream &
327 {
328  return manip(os);
329 }
330 
331 
332 //
333 // Inline function implementations
334 //
335 
336 inline UT_OStream &
338 {
339  myOS->put(ch);
340  return *this;
341 }
342 
343 inline UT_OStream &
345 {
346  myOS->write(str, (std::streamsize)count);
347  return *this;
348 }
349 
350 inline UT_OStream &
352 {
353  myOS->flush();
354  return *this;
355 }
356 
357 inline UT_OStream &
359 {
360  switch (dir)
361  {
362  case UT_IOS_SEEK_BEG:
363  myOS->seekp(offset, std::ios_base::beg);
364  break;
365  case UT_IOS_SEEK_CUR:
366  myOS->seekp(offset, std::ios_base::cur);
367  break;
368  case UT_IOS_SEEK_END:
369  myOS->seekp(offset, std::ios_base::end);
370  break;
371  }
372  return *this;
373 }
374 
377 {
378  return myOS->tellp();
379 }
380 
381 
382 #define UT_OSTREAM_ACCESSOR(FUNCNAME) \
383 inline int UT_OStream::FUNCNAME() const \
384 { \
385  return myOS->FUNCNAME(); \
386 } \
387 inline int UT_OStream::FUNCNAME(int value) \
388 { \
389  return myOS->FUNCNAME(value); \
390 } \
391 /**/
394 #undef UT_OSTREAM_ACCESSOR
395 
396 
397 #define UT_OSTREAM_INSERTER(T) \
398 inline UT_OStream & \
399 operator<<(UT_OStream &os, T value) \
400 { \
401  *os.myOS << value; \
402  return os; \
403 } \
404 /**/
414 UT_OSTREAM_INSERTER(unsigned char)
416 #ifdef UT_NEED_LONG_OSTREAM_INSERTER
417  UT_OSTREAM_INSERTER(long)
418  UT_OSTREAM_INSERTER(unsigned long)
419 #endif
420 #undef UT_OSTREAM_INSERTER
421 
422 
423 inline
424 UT_OStream::operator bool() const
425 {
426  return !myOS->fail();
427 }
428 
429 inline bool
431 {
432  return myOS->fail();
433 }
434 
435 
436 #define UT_OSTREAM_FLAG_GETTOR(FUNCNAME) \
437 inline bool UT_OStream::FUNCNAME() const { return myOS->FUNCNAME(); }
438 /**/
443 #undef UT_OSTREAM_FLAG_GETTOR
444 
445 
446 #define UT_OSTREAM_WRITE(TYPENAME) \
447 inline int64 \
448 UT_OStream::write(const TYPENAME *buf, int64 cnt, bool nl) \
449 { \
450  return myIsBinary ? bwrite(buf, cnt) : awrite(buf, cnt, nl); \
451 } \
452 /**/
453 UT_OSTREAM_WRITE(bool)
460 #undef UT_OSTREAM_WRITE
461 inline bool
462 UT_OStream::write(const UT_String &str, bool nl)
463 {
464  return myIsBinary ? bwrite(str) : awrite(str, nl);
465 }
466 inline bool
467 UT_OStream::write(const UT_StringRef &str, bool nl)
468 {
469  return myIsBinary ? bwrite(str) : awrite(str, nl);
470 }
471 inline bool
472 UT_OStream::write(const std::string &std_str, bool nl)
473 {
474  return myIsBinary ? bwrite(std_str) : awrite(std_str, nl);
475 }
476 
477 #define UT_OSTREAM_BWRITE(TYPENAME) \
478 inline int64 \
479 UT_OStream::bwrite(const TYPENAME *buf, int64 cnt) \
480 { \
481  return UTwrite(*myOS, buf, cnt).bad() ? 0 : cnt; \
482 } \
483 /**/
484 UT_OSTREAM_BWRITE(char)
485 UT_OSTREAM_BWRITE(unsigned char)
486 UT_OSTREAM_BWRITE(signed char)
493 #undef UT_OSTREAM_BWRITE
494 inline int64
495 UT_OStream::bwrite(const bool *buf, int64 cnt)
496 {
497  return UTwrite(*myOS, (char *)buf, cnt).bad() ? 0 : cnt;
498 }
499 
500 
501 #define UT_OSTREAM_AWRITE(TYPENAME) \
502 inline int64 \
503 UT_OStream::awrite(const TYPENAME *buf, int64 cnt, bool nl) \
504 { \
505  if (cnt < 1) \
506  return 0; \
507  *myOS << buf[0]; \
508  for (int64 i = 1; i < cnt; i++) \
509  *myOS << ' ' << buf[i]; \
510  *myOS << (nl ? '\n' : ' '); \
511  return myOS->bad() ? 0 : cnt; \
512 } \
513 /**/
514 UT_OSTREAM_AWRITE(char)
515 UT_OSTREAM_AWRITE(unsigned char)
516 UT_OSTREAM_AWRITE(signed char)
525 #undef UT_OSTREAM_AWRITE
526 inline int64
527 UT_OStream::awrite(const bool *buf, int64 cnt, bool nl)
528 {
529  if (cnt < 1)
530  return 0;
531  *myOS << int32(buf[0] ? 1 : 0);
532  for (int64 i = 1; i < cnt; i++)
533  *myOS << ' ' << int32(buf[i] ? 1 : 0);
534  *myOS << (nl ? '\n' : ' ');
535  return myOS->bad() ? 0 : cnt;
536 }
537 
538 
539 #define UT_OSTREAM_WRITEFLOAT(TYPENAME) \
540 template <typename DEST_TYPE> \
541 inline int64 \
542 UT_OStream::write(const TYPENAME *buf, int64 cnt, bool nl) \
543 { \
544  return myIsBinary ? bwrite<DEST_TYPE>(buf, cnt) : awrite(buf, cnt, nl); \
545 } \
546 template <typename DEST_TYPE> \
547 inline int64 \
548 UT_OStream::bwrite(const TYPENAME *buf, int64 cnt) \
549 { \
550  return UTwrite<DEST_TYPE>(*myOS, buf, cnt).bad() ? 0 : cnt; \
551 } \
552 /**/
555 #undef UT_OSTREAM_WRITEFLOAT
556 
557 
558 #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:397
UT_OStream & seekp(pos_type offset, seek_type dir=UT_IOS_SEEK_BEG)
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:358
UT_OStream & write(const char_type *str, int64 count)
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:344
UT_AutoPrecision(UT_OStream &os, int new_precision)
Definition: UT_OStream.h:299
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:382
int64 bwrite(const bool *buf, int64 cnt=1)
Definition: UT_OStream.h:495
#define UT_API
Definition: UT_API.h:13
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:326
#define UT_OSTREAM_FLAG_GETTOR(FUNCNAME)
Definition: UT_OStream.h:436
png_uint_32 i
Definition: png.h:2877
int64 awrite(const bool *buf, int64 cnt=1, bool nl=false)
Definition: UT_OStream.h:527
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:337
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:107
unsigned long long uint64
Definition: SYS_Types.h:108
#define SYS_SAFE_BOOL
Definition: SYS_Compiler.h:62
double fpreal64
Definition: SYS_Types.h:192
GLuint GLuint end
Definition: glcorearb.h:474
GLintptr offset
Definition: glcorearb.h:664
#define UT_OSTREAM_BWRITE(TYPENAME)
Definition: UT_OStream.h:477
pos_type tellp()
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:376
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:446
int int32
Definition: SYS_Types.h:35
UT_OStream & flush()
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:351
#define UT_OSTREAM_WRITEFLOAT(TYPENAME)
Definition: UT_OStream.h:539
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:34
GLsizei const GLfloat * value
Definition: glcorearb.h:823
Usd_Term operator!(Usd_PrimFlags flag)
Definition: primFlags.h:130
UT_OStreamAutoBinary(UT_OStream &os, bool bin)
Definition: UT_OStream.h:274
char char_type
Definition: UT_OStream.h:38
UT_OStreamAutoBinary(UT_OStream &os, UT_IOS_TYPE bin)
Definition: UT_OStream.h:268
bool operator!() const
Compatibility methods for std::ostream replacement.
Definition: UT_OStream.h:430
#define const
Definition: zconf.h:214
short int16
Definition: SYS_Types.h:33
void write(T &out, bool v)
Definition: ImfXdr.h:332
float fpreal32
Definition: SYS_Types.h:191
Turn the stream into binary or ASCII while in variable scope.
Definition: UT_OStream.h:265
#define UT_OSTREAM_AWRITE(TYPENAME)
Definition: UT_OStream.h:501
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:36