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(ARM64) && !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  ~UT_OStream() = default;
69 
71 
72  /// Compatibility methods for std::ostream replacement.
73  /// @{
74 
75  UT_OStream & put(char_type ch);
76  UT_OStream & write(const char_type *str, int64 count);
77  UT_OStream & flush();
78 
79  UT_OStream & seekp(pos_type offset, seek_type dir = UT_IOS_SEEK_BEG);
80  pos_type tellp();
81 
82  int precision() const;
83  int precision(int dig);
84  int width() const;
85  int width(int dig);
86 
87  SYS_SAFE_BOOL operator bool() const;
88  bool operator!() const;
89 
90  bool bad() const;
91  bool eof() const;
92  bool fail() const;
93  bool good() const;
94 
95  void setstate(std::ios::iostate state)
96  { myOS->setstate(state); }
97 
100  friend UT_OStream & operator<<(UT_OStream &os, int32 value);
102  friend UT_OStream & operator<<(UT_OStream &os, int64 value);
106  friend UT_OStream & operator<<(UT_OStream &os, char value);
107  friend UT_OStream & operator<<(UT_OStream &os, unsigned char value);
108  friend UT_OStream & operator<<(UT_OStream &os, const char *value);
109 
110 #ifdef UT_NEED_LONG_OSTREAM_INSERTER
111  friend UT_OStream & operator<<(UT_OStream &os, long value);
112  friend UT_OStream & operator<<(UT_OStream &os, unsigned long value);
113 #endif
114 
115 
116  /// @}
117 
118  /// Manipulation of the underlying std::ostream reference
119  /// @{
120  std::ostream & getOStream() { return *myOS; }
121  void setOStream(std::ostream &os) { myOS = &os; }
122  /// @}
123 
124  /// Set the stream reference to same as the given UT_OStream object.
125  /// @note The binary flag of this stream is not modified.
127  { myOS = &os.getOStream(); }
128 
129  /// Determine if the stream is currently in ASCII or binary mode.
130  /// @{
131  bool isAscii() const { return !myIsBinary; }
132  bool isBinary() const { return myIsBinary; }
133  /// @}
134 
135  /// Write array data to the stream. It will call bwrite() or awrite()
136  /// depending on the current streaming mode. For binary output, the data
137  /// will be byte-swapped according to the Houdini standard.
138  ///
139  /// @param buf The array to write.
140  /// @param cnt The number of array elements to write.
141  /// @param nl If true, a newline is appended, else a space is
142  /// appended. This parameter is ignored for binary writes.
143  /// @return The number of elements written.
144  /// @{
145  int64 write(const bool *buf, int64 cnt=1, bool nl=false);
146  int64 write(const int16 *buf, int64 cnt=1, bool nl=false);
147  int64 write(const uint16 *buf, int64 cnt=1, bool nl=false);
148  int64 write(const int32 *buf, int64 cnt=1, bool nl=false);
149  int64 write(const uint32 *buf, int64 cnt=1, bool nl=false);
150  int64 write(const int64 *buf, int64 cnt=1, bool nl=false);
151  int64 write(const uint64 *buf, int64 cnt=1, bool nl=false);
152  template <typename DEST_TYPE>
153  int64 write(const fpreal32 *buf, int64 cnt=1, bool nl=false);
154  template <typename DEST_TYPE>
155  int64 write(const fpreal64 *buf, int64 cnt=1, bool nl=false);
156  /// @}
157 
158  /// Write string data to the stream. It will call bwrite() or awrite()
159  /// depending on the current streaming mode.
160  ///
161  /// @param str The string to write.
162  /// @param nl If true, a newline is appended, else a space is
163  /// appended. This parameter is ignored for binary writes.
164  /// @return @c true if the string was successfully written out.
165  /// @{
166  bool write(const UT_String &str, bool nl=false);
167  bool write(const UT_StringRef &str, bool nl=false);
168  bool write(const std::string &str, bool nl=false);
169  /// @}
170 
171  /// Platform consistent binary writes of data. The data will be
172  /// byte-swapped according to the Houdini standard.
173  ///
174  /// @param buf The array to write.
175  /// @param cnt The number of array elements to write.
176  /// @return The number of elements written.
177  /// @{
178  int64 bwrite(const bool *buf, int64 cnt=1);
179  int64 bwrite(const char *buf, int64 cnt=1);
180  int64 bwrite(const unsigned char *buf, int64 cnt=1);
181  int64 bwrite(const signed char *buf, int64 cnt=1);
182  int64 bwrite(const int16 *buf, int64 cnt=1);
183  int64 bwrite(const uint16 *buf, int64 cnt=1);
184  int64 bwrite(const int32 *buf, int64 cnt=1);
185  int64 bwrite(const uint32 *buf, int64 cnt=1);
186  int64 bwrite(const int64 *buf, int64 cnt=1);
187  int64 bwrite(const uint64 *buf, int64 cnt=1);
188  template <typename DEST_TYPE>
189  int64 bwrite(const fpreal32 *buf, int64 cnt=1);
190  template <typename DEST_TYPE>
191  int64 bwrite(const fpreal64 *buf, int64 cnt=1);
192  /// @}
193 
194  /// Platform consistent binary writes of strings.
195  ///
196  /// @param str The string to write.
197  /// @return @c true if the string was successfully written out.
198  /// @{
199  bool bwrite(const UT_String &str);
200  bool bwrite(const UT_StringRef &str);
201  bool bwrite(const std::string &str);
202  /// @}
203 
204 
205  /// Platform consistent ASCII writes of data.
206  ///
207  /// @param buf The array to write.
208  /// @param cnt The number of array elements to write.
209  /// @param nl If true, a newline is appended, else a space is
210  /// appended.
211  /// @return The number of elements written.
212  /// @{
213  int64 awrite(const bool *buf, int64 cnt=1, bool nl=false);
214  int64 awrite(const char *buf, int64 cnt=1, bool nl=false);
215  int64 awrite(const unsigned char *buf, int64 cnt=1,
216  bool nl=false);
217  int64 awrite(const signed char *buf, int64 cnt=1,
218  bool nl=false);
219  int64 awrite(const int16 *buf, int64 cnt=1, bool nl=false);
220  int64 awrite(const uint16 *buf, int64 cnt=1, bool nl=false);
221  int64 awrite(const int32 *buf, int64 cnt=1, bool nl=false);
222  int64 awrite(const uint32 *buf, int64 cnt=1, bool nl=false);
223  int64 awrite(const int64 *buf, int64 cnt=1, bool nl=false);
224  int64 awrite(const uint64 *buf, int64 cnt=1, bool nl=false);
225  int64 awrite(const fpreal32 *buf, int64 cnt=1, bool nl=false);
226  int64 awrite(const fpreal64 *buf, int64 cnt=1, bool nl=false);
227  /// @}
228 
229  /// Platform consistent ASCII writes of strings.
230  ///
231  /// @param str The string to write.
232  /// @param nl If true, a newline is appended, else a space is
233  /// appended. This parameter is ignored for binary writes.
234  /// @return @c true if the string was successfully written out.
235  /// @{
236  bool awrite(const UT_String &str, bool nl=false);
237  bool awrite(const UT_StringRef &str, bool nl=false);
238  bool awrite(const std::string &str, bool nl=false);
239  /// @}
240 
241 private:
242  UT_OStream(); // not implemented
243 
244 private:
245  std::ostream * myOS;
246  bool myIsBinary;
247 
249 };
250 
251 //////////////////////////////////////////////////////////////////////////////
252 
253 // Null stream, for when a stream is required but we don't need the output.
254 class UT_API UT_ONullStream : public std::ostream
255 {
256 public:
257  UT_ONullStream() : std::ios(0), std::ostream(0) {}
258 
259  // Streams cannot be moved. This results MSVC 2015 failing to build
260  // when trying to export the move constructor, which std::ios_base
261  // does not have.
262  UT_ONullStream(UT_ONullStream &&) = delete;
263 };
264 
265 
266 //////////////////////////////////////////////////////////////////////////////
267 
268 /// Turn the stream into binary or ASCII while in variable scope.
270 {
271 public:
273  : myOS(os)
274  , myOldIsBinary(os.isBinary())
275  {
276  myOS.myIsBinary = (bin == UT_IOS_BINARY);
277  }
278  explicit UT_OStreamAutoBinary(UT_OStream &os, bool bin)
279  : myOS(os)
280  , myOldIsBinary(os.isBinary())
281  {
282  myOS.myIsBinary = bin;
283  }
284 
286  {
287  myOS.myIsBinary = myOldIsBinary; // restore
288  }
289 
290 private:
291  UT_OStream & myOS;
292  bool myOldIsBinary;
293 };
294 
295 
296 //////////////////////////////////////////////////////////////////////////////
297 
298 /// UT_AutoPrecision allows one to temporarily change a stream's precision
299 /// for the life of this object.
301 {
302 public:
303  explicit UT_AutoPrecision(UT_OStream &os, int new_precision)
304  : myStream(os)
305  , myOldPrecision(os.precision(new_precision))
306  {
307  }
308 
310  {
311  (void) myStream.precision(myOldPrecision); // restore
312  }
313 
314 private:
315  UT_OStream & myStream;
316  int myOldPrecision;
317 };
318 
319 
320 //////////////////////////////////////////////////////////////////////////////
321 //
322 // UT_OStream Implementation
323 //
324 
325 //
326 // Manipulators
327 //
328 
329 inline UT_OStream &
331 {
332  return manip(os);
333 }
334 
335 
336 //
337 // Inline function implementations
338 //
339 
340 inline UT_OStream &
342 {
343  myOS->put(ch);
344  return *this;
345 }
346 
347 inline UT_OStream &
349 {
350  myOS->write(str, (std::streamsize)count);
351  return *this;
352 }
353 
354 inline UT_OStream &
356 {
357  myOS->flush();
358  return *this;
359 }
360 
361 inline UT_OStream &
363 {
364  switch (dir)
365  {
366  case UT_IOS_SEEK_BEG:
367  myOS->seekp(offset, std::ios_base::beg);
368  break;
369  case UT_IOS_SEEK_CUR:
370  myOS->seekp(offset, std::ios_base::cur);
371  break;
372  case UT_IOS_SEEK_END:
373  myOS->seekp(offset, std::ios_base::end);
374  break;
375  }
376  return *this;
377 }
378 
381 {
382  return myOS->tellp();
383 }
384 
385 
386 #define UT_OSTREAM_ACCESSOR(FUNCNAME) \
387 inline int UT_OStream::FUNCNAME() const \
388 { \
389  return myOS->FUNCNAME(); \
390 } \
391 inline int UT_OStream::FUNCNAME(int value) \
392 { \
393  return myOS->FUNCNAME(value); \
394 } \
395 /**/
398 #undef UT_OSTREAM_ACCESSOR
399 
400 
401 #define UT_OSTREAM_INSERTER(T) \
402 inline UT_OStream & \
403 operator<<(UT_OStream &os, T value) \
404 { \
405  *os.myOS << value; \
406  return os; \
407 } \
408 /**/
418 UT_OSTREAM_INSERTER(unsigned char)
419 UT_OSTREAM_INSERTER(const char *)
420 #ifdef UT_NEED_LONG_OSTREAM_INSERTER
421  UT_OSTREAM_INSERTER(long)
422  UT_OSTREAM_INSERTER(unsigned long)
423 #endif
424 #undef UT_OSTREAM_INSERTER
425 
426 
427 inline
428 UT_OStream::operator bool() const
429 {
430  return !myOS->fail();
431 }
432 
433 inline bool
435 {
436  return myOS->fail();
437 }
438 
439 
440 #define UT_OSTREAM_FLAG_GETTOR(FUNCNAME) \
441 inline bool UT_OStream::FUNCNAME() const { return myOS->FUNCNAME(); }
442 /**/
447 #undef UT_OSTREAM_FLAG_GETTOR
448 
449 
450 #define UT_OSTREAM_WRITE(TYPENAME) \
451 inline int64 \
452 UT_OStream::write(const TYPENAME *buf, int64 cnt, bool nl) \
453 { \
454  return myIsBinary ? bwrite(buf, cnt) : awrite(buf, cnt, nl); \
455 } \
456 /**/
457 UT_OSTREAM_WRITE(bool)
464 #undef UT_OSTREAM_WRITE
465 inline bool
466 UT_OStream::write(const UT_String &str, bool nl)
467 {
468  return myIsBinary ? bwrite(str) : awrite(str, nl);
469 }
470 inline bool
471 UT_OStream::write(const UT_StringRef &str, bool nl)
472 {
473  return myIsBinary ? bwrite(str) : awrite(str, nl);
474 }
475 inline bool
476 UT_OStream::write(const std::string &std_str, bool nl)
477 {
478  return myIsBinary ? bwrite(std_str) : awrite(std_str, nl);
479 }
480 
481 #define UT_OSTREAM_BWRITE(TYPENAME) \
482 inline int64 \
483 UT_OStream::bwrite(const TYPENAME *buf, int64 cnt) \
484 { \
485  return UTwrite(*myOS, buf, cnt).bad() ? 0 : cnt; \
486 } \
487 /**/
488 UT_OSTREAM_BWRITE(char)
489 UT_OSTREAM_BWRITE(unsigned char)
490 UT_OSTREAM_BWRITE(signed char)
497 #undef UT_OSTREAM_BWRITE
498 inline int64
499 UT_OStream::bwrite(const bool *buf, int64 cnt)
500 {
501  return UTwrite(*myOS, (char *)buf, cnt).bad() ? 0 : cnt;
502 }
503 
504 
505 #define UT_OSTREAM_AWRITE(TYPENAME) \
506 inline int64 \
507 UT_OStream::awrite(const TYPENAME *buf, int64 cnt, bool nl) \
508 { \
509  if (cnt < 1) \
510  return 0; \
511  *myOS << buf[0]; \
512  for (int64 i = 1; i < cnt; i++) \
513  *myOS << ' ' << buf[i]; \
514  *myOS << (nl ? '\n' : ' '); \
515  return myOS->bad() ? 0 : cnt; \
516 } \
517 /**/
518 UT_OSTREAM_AWRITE(char)
519 UT_OSTREAM_AWRITE(unsigned char)
520 UT_OSTREAM_AWRITE(signed char)
529 #undef UT_OSTREAM_AWRITE
530 inline int64
531 UT_OStream::awrite(const bool *buf, int64 cnt, bool nl)
532 {
533  if (cnt < 1)
534  return 0;
535  *myOS << int32(buf[0] ? 1 : 0);
536  for (int64 i = 1; i < cnt; i++)
537  *myOS << ' ' << int32(buf[i] ? 1 : 0);
538  *myOS << (nl ? '\n' : ' ');
539  return myOS->bad() ? 0 : cnt;
540 }
541 
542 
543 #define UT_OSTREAM_WRITEFLOAT(TYPENAME) \
544 template <typename DEST_TYPE> \
545 inline int64 \
546 UT_OStream::write(const TYPENAME *buf, int64 cnt, bool nl) \
547 { \
548  return myIsBinary ? bwrite<DEST_TYPE>(buf, cnt) : awrite(buf, cnt, nl); \
549 } \
550 template <typename DEST_TYPE> \
551 inline int64 \
552 UT_OStream::bwrite(const TYPENAME *buf, int64 cnt) \
553 { \
554  return UTwrite<DEST_TYPE>(*myOS, buf, cnt).bad() ? 0 : cnt; \
555 } \
556 /**/
559 #undef UT_OSTREAM_WRITEFLOAT
560 
561 
562 #endif // __UT_OSTREAM_H_INCLUDED__
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
unsigned short uint16
Definition: SYS_Types.h:38
int int32
Definition: SYS_Types.h:39
UT_OStream(Uninitialized, UT_IOS_TYPE bin)
Definition: UT_OStream.h:62
void setStreamRef(UT_OStream &os)
Definition: UT_OStream.h:126
#define UT_OSTREAM_INSERTER(T)
Definition: UT_OStream.h:401
UT_OStream & seekp(pos_type offset, seek_type dir=UT_IOS_SEEK_BEG)
Definition: UT_OStream.h:362
void
Definition: png.h:1083
UT_OStream & write(const char_type *str, int64 count)
Definition: UT_OStream.h:348
UT_AutoPrecision(UT_OStream &os, int new_precision)
Definition: UT_OStream.h:303
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
bool isBinary() const
Definition: UT_OStream.h:132
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
std::ostream & getOStream()
Definition: UT_OStream.h:120
#define UT_OSTREAM_ACCESSOR(FUNCNAME)
Definition: UT_OStream.h:386
int64 bwrite(const bool *buf, int64 cnt=1)
Definition: UT_OStream.h:499
#define UT_API
Definition: UT_API.h:14
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:330
unsigned long long uint64
Definition: SYS_Types.h:117
#define UT_OSTREAM_FLAG_GETTOR(FUNCNAME)
Definition: UT_OStream.h:440
float fpreal32
Definition: SYS_Types.h:200
int64 awrite(const bool *buf, int64 cnt=1, bool nl=false)
Definition: UT_OStream.h:531
UT_OStream(std::ostream &os, bool binary)
Definition: UT_OStream.h:54
void setOStream(std::ostream &os)
Definition: UT_OStream.h:121
UT_OStream & put(char_type ch)
Definition: UT_OStream.h:341
int64 pos_type
Definition: UT_OStream.h:39
double fpreal64
Definition: SYS_Types.h:201
UT_OStream(std::ostream &os, UT_IOS_TYPE bin)
Definition: UT_OStream.h:49
GLintptr offset
Definition: glcorearb.h:665
#define SYS_SAFE_BOOL
Definition: SYS_Compiler.h:55
GLuint GLuint end
Definition: glcorearb.h:475
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define UT_OSTREAM_BWRITE(TYPENAME)
Definition: UT_OStream.h:481
pos_type tellp()
Definition: UT_OStream.h:380
long long int64
Definition: SYS_Types.h:116
bool isAscii() const
Definition: UT_OStream.h:131
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
#define UT_OSTREAM_WRITE(TYPENAME)
Definition: UT_OStream.h:450
UT_OStream & flush()
Definition: UT_OStream.h:355
#define UT_OSTREAM_WRITEFLOAT(TYPENAME)
Definition: UT_OStream.h:543
Uninitialized
Tag for suppressing uninitialization.
Definition: UT_OStream.h:43
GLenum GLint GLint * precision
Definition: glcorearb.h:1925
UT_OStreamAutoBinary(UT_OStream &os, bool bin)
Definition: UT_OStream.h:278
short int16
Definition: SYS_Types.h:37
char char_type
Definition: UT_OStream.h:38
UT_OStreamAutoBinary(UT_OStream &os, UT_IOS_TYPE bin)
Definition: UT_OStream.h:272
unsigned int uint32
Definition: SYS_Types.h:40
GLint GLsizei width
Definition: glcorearb.h:103
Definition: core.h:1131
bool operator!() const
Definition: UT_OStream.h:434
Turn the stream into binary or ASCII while in variable scope.
Definition: UT_OStream.h:269
GLint GLsizei count
Definition: glcorearb.h:405
#define UT_OSTREAM_AWRITE(TYPENAME)
Definition: UT_OStream.h:505
UT_IOS_SEEK seek_type
Definition: UT_OStream.h:40