Panda3D
streamWrapper.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file streamWrapper.I
10  * @author drose
11  * @date 2008-11-11
12  */
13 
14 /**
15  *
16  */
17 INLINE StreamWrapperBase::
18 StreamWrapperBase() {
19 #ifdef SIMPLE_THREADS
20  _lock_flag = false;
21 #endif
22 }
23 
24 /**
25  * Acquires the internal lock.
26  *
27  * User code should call this to take temporary possession of the stream and
28  * perform direct I/O operations on it, for instance to make several
29  * sequential atomic reads. You may not call any of the StreamWrapper methods
30  * while the lock is held, other than release().
31  *
32  * Use with extreme caution! This is a very low-level, non-recursive lock.
33  * You must call acquire() only once, and you must later call release()
34  * exactly once. Failing to do so may result in a hard deadlock with no
35  * available debugging features.
36  */
37 INLINE void StreamWrapperBase::
39  _lock.lock();
40 #ifdef SIMPLE_THREADS
41  while (_lock_flag) {
42  thread_yield();
43  }
44  _lock_flag = true;
45 #endif
46 }
47 
48 /**
49  * Releases the internal lock. Must be called exactly once following a call
50  * to acquire(). See the cautions with acquire().
51  */
52 INLINE void StreamWrapperBase::
54 #ifdef SIMPLE_THREADS
55  assert(_lock_flag);
56  _lock_flag = false;
57 #endif
58  _lock.unlock();
59 }
60 
61 /**
62  * Increments the reference count. Only has impact if the class that manages
63  * this StreamWrapper's lifetime (eg. Multifile) respects it.
64  */
65 INLINE void StreamWrapperBase::
66 ref() const {
67  AtomicAdjust::inc(_ref_count);
68 }
69 
70 /**
71  * Decrements the reference count. Only has impact if the class that manages
72  * this StreamWrapper's lifetime (eg. Multifile) respects it.
73  */
74 INLINE bool StreamWrapperBase::
75 unref() const {
76  return AtomicAdjust::dec(_ref_count);
77 }
78 
79 /**
80  *
81  */
82 INLINE IStreamWrapper::
83 IStreamWrapper(std::istream *stream, bool owns_pointer) :
84  _istream(stream),
85  _owns_pointer(owns_pointer)
86 {
87 }
88 
89 /**
90  *
91  */
92 INLINE IStreamWrapper::
93 IStreamWrapper(std::istream &stream) :
94  _istream(&stream),
95  _owns_pointer(false)
96 {
97 }
98 
99 /**
100  * Returns the istream this object is wrapping.
101  */
102 INLINE std::istream *IStreamWrapper::
103 get_istream() const {
104  return _istream;
105 }
106 
107 /**
108  * Atomically reads a single character from the stream.
109  */
110 INLINE int IStreamWrapper::
111 get() {
112  int result;
113  acquire();
114  result = _istream->get();
115  release();
116  return result;
117 }
118 
119 
120 /**
121  *
122  */
123 INLINE OStreamWrapper::
124 OStreamWrapper(std::ostream *stream, bool owns_pointer, bool stringstream_hack) :
125  _ostream(stream),
126  _owns_pointer(owns_pointer)
127 #ifdef WIN32_VC
128 , _stringstream_hack(stringstream_hack)
129 #endif
130 {
131 }
132 
133 /**
134  *
135  */
136 INLINE OStreamWrapper::
137 OStreamWrapper(std::ostream &stream) :
138  _ostream(&stream),
139  _owns_pointer(false)
140 #ifdef WIN32_VC
141 , _stringstream_hack(false)
142 #endif
143 {
144 }
145 
146 /**
147  * Returns the ostream this object is wrapping.
148  */
149 INLINE std::ostream *OStreamWrapper::
150 get_ostream() const {
151  return _ostream;
152 }
153 
154 /**
155  * Atomically writes a single character to the stream. Returns true on
156  * success, false on failure.
157  */
158 INLINE bool OStreamWrapper::
159 put(char c) {
160  bool success;
161  acquire();
162  _ostream->put(c);
163  success = !_ostream->bad();
164  release();
165  return success;
166 }
167 
168 /**
169  *
170  */
171 INLINE StreamWrapper::
172 StreamWrapper(std::iostream *stream, bool owns_pointer, bool stringstream_hack) :
173  IStreamWrapper(stream, false),
174  OStreamWrapper(stream, false, stringstream_hack),
175  _iostream(stream),
176  _owns_pointer(owns_pointer)
177 {
178 }
179 
180 /**
181  *
182  */
183 INLINE StreamWrapper::
184 StreamWrapper(std::iostream &stream) :
185  IStreamWrapper(&stream, false),
186  OStreamWrapper(&stream, false),
187  _iostream(&stream),
188  _owns_pointer(false)
189 {
190 }
191 
192 /**
193  * Returns the iostream this object is wrapping.
194  */
195 INLINE std::iostream *StreamWrapper::
196 get_iostream() const {
197  return _iostream;
198 }
static void inc(Integer &var)
Atomically increments the indicated variable.
static bool dec(Integer &var)
Atomically decrements the indicated variable and returns true if the new value is nonzero,...
int get()
Atomically reads a single character from the stream.
void release()
Releases the internal lock.
Definition: streamWrapper.I:53
This class provides a locking wrapper around an arbitrary istream pointer.
Definition: streamWrapper.h:59
bool put(char c)
Atomically writes a single character to the stream.
void acquire()
Acquires the internal lock.
Definition: streamWrapper.I:38
void ref() const
Increments the reference count.
Definition: streamWrapper.I:66
This class provides a locking wrapper around an arbitrary ostream pointer.
Definition: streamWrapper.h:86
bool unref() const
Decrements the reference count.
Definition: streamWrapper.I:75