Panda3D
transformTable.cxx
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 transformTable.cxx
10  * @author drose
11  * @date 2005-03-23
12  */
13 
14 #include "transformTable.h"
15 #include "bamReader.h"
16 #include "bamWriter.h"
17 
18 TypeHandle TransformTable::_type_handle;
19 
20 /**
21  *
22  */
23 TransformTable::
24 TransformTable() :
25  _is_registered(false)
26 {
27 }
28 
29 /**
30  *
31  */
32 TransformTable::
33 TransformTable(const TransformTable &copy) :
34  _is_registered(false),
35  _transforms(copy._transforms)
36 {
37 }
38 
39 /**
40  *
41  */
42 void TransformTable::
43 operator = (const TransformTable &copy) {
44  nassertv(!_is_registered);
45  _transforms = copy._transforms;
46 }
47 
48 /**
49  *
50  */
51 TransformTable::
52 ~TransformTable() {
53  if (_is_registered) {
54  do_unregister();
55  }
56 }
57 
58 /**
59  * Replaces the nth transform. Only valid for unregistered tables.
60  */
62 set_transform(size_t n, const VertexTransform *transform) {
63  nassertv(!_is_registered);
64  nassertv(n < _transforms.size());
65  _transforms[n] = transform;
66 }
67 
68 /**
69  * Inserts a new transform to the table at the given index position. If the
70  * index is beyond the end of the table, appends it to the end. Only valid
71  * for unregistered tables.
72  *
73  * This does not automatically uniquify the pointer; if the transform is
74  * already present in the table, it will be added twice.
75  */
77 insert_transform(size_t n, const VertexTransform *transform) {
78  nassertv(!_is_registered);
79  if (n > _transforms.size()) {
80  n = _transforms.size();
81  }
82  _transforms.insert(_transforms.begin() + n, transform);
83 }
84 
85 /**
86  * Removes the nth transform. Only valid for unregistered tables.
87  */
89 remove_transform(size_t n) {
90  nassertv(!_is_registered);
91  nassertv(n < _transforms.size());
92  _transforms.erase(_transforms.begin() + n);
93 }
94 
95 /**
96  * Adds a new transform to the table and returns the index number of the new
97  * transform. Only valid for unregistered tables.
98  *
99  * This does not automatically uniquify the pointer; if the transform is
100  * already present in the table, it will be added twice.
101  */
102 size_t TransformTable::
103 add_transform(const VertexTransform *transform) {
104  nassertr(!_is_registered, -1);
105  size_t new_index = _transforms.size();
106  _transforms.push_back(transform);
107  return new_index;
108 }
109 
110 /**
111  *
112  */
113 void TransformTable::
114 write(std::ostream &out) const {
115  for (size_t i = 0; i < _transforms.size(); ++i) {
116  out << i << ". " << *_transforms[i] << "\n";
117  }
118 }
119 
120 /**
121  * Called internally when the table is registered.
122  */
123 void TransformTable::
124 do_register() {
125  nassertv(!_is_registered);
126 
127  Transforms::iterator ti;
128  for (ti = _transforms.begin(); ti != _transforms.end(); ++ti) {
129  VertexTransform *transform = (VertexTransform *)(*ti).p();
130  bool inserted = transform->_tables.insert(this).second;
131  nassertv(inserted);
132  }
133  _is_registered = true;
134 }
135 
136 /**
137  * Called internally when the table is unregistered (i.e. right before
138  * destruction).
139  */
140 void TransformTable::
141 do_unregister() {
142  nassertv(_is_registered);
143 
144  Transforms::iterator ti;
145  for (ti = _transforms.begin(); ti != _transforms.end(); ++ti) {
146  VertexTransform *transform = (VertexTransform *)(*ti).p();
147  transform->_tables.erase(this);
148  }
149  _is_registered = false;
150 }
151 
152 /**
153  * Tells the BamReader how to create objects of type TransformTable.
154  */
155 void TransformTable::
157  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
158 }
159 
160 /**
161  * Writes the contents of this object to the datagram for shipping out to a
162  * Bam file.
163  */
164 void TransformTable::
167 
168  dg.add_uint16(_transforms.size());
169  for (Transforms::const_iterator ti = _transforms.begin();
170  ti != _transforms.end();
171  ++ti) {
172  manager->write_pointer(dg, *ti);
173  }
174 
175  manager->write_cdata(dg, _cycler);
176 }
177 
178 /**
179  * Receives an array of pointers, one for each time manager->read_pointer()
180  * was called in fillin(). Returns the number of pointers processed.
181  */
184  int pi = TypedWritableReferenceCount::complete_pointers(p_list, manager);
185 
186  for (Transforms::iterator ti = _transforms.begin();
187  ti != _transforms.end();
188  ++ti) {
189  (*ti) = DCAST(VertexTransform, p_list[pi++]);
190  }
191 
192  return pi;
193 }
194 
195 /**
196  * This function is called by the BamReader's factory when a new object of
197  * type TransformTable is encountered in the Bam file. It should create the
198  * TransformTable and extract its information from the file.
199  */
200 TypedWritable *TransformTable::
201 make_from_bam(const FactoryParams &params) {
202  TransformTable *object = new TransformTable;
203  DatagramIterator scan;
204  BamReader *manager;
205 
206  parse_params(params, scan, manager);
207  object->fillin(scan, manager);
208 
209  return object;
210 }
211 
212 /**
213  * This internal function is called by make_from_bam to read in all of the
214  * relevant data from the BamFile for the new TransformTable.
215  */
216 void TransformTable::
217 fillin(DatagramIterator &scan, BamReader *manager) {
219 
220  size_t num_transforms = scan.get_uint16();
221  _transforms.reserve(num_transforms);
222  for (size_t i = 0; i < num_transforms; ++i) {
223  manager->read_pointer(scan);
224  _transforms.push_back(nullptr);
225  }
226 
227  manager->read_cdata(scan, _cycler);
228 }
229 
230 /**
231  *
232  */
233 CycleData *TransformTable::CData::
234 make_copy() const {
235  return new CData(*this);
236 }
237 
238 /**
239  * Writes the contents of this object to the datagram for shipping out to a
240  * Bam file.
241  */
242 void TransformTable::CData::
243 write_datagram(BamWriter *manager, Datagram &dg) const {
244 }
245 
246 /**
247  * This internal function is called by make_from_bam to read in all of the
248  * relevant data from the BamFile for the new TransformTable.
249  */
250 void TransformTable::CData::
251 fillin(DatagramIterator &scan, BamReader *manager) {
252  Thread *current_thread = Thread::get_current_thread();
253  _modified = VertexTransform::get_next_modified(current_thread);
254 }
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:695
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:47
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:425
static UpdateSeq get_next_modified(Thread *current_thread)
Returns a monotonically increasing sequence.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static void register_with_read_factory()
Tells the BamReader how to create objects of type TransformTable.
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
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition: thread.h:109
set_transform
Replaces the nth transform.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
This is an abstract base class that holds a pointer to some transform, computed in some arbitrary way...
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Stores the total set of VertexTransforms that the vertices in a particular GeomVertexData object migh...
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
A thread; that is, a lightweight process.
Definition: thread.h:46
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
insert_transform
Inserts a new transform to the table at the given index position.
A class to retrieve the individual data elements previously stored in a Datagram.
remove_transform
Removes the nth transform.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317
size_t add_transform(const VertexTransform *transform)
Adds a new transform to the table and returns the index number of the new transform.