Panda3D
bamReader.I
1 // Filename: bamReader.I
2 // Created by: jason (12Jun00)
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: BamReaderAuxData::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE BamReaderAuxData::
22 BamReaderAuxData() {
23 }
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: BamReader::get_source
27 // Access: Published
28 // Description: Returns the current source of the BamReader as set by
29 // set_source() or the constructor.
30 ////////////////////////////////////////////////////////////////////
33  return _source;
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: BamReader::get_filename
38 // Access: Published
39 // Description: If a BAM is a file, then the BamReader should
40 // contain the name of the file. This enables the
41 // reader to interpret pathnames in the BAM as relative
42 // to the directory containing the BAM.
43 ////////////////////////////////////////////////////////////////////
44 INLINE const Filename &BamReader::
45 get_filename() const {
46  if (_source != (DatagramGenerator *)NULL) {
47  return _source->get_filename();
48  }
49  static const Filename empty_filename;
50  return empty_filename;
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: BamReader::get_loader_options
55 // Access: Published
56 // Description: Returns the LoaderOptions passed to the loader when
57 // the model was requested, if any.
58 ////////////////////////////////////////////////////////////////////
59 INLINE const LoaderOptions &BamReader::
61  return _loader_options;
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: BamReader::set_loader_options
66 // Access: Published
67 // Description: Specifies the LoaderOptions for this BamReader.
68 ////////////////////////////////////////////////////////////////////
69 INLINE void BamReader::
71  _loader_options = options;
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: BamReader::is_eof
76 // Access: Published
77 // Description: Returns true if the reader has reached end-of-file,
78 // false otherwise. This call is only valid after a
79 // call to read_object().
80 ////////////////////////////////////////////////////////////////////
81 INLINE bool BamReader::
82 is_eof() const {
83  nassertr(_source != NULL, true);
84  return _source->is_eof();
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: BamReader::get_file_major_ver
89 // Access: Published
90 // Description: Returns the major version number of the Bam file
91 // currently being read.
92 ////////////////////////////////////////////////////////////////////
93 INLINE int BamReader::
95  return _file_major;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: BamReader::get_file_minor_ver
100 // Access: Published
101 // Description: Returns the minor version number of the Bam file
102 // currently being read.
103 ////////////////////////////////////////////////////////////////////
104 INLINE int BamReader::
106  return _file_minor;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: BamReader::get_file_endian
111 // Access: Published
112 // Description: Returns the endian preference indicated by the Bam
113 // file currently being read. This does not imply that
114 // every number is stored using the indicated
115 // convention, but individual objects may choose to
116 // respect this flag when recording data.
117 ////////////////////////////////////////////////////////////////////
118 INLINE BamReader::BamEndian BamReader::
120  return _file_endian;
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: BamReader::get_file_stdfloat_double
125 // Access: Published
126 // Description: Returns true if the file stores all "standard"
127 // floats as 64-bit doubles, or false if they are 32-bit
128 // floats. This is determined by the compilation flags
129 // of the version of Panda that generated this file.
130 ////////////////////////////////////////////////////////////////////
131 INLINE bool BamReader::
133  return _file_stdfloat_double;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: BamReader::get_current_major_ver
138 // Access: Published
139 // Description: Returns the major version number of Bam files
140 // supported by the current code base. This must match
141 // get_file_major_ver() in order to successfully read a
142 // file.
143 ////////////////////////////////////////////////////////////////////
144 INLINE int BamReader::
146  return _cur_major;
147 }
148 
149 ////////////////////////////////////////////////////////////////////
150 // Function: BamReader::get_current_minor_ver
151 // Access: Published
152 // Description: Returns the minor version number of Bam files
153 // supported by the current code base. This must match
154 // or exceed get_file_minor_ver() in order to
155 // successfully read a file.
156 ////////////////////////////////////////////////////////////////////
157 INLINE int BamReader::
159  return _cur_minor;
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function: BamReader::get_file
164 // Access: Public
165 // Description: Returns the FileReference that provides the source for
166 // these datagrams, if any, or NULL if the datagrams do
167 // not originate from a file on disk.
168 ////////////////////////////////////////////////////////////////////
169 INLINE const FileReference *BamReader::
171  nassertr(_source != NULL, NULL);
172  return _source->get_file();
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: BamReader::get_vfile
177 // Access: Public
178 // Description: Returns the VirtualFile that provides the source for
179 // these datagrams, if any, or NULL if the datagrams do
180 // not originate from a VirtualFile.
181 ////////////////////////////////////////////////////////////////////
182 INLINE VirtualFile *BamReader::
184  nassertr(_source != NULL, NULL);
185  return _source->get_vfile();
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: BamReader::get_file_pos
190 // Access: Public
191 // Description: Returns the current file position within the data
192 // stream, if any, or 0 if the file position is not
193 // meaningful or cannot be determined.
194 //
195 // For BamReaders that return a meaningful file
196 // position, this will be pointing to the first byte
197 // following the datagram returned after a call to
198 // get_datagram().
199 ////////////////////////////////////////////////////////////////////
200 INLINE streampos BamReader::
202  nassertr(_source != NULL, 0);
203  return _source->get_file_pos();
204 }
205 
206 ////////////////////////////////////////////////////////////////////
207 // Function: BamReader::get_factory
208 // Access: Public, Static
209 // Description: Returns the global WritableFactory for generating
210 // TypedWritable objects
211 ////////////////////////////////////////////////////////////////////
214  if (_factory == (WritableFactory *)NULL) {
215  create_factory();
216  }
217  return _factory;
218 }
219 
220 ////////////////////////////////////////////////////////////////////
221 // Function: BamReader::create_factory
222 // Access: Private, Static
223 // Description: Creates a new WritableFactory for generating
224 // TypedWritable objects
225 ////////////////////////////////////////////////////////////////////
226 INLINE void BamReader::
227 create_factory() {
228  _factory = new WritableFactory;
229 }
230 
231 ////////////////////////////////////////////////////////////////////
232 // Function: BamReader::get_datagram
233 // Access: Private
234 // Description: Reads a single datagram from the stream. Returns
235 // true on success, false on failure.
236 ////////////////////////////////////////////////////////////////////
237 INLINE bool BamReader::
238 get_datagram(Datagram &datagram) {
239  nassertr(_source != NULL, false);
240  if (_source->is_error()) {
241  return false;
242  }
243 
244  if (!_source->get_datagram(datagram)) {
245  return false;
246  }
247 
248  datagram.set_stdfloat_double(_file_stdfloat_double);
249  return true;
250 }
251 
252 ////////////////////////////////////////////////////////////////////
253 // Function: BamReader::AuxData::Constructor
254 // Access: Public
255 // Description:
256 ////////////////////////////////////////////////////////////////////
257 INLINE BamReader::AuxData::
258 AuxData() {
259 }
260 
261 ////////////////////////////////////////////////////////////////////
262 // Function: BamReader::CreatedObj::Constructor
263 // Access: Public
264 // Description:
265 ////////////////////////////////////////////////////////////////////
266 INLINE BamReader::CreatedObj::
267 CreatedObj() :
268  _created(false),
269  _ptr(NULL),
270  _ref_ptr(NULL),
271  _change_this(NULL),
272  _change_this_ref(NULL)
273 {
274 }
275 
276 ////////////////////////////////////////////////////////////////////
277 // Function: BamReader::CreatedObj::Destructor
278 // Access: Public
279 // Description:
280 ////////////////////////////////////////////////////////////////////
281 INLINE BamReader::CreatedObj::
282 ~CreatedObj() {
283  set_ptr(NULL, NULL);
284 }
285 
286 ////////////////////////////////////////////////////////////////////
287 // Function: BamReader::CreatedObj::set_ptr
288 // Access: Public
289 // Description: Replaces the pointer to the created object. There
290 // are actually two pointers to the same object in
291 // different forms: a generic TypedWritable pointer, and
292 // an untyped ReferenceCount pointer. We need both
293 // pointers because some objects (like PandaNode)
294 // inherit from TypedWritable and ReferenceCount
295 // independently.
296 //
297 // Managing a typed pointer and an untyped
298 // ReferenceCount pointer to the same object takes just
299 // a bit of extra care.
300 ////////////////////////////////////////////////////////////////////
301 INLINE void BamReader::CreatedObj::
302 set_ptr(TypedWritable *ptr, ReferenceCount *ref_ptr) {
303  if (_ptr != ptr) {
304  if (_ref_ptr != NULL) {
305  nassertv(_ref_ptr != ref_ptr);
306  unref_delete(_ref_ptr);
307  }
308 
309  _ptr = ptr;
310  _ref_ptr = ref_ptr;
311 
312  if (_ref_ptr != NULL) {
313  _ref_ptr->ref();
314  }
315  } else {
316  nassertv(_ref_ptr == ref_ptr);
317  }
318 }
319 
320 ////////////////////////////////////////////////////////////////////
321 // Function: parse_params
322 // Access: Private, Static
323 // Description: Takes in a FactoryParams, passed from a WritableFactory
324 // into any TypedWritable's make function, and parses
325 // out the datagram that contatins the data it needs to
326 // construct itself, and parses out the pointer to the
327 // managing BamReader object
328 ////////////////////////////////////////////////////////////////////
329 INLINE void
330 parse_params(const FactoryParams &params,
331  DatagramIterator &scan, BamReader *&manager) {
332  BamReaderParam *param = DCAST(BamReaderParam, params.get_param(0));
333 
334  scan = param->get_iterator();
335  manager = param->get_manager();
336 }
337 
Keeps a reference-counted pointer to a file on disk.
Definition: fileReference.h:29
A Factory can be used to create an instance of a particular subclass of some general base class...
Definition: factory.h:41
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:26
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
BamEndian get_file_endian() const
Returns the endian preference indicated by the Bam file currently being read.
Definition: bamReader.I:119
streampos get_file_pos()
Returns the current file position within the data stream, if any, or 0 if the file position is not me...
Definition: bamReader.I:201
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:37
FactoryParam * get_param(int n) const
Returns the nth parameter that has been added to the set.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:105
int get_file_major_ver() const
Returns the major version number of the Bam file currently being read.
Definition: bamReader.I:94
void set_stdfloat_double(bool stdfloat_double)
Changes the stdfloat_double flag, which defines the operation performed by add_stdfloat() and Datagra...
Definition: datagram.I:523
void set_loader_options(const LoaderOptions &options)
Specifies the LoaderOptions for this BamReader.
Definition: bamReader.I:70
bool get_file_stdfloat_double() const
Returns true if the file stores all "standard" floats as 64-bit doubles, or false if they are 32-bit ...
Definition: bamReader.I:132
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
const LoaderOptions & get_loader_options() const
Returns the LoaderOptions passed to the loader when the model was requested, if any.
Definition: bamReader.I:60
const FileReference * get_file()
Returns the FileReference that provides the source for these datagrams, if any, or NULL if the datagr...
Definition: bamReader.I:170
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
DatagramGenerator * get_source()
Returns the current source of the BamReader as set by set_source() or the constructor.
Definition: bamReader.I:32
void ref() const
Explicitly increments the reference count.
int get_current_minor_ver() const
Returns the minor version number of Bam files supported by the current code base. ...
Definition: bamReader.I:158
A base class for all things that want to be reference-counted.
VirtualFile * get_vfile()
Returns the VirtualFile that provides the source for these datagrams, if any, or NULL if the datagram...
Definition: bamReader.I:183
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
A class to retrieve the individual data elements previously stored in a Datagram. ...
The parameters that are passed through the Factory to any object constructing itself from a Bam file...
This class defines the abstract interace to any source of datagrams, whether it be from a file or fro...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
const Filename & get_filename() const
If a BAM is a file, then the BamReader should contain the name of the file.
Definition: bamReader.I:45
bool is_eof() const
Returns true if the reader has reached end-of-file, false otherwise.
Definition: bamReader.I:82
int get_current_major_ver() const
Returns the major version number of Bam files supported by the current code base. ...
Definition: bamReader.I:145