forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamWrapper.I
More file actions
180 lines (165 loc) · 3.35 KB
/
streamWrapper.I
File metadata and controls
180 lines (165 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file streamWrapper.I
* @author drose
* @date 2008-11-11
*/
/**
*
*/
INLINE StreamWrapperBase::
StreamWrapperBase() {
#ifdef SIMPLE_THREADS
_lock_flag = false;
#endif
}
/**
* Acquires the internal lock.
*
* User code should call this to take temporary possession of the stream and
* perform direct I/O operations on it, for instance to make several
* sequential atomic reads. You may not call any of the StreamWrapper methods
* while the lock is held, other than release().
*
* Use with extreme caution! This is a very low-level, non-recursive lock.
* You must call acquire() only once, and you must later call release()
* exactly once. Failing to do so may result in a hard deadlock with no
* available debugging features.
*/
INLINE void StreamWrapperBase::
acquire() {
_lock.acquire();
#ifdef SIMPLE_THREADS
while (_lock_flag) {
thread_yield();
}
_lock_flag = true;
#endif
}
/**
* Releases the internal lock. Must be called exactly once following a call
* to acquire(). See the cautions with acquire().
*/
INLINE void StreamWrapperBase::
release() {
#ifdef SIMPLE_THREADS
assert(_lock_flag);
_lock_flag = false;
#endif
_lock.release();
}
/**
*
*/
INLINE IStreamWrapper::
IStreamWrapper(istream *stream, bool owns_pointer) :
_istream(stream),
_owns_pointer(owns_pointer)
{
}
/**
*
*/
INLINE IStreamWrapper::
IStreamWrapper(istream &stream) :
_istream(&stream),
_owns_pointer(false)
{
}
/**
* Returns the istream this object is wrapping.
*/
INLINE istream *IStreamWrapper::
get_istream() const {
return _istream;
}
/**
* Atomically reads a single character from the stream.
*/
INLINE int IStreamWrapper::
get() {
int result;
acquire();
result = _istream->get();
release();
return result;
}
/**
*
*/
INLINE OStreamWrapper::
OStreamWrapper(ostream *stream, bool owns_pointer, bool stringstream_hack) :
_ostream(stream),
_owns_pointer(owns_pointer)
#ifdef WIN32_VC
, _stringstream_hack(stringstream_hack)
#endif
{
}
/**
*
*/
INLINE OStreamWrapper::
OStreamWrapper(ostream &stream) :
_ostream(&stream),
_owns_pointer(false)
#ifdef WIN32_VC
, _stringstream_hack(false)
#endif
{
}
/**
* Returns the ostream this object is wrapping.
*/
INLINE ostream *OStreamWrapper::
get_ostream() const {
return _ostream;
}
/**
* Atomically writes a single character to the stream. Returns true on
* success, false on failure.
*/
INLINE bool OStreamWrapper::
put(char c) {
bool success;
acquire();
_ostream->put(c);
success = !_ostream->bad();
release();
return success;
}
/**
*
*/
INLINE StreamWrapper::
StreamWrapper(iostream *stream, bool owns_pointer, bool stringstream_hack) :
IStreamWrapper(stream, false),
OStreamWrapper(stream, false, stringstream_hack),
_iostream(stream),
_owns_pointer(owns_pointer)
{
}
/**
*
*/
INLINE StreamWrapper::
StreamWrapper(iostream &stream) :
IStreamWrapper(&stream, false),
OStreamWrapper(&stream, false),
_iostream(&stream),
_owns_pointer(false)
{
}
/**
* Returns the iostream this object is wrapping.
*/
INLINE iostream *StreamWrapper::
get_iostream() const {
return _iostream;
}