Panda3D
bamReader.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 bamReader.I
10  * @author jason
11  * @date 2000-06-12
12  */
13 
14 /**
15  *
16  */
17 INLINE BamReaderAuxData::
18 BamReaderAuxData() {
19 }
20 
21 /**
22  * Returns the current source of the BamReader as set by set_source() or the
23  * constructor.
24  */
25 INLINE DatagramGenerator *BamReader::
26 get_source() {
27  return _source;
28 }
29 
30 /**
31  * If a BAM is a file, then the BamReader should contain the name of the file.
32  * This enables the reader to interpret pathnames in the BAM as relative to
33  * the directory containing the BAM.
34  */
35 INLINE const Filename &BamReader::
36 get_filename() const {
37  if (_source != nullptr) {
38  return _source->get_filename();
39  }
40  static const Filename empty_filename;
41  return empty_filename;
42 }
43 
44 /**
45  * Returns the LoaderOptions passed to the loader when the model was
46  * requested, if any.
47  */
48 INLINE const LoaderOptions &BamReader::
49 get_loader_options() const {
50  return _loader_options;
51 }
52 
53 /**
54  * Specifies the LoaderOptions for this BamReader.
55  */
56 INLINE void BamReader::
57 set_loader_options(const LoaderOptions &options) {
58  _loader_options = options;
59 }
60 
61 /**
62  * Returns true if the reader has reached end-of-file, false otherwise. This
63  * call is only valid after a call to read_object().
64  */
65 INLINE bool BamReader::
66 is_eof() const {
67  nassertr(_source != nullptr, true);
68  return _source->is_eof();
69 }
70 
71 /**
72  * Returns the major version number of the Bam file currently being read.
73  */
74 INLINE int BamReader::
76  return _file_major;
77 }
78 
79 /**
80  * Returns the minor version number of the Bam file currently being read.
81  */
82 INLINE int BamReader::
84  return _file_minor;
85 }
86 
87 /**
88  * Returns the endian preference indicated by the Bam file currently being
89  * read. This does not imply that every number is stored using the indicated
90  * convention, but individual objects may choose to respect this flag when
91  * recording data.
92  */
93 INLINE BamReader::BamEndian BamReader::
94 get_file_endian() const {
95  return _file_endian;
96 }
97 
98 /**
99  * Returns true if the file stores all "standard" floats as 64-bit doubles, or
100  * false if they are 32-bit floats. This is determined by the compilation
101  * flags of the version of Panda that generated this file.
102  */
103 INLINE bool BamReader::
104 get_file_stdfloat_double() const {
105  return _file_stdfloat_double;
106 }
107 
108 /**
109  * Returns the major version number of Bam files supported by the current code
110  * base. This must match get_file_major_ver() in order to successfully read a
111  * file.
112  */
113 INLINE int BamReader::
115  return _cur_major;
116 }
117 
118 /**
119  * Returns the minor version number of Bam files supported by the current code
120  * base. This must match or exceed get_file_minor_ver() in order to
121  * successfully read a file.
122  */
123 INLINE int BamReader::
125  return _cur_minor;
126 }
127 
128 /**
129  * Returns the FileReference that provides the source for these datagrams, if
130  * any, or NULL if the datagrams do not originate from a file on disk.
131  */
132 INLINE const FileReference *BamReader::
134  nassertr(_source != nullptr, nullptr);
135  return _source->get_file();
136 }
137 
138 /**
139  * Returns the VirtualFile that provides the source for these datagrams, if
140  * any, or NULL if the datagrams do not originate from a VirtualFile.
141  */
142 INLINE VirtualFile *BamReader::
144  nassertr(_source != nullptr, nullptr);
145  return _source->get_vfile();
146 }
147 
148 /**
149  * Returns the current file position within the data stream, if any, or 0 if
150  * the file position is not meaningful or cannot be determined.
151  *
152  * For BamReaders that return a meaningful file position, this will be
153  * pointing to the first byte following the datagram returned after a call to
154  * get_datagram().
155  */
156 INLINE std::streampos BamReader::
158  nassertr(_source != nullptr, 0);
159  return _source->get_file_pos();
160 }
161 
162 /**
163  * Registers a factory function that is called when an object of the given
164  * type is encountered within the .bam stream.
165  *
166  * @param user_data an optional pointer to be passed along to the function.
167  */
168 void BamReader::
169 register_factory(TypeHandle handle, WritableFactory::CreateFunc *func, void *user_data) {
170  get_factory()->register_factory(handle, func, user_data);
171 }
172 
173 /**
174  * Returns the global WritableFactory for generating TypedWritable objects
175  */
178  if (_factory == nullptr) {
179  create_factory();
180  }
181  return _factory;
182 }
183 
184 /**
185  * Creates a new WritableFactory for generating TypedWritable objects
186  */
187 INLINE void BamReader::
188 create_factory() {
189  _factory = new WritableFactory;
190 }
191 
192 /**
193  * Reads a single datagram from the stream. Returns true on success, false on
194  * failure.
195  */
196 INLINE bool BamReader::
197 get_datagram(Datagram &datagram) {
198  nassertr(_source != nullptr, false);
199  if (_source->is_error()) {
200  return false;
201  }
202 
203  if (!_source->get_datagram(datagram)) {
204  return false;
205  }
206 
207  datagram.set_stdfloat_double(_file_stdfloat_double);
208  return true;
209 }
210 
211 /**
212  *
213  */
214 INLINE BamReader::AuxData::
215 AuxData() {
216 }
217 
218 /**
219  *
220  */
221 INLINE BamReader::CreatedObj::
222 CreatedObj() :
223  _created(false),
224  _ptr(nullptr),
225  _ref_ptr(nullptr),
226  _change_this(nullptr),
227  _change_this_ref(nullptr)
228 {
229 }
230 
231 /**
232  *
233  */
234 INLINE BamReader::CreatedObj::
235 ~CreatedObj() {
236  set_ptr(nullptr, nullptr);
237 }
238 
239 /**
240  * Replaces the pointer to the created object. There are actually two
241  * pointers to the same object in different forms: a generic TypedWritable
242  * pointer, and an untyped ReferenceCount pointer. We need both pointers
243  * because some objects (like PandaNode) inherit from TypedWritable and
244  * ReferenceCount independently.
245  *
246  * Managing a typed pointer and an untyped ReferenceCount pointer to the same
247  * object takes just a bit of extra care.
248  */
249 INLINE void BamReader::CreatedObj::
250 set_ptr(TypedWritable *ptr, ReferenceCount *ref_ptr) {
251  if (_ptr != ptr) {
252  if (_ref_ptr != nullptr) {
253  nassertv(_ref_ptr != ref_ptr);
254  unref_delete(_ref_ptr);
255  }
256 
257  _ptr = ptr;
258  _ref_ptr = ref_ptr;
259 
260  if (_ref_ptr != nullptr) {
261  _ref_ptr->ref();
262  }
263  } else {
264  nassertv(_ref_ptr == ref_ptr);
265  }
266 }
267 
268 /**
269  * Takes in a FactoryParams, passed from a WritableFactory into any
270  * TypedWritable's make function, and parses out the datagram that contatins
271  * the data it needs to construct itself, and parses out the pointer to the
272  * managing BamReader object
273  */
274 INLINE void
276  DatagramIterator &scan, BamReader *&manager) {
277  BamReaderParam *param = DCAST(BamReaderParam, params.get_param(0));
278 
279  scan = param->get_iterator();
280  manager = param->get_manager();
281 }
Keeps a reference-counted pointer to a file on disk.
Definition: fileReference.h:26
A Factory can be used to create an instance of a particular subclass of some general base class.
Definition: factory.h:34
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:23
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:35
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:83
set_loader_options
Specifies the LoaderOptions for this BamReader.
Definition: bamReader.h:156
int get_file_major_ver() const
Returns the major version number of the Bam file currently being read.
Definition: bamReader.I:75
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:395
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
virtual const FileReference * get_file()
Returns the FileReference that provides the source for these datagrams, if any, or NULL if the datagr...
const FileReference * get_file()
Returns the FileReference that provides the source for these datagrams, if any, or NULL if the datagr...
Definition: bamReader.I:133
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
virtual VirtualFile * get_vfile()
Returns the VirtualFile that provides the source for these datagrams, if any, or NULL if the datagram...
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:124
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:143
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
static void register_factory(TypeHandle type, WritableFactory::CreateFunc *func, void *user_data=nullptr)
Registers a factory function that is called when an object of the given type is encountered within th...
Definition: bamReader.I:169
virtual std::streampos get_file_pos()
Returns the current file position within the data stream, if any, or 0 if the file position is not me...
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
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:38
void unref_delete(RefCountType *ptr)
This global helper function will unref the given ReferenceCount object, and if the reference count re...
virtual const Filename & get_filename()
Returns the filename that provides the source for these datagrams, if any, or empty string if the dat...
bool is_eof() const
Returns true if the reader has reached end-of-file, false otherwise.
Definition: bamReader.I:66
int get_current_major_ver() const
Returns the major version number of Bam files supported by the current code base.
Definition: bamReader.I:114
std::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:157