Panda3D
Loading...
Searching...
No Matches
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 */
17INLINE StreamWrapperBase::
18StreamWrapperBase() {
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 */
38acquire() {
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 */
53release() {
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 */
66ref() 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 */
75unref() const {
76 return AtomicAdjust::dec(_ref_count);
77}
78
79/**
80 *
81 */
82INLINE IStreamWrapper::
83IStreamWrapper(std::istream *stream, bool owns_pointer) :
84 _istream(stream),
85 _owns_pointer(owns_pointer)
86{
87}
88
89/**
90 *
91 */
92INLINE IStreamWrapper::
93IStreamWrapper(std::istream &stream) :
94 _istream(&stream),
95 _owns_pointer(false)
96{
97}
98
99/**
100 * Returns the istream this object is wrapping.
101 */
102INLINE std::istream *IStreamWrapper::
103get_istream() const {
104 return _istream;
105}
106
107/**
108 * Atomically reads a single character from the stream.
109 */
111get() {
112 int result;
113 acquire();
114 result = _istream->get();
115 release();
116 return result;
117}
118
119
120/**
121 *
122 */
123INLINE OStreamWrapper::
124OStreamWrapper(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 */
136INLINE OStreamWrapper::
137OStreamWrapper(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 */
149INLINE std::ostream *OStreamWrapper::
150get_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 */
159put(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 */
171INLINE StreamWrapper::
172StreamWrapper(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 */
183INLINE StreamWrapper::
184StreamWrapper(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 */
195INLINE std::iostream *StreamWrapper::
196get_iostream() const {
197 return _iostream;
198}
static bool dec(Integer &var)
Atomically decrements the indicated variable and returns true if the new value is nonzero,...
static void inc(Integer &var)
Atomically increments the indicated variable.
This class provides a locking wrapper around an arbitrary istream pointer.
get_istream
Returns the istream this object is wrapping.
int get()
Atomically reads a single character from the stream.
This class provides a locking wrapper around an arbitrary ostream pointer.
get_ostream
Returns the ostream this object is wrapping.
bool put(char c)
Atomically writes a single character to the stream.
bool unref() const
Decrements the reference count.
void release()
Releases the internal lock.
void acquire()
Acquires the internal lock.
void ref() const
Increments the reference count.
get_iostream
Returns the iostream this object is wrapping.