Panda3D
streamWrapper.I
1 // Filename: streamWrapper.I
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: StreamWrapperBase::Constructor
18 // Access: Protected
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE StreamWrapperBase::
22 StreamWrapperBase() {
23 #ifdef SIMPLE_THREADS
24  _lock_flag = false;
25 #endif
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: StreamWrapperBase::acquire
30 // Access: Published
31 // Description: Acquires the internal lock.
32 //
33 // User code should call this to take temporary
34 // possession of the stream and perform direct I/O
35 // operations on it, for instance to make several
36 // sequential atomic reads. You may not call any of the
37 // StreamWrapper methods while the lock is held, other
38 // than release().
39 //
40 // Use with extreme caution! This is a very low-level,
41 // non-recursive lock. You must call acquire() only
42 // once, and you must later call release() exactly once.
43 // Failing to do so may result in a hard deadlock with
44 // no available debugging features.
45 ////////////////////////////////////////////////////////////////////
46 INLINE void StreamWrapperBase::
48  _lock.acquire();
49 #ifdef SIMPLE_THREADS
50  while (_lock_flag) {
51  thread_yield();
52  }
53  _lock_flag = true;
54 #endif
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: StreamWrapperBase::release
59 // Access: Published
60 // Description: Releases the internal lock. Must be called exactly
61 // once following a call to acquire(). See the cautions
62 // with acquire().
63 ////////////////////////////////////////////////////////////////////
64 INLINE void StreamWrapperBase::
66 #ifdef SIMPLE_THREADS
67  assert(_lock_flag);
68  _lock_flag = false;
69 #endif
70  _lock.release();
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: IStreamWrapper::Constructor
75 // Access: Public
76 // Description:
77 ////////////////////////////////////////////////////////////////////
78 INLINE IStreamWrapper::
79 IStreamWrapper(istream *stream, bool owns_pointer) :
80  _istream(stream),
81  _owns_pointer(owns_pointer)
82 {
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: IStreamWrapper::Constructor
87 // Access: Published
88 // Description:
89 ////////////////////////////////////////////////////////////////////
90 INLINE IStreamWrapper::
91 IStreamWrapper(istream &stream) :
92  _istream(&stream),
93  _owns_pointer(false)
94 {
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: IStreamWrapper::get_istream
99 // Access: Published
100 // Description: Returns the istream this object is wrapping.
101 ////////////////////////////////////////////////////////////////////
102 INLINE istream *IStreamWrapper::
103 get_istream() const {
104  return _istream;
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: IStreamWrapper::get
109 // Access: Public
110 // Description: Atomically reads a single character from the stream.
111 ////////////////////////////////////////////////////////////////////
112 INLINE int IStreamWrapper::
113 get() {
114  int result;
115  acquire();
116  result = _istream->get();
117  release();
118  return result;
119 }
120 
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: OStreamWrapper::Constructor
124 // Access: Public
125 // Description:
126 ////////////////////////////////////////////////////////////////////
127 INLINE OStreamWrapper::
128 OStreamWrapper(ostream *stream, bool owns_pointer, bool stringstream_hack) :
129  _ostream(stream),
130  _owns_pointer(owns_pointer)
131 #ifdef WIN32_VC
132 , _stringstream_hack(stringstream_hack)
133 #endif
134 {
135 }
136 
137 ////////////////////////////////////////////////////////////////////
138 // Function: OStreamWrapper::Constructor
139 // Access: Published
140 // Description:
141 ////////////////////////////////////////////////////////////////////
142 INLINE OStreamWrapper::
143 OStreamWrapper(ostream &stream) :
144  _ostream(&stream),
145  _owns_pointer(false)
146 #ifdef WIN32_VC
147 , _stringstream_hack(false)
148 #endif
149 {
150 }
151 
152 ////////////////////////////////////////////////////////////////////
153 // Function: OStreamWrapper::get_ostream
154 // Access: Published
155 // Description: Returns the ostream this object is wrapping.
156 ////////////////////////////////////////////////////////////////////
157 INLINE ostream *OStreamWrapper::
158 get_ostream() const {
159  return _ostream;
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function: OStreamWrapper::put
164 // Access: Public
165 // Description: Atomically writes a single character to the stream.
166 // Returns true on success, false on failure.
167 ////////////////////////////////////////////////////////////////////
168 INLINE bool OStreamWrapper::
169 put(char c) {
170  bool success;
171  acquire();
172  _ostream->put(c);
173  success = !_ostream->bad();
174  release();
175  return success;
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function: StreamWrapper::Constructor
180 // Access: Public
181 // Description:
182 ////////////////////////////////////////////////////////////////////
183 INLINE StreamWrapper::
184 StreamWrapper(iostream *stream, bool owns_pointer, bool stringstream_hack) :
185  IStreamWrapper(stream, false),
186  OStreamWrapper(stream, false, stringstream_hack),
187  _iostream(stream),
188  _owns_pointer(owns_pointer)
189 {
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function: StreamWrapper::Constructor
194 // Access: Published
195 // Description:
196 ////////////////////////////////////////////////////////////////////
197 INLINE StreamWrapper::
198 StreamWrapper(iostream &stream) :
199  IStreamWrapper(&stream, false),
200  OStreamWrapper(&stream, false),
201  _iostream(&stream),
202  _owns_pointer(false)
203 {
204 }
205 
206 ////////////////////////////////////////////////////////////////////
207 // Function: StreamWrapper::get_iostream
208 // Access: Published
209 // Description: Returns the iostream this object is wrapping.
210 ////////////////////////////////////////////////////////////////////
211 INLINE iostream *StreamWrapper::
212 get_iostream() const {
213  return _iostream;
214 }
ostream * get_ostream() const
Returns the ostream this object is wrapping.
iostream * get_iostream() const
Returns the iostream this object is wrapping.
int get()
Atomically reads a single character from the stream.
void release()
Releases the internal lock.
Definition: streamWrapper.I:65
This class provides a locking wrapper around an arbitrary istream pointer.
Definition: streamWrapper.h:53
istream * get_istream() const
Returns the istream this object is wrapping.
bool put(char c)
Atomically writes a single character to the stream.
void acquire()
Acquires the internal lock.
Definition: streamWrapper.I:47
This class provides a locking wrapper around an arbitrary ostream pointer.
Definition: streamWrapper.h:81