Panda3D
streamWrapper.h
1 // Filename: streamWrapper.h
2 // Created by: drose (11Nov08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef STREAMWRAPPER_H
16 #define STREAMWRAPPER_H
17 
18 #include "dtoolbase.h"
19 #include "mutexImpl.h"
20 
21 ////////////////////////////////////////////////////////////////////
22 // Class : StreamWrapperBase
23 // Description : The base class for both IStreamWrapper and
24 // OStreamWrapper, this provides the common locking
25 // interface.
26 ////////////////////////////////////////////////////////////////////
27 class EXPCL_DTOOLCONFIG StreamWrapperBase {
28 protected:
29  INLINE StreamWrapperBase();
30 
31 PUBLISHED:
32  INLINE void acquire();
33  INLINE void release();
34 
35 private:
36  MutexImpl _lock;
37 #ifdef SIMPLE_THREADS
38  // In the SIMPLE_THREADS case, we need to use a bool flag, because
39  // MutexImpl defines to nothing in this case--but we still need to
40  // achieve a form of locking, since I/O operations can cause the
41  // thread to swap without warning.
42  bool _lock_flag;
43 #endif
44 };
45 
46 ////////////////////////////////////////////////////////////////////
47 // Class : IStreamWrapper
48 // Description : This class provides a locking wrapper around an
49 // arbitrary istream pointer. A thread may use this
50 // class to perform an atomic seek/read/gcount
51 // operation.
52 ////////////////////////////////////////////////////////////////////
53 class EXPCL_DTOOLCONFIG IStreamWrapper : virtual public StreamWrapperBase {
54 public:
55  INLINE IStreamWrapper(istream *stream, bool owns_pointer);
56 PUBLISHED:
57  INLINE IStreamWrapper(istream &stream);
58  ~IStreamWrapper();
59 
60  INLINE istream *get_istream() const;
61 
62 public:
63  void read(char *buffer, streamsize num_bytes);
64  void read(char *buffer, streamsize num_bytes, streamsize &read_bytes);
65  void read(char *buffer, streamsize num_bytes, streamsize &read_bytes, bool &eof);
66  void seek_read(streamsize pos, char *buffer, streamsize num_bytes, streamsize &read_bytes, bool &eof);
67  INLINE int get();
68  streamsize seek_gpos_eof();
69 
70 private:
71  istream *_istream;
72  bool _owns_pointer;
73 };
74 
75 ////////////////////////////////////////////////////////////////////
76 // Class : OStreamWrapper
77 // Description : This class provides a locking wrapper around an
78 // arbitrary ostream pointer. A thread may use this
79 // class to perform an atomic seek/write operation.
80 ////////////////////////////////////////////////////////////////////
81 class EXPCL_DTOOLCONFIG OStreamWrapper : virtual public StreamWrapperBase {
82 public:
83  INLINE OStreamWrapper(ostream *stream, bool owns_pointer, bool stringstream_hack = false);
84 PUBLISHED:
85  INLINE OStreamWrapper(ostream &stream);
86  ~OStreamWrapper();
87 
88  INLINE ostream *get_ostream() const;
89 
90 public:
91  void write(const char *buffer, streamsize num_bytes);
92  void write(const char *buffer, streamsize num_bytes, bool &fail);
93  void seek_write(streamsize pos, const char *buffer, streamsize num_bytes, bool &fail);
94  void seek_eof_write(const char *buffer, streamsize num_bytes, bool &fail);
95  INLINE bool put(char c);
96  streamsize seek_ppos_eof();
97 
98 private:
99  ostream *_ostream;
100  bool _owns_pointer;
101 
102  // This flag is necessary to work around a weird quirk in the MSVS
103  // C++ runtime library: an empty stringstream cannot successfully
104  // seekp(0), until some data has been written to the stream. When
105  // this flag is set true, we know we have a possibly-empty
106  // stringstream, so we allow seekp(0) to fail silently, knowing that
107  // there's no harm in this case.
108 #ifdef WIN32_VC
109  bool _stringstream_hack;
110 #endif
111 };
112 
113 ////////////////////////////////////////////////////////////////////
114 // Class : StreamWrapper
115 // Description : This class provides a locking wrapper around a
116 // combination ostream/istream pointer.
117 ////////////////////////////////////////////////////////////////////
118 class EXPCL_DTOOLCONFIG StreamWrapper : public IStreamWrapper, public OStreamWrapper {
119 public:
120  INLINE StreamWrapper(iostream *stream, bool owns_pointer, bool stringstream_hack = false);
121 PUBLISHED:
122  INLINE StreamWrapper(iostream &stream);
123  ~StreamWrapper();
124 
125  INLINE iostream *get_iostream() const;
126 
127 private:
128  iostream *_iostream;
129  bool _owns_pointer;
130 };
131 
132 #include "streamWrapper.I"
133 
134 #endif
This class provides a locking wrapper around a combination ostream/istream pointer.
This class provides a locking wrapper around an arbitrary istream pointer.
Definition: streamWrapper.h:53
A fake mutex implementation for single-threaded applications that don't need any synchronization cont...
This class provides a locking wrapper around an arbitrary ostream pointer.
Definition: streamWrapper.h:81
The base class for both IStreamWrapper and OStreamWrapper, this provides the common locking interface...
Definition: streamWrapper.h:27