Panda3D
transformTable.cxx
1 // Filename: transformTable.cxx
2 // Created by: drose (23Mar05)
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 #include "transformTable.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 
19 TypeHandle TransformTable::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: TransformTable::Constructor
23 // Access: Published
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 TransformTable::
27 TransformTable() :
28  _is_registered(false)
29 {
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: TransformTable::Copy Constructor
34 // Access: Published
35 // Description:
36 ////////////////////////////////////////////////////////////////////
37 TransformTable::
38 TransformTable(const TransformTable &copy) :
39  _is_registered(false),
40  _transforms(copy._transforms)
41 {
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: TransformTable::Copy Assignment Operator
46 // Access: Published
47 // Description:
48 ////////////////////////////////////////////////////////////////////
49 void TransformTable::
50 operator = (const TransformTable &copy) {
51  nassertv(!_is_registered);
52  _transforms = copy._transforms;
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: TransformTable::Destructor
57 // Access: Published, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 TransformTable::
61 ~TransformTable() {
62  if (_is_registered) {
63  do_unregister();
64  }
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: TransformTable::set_transform
69 // Access: Published
70 // Description: Replaces the nth transform. Only valid for
71 // unregistered tables.
72 ////////////////////////////////////////////////////////////////////
74 set_transform(int n, const VertexTransform *transform) {
75  nassertv(!_is_registered);
76  nassertv(n >= 0 && n < (int)_transforms.size());
77  _transforms[n] = transform;
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: TransformTable::remove_transform
82 // Access: Published
83 // Description: Removes the nth transform. Only valid for
84 // unregistered tables.
85 ////////////////////////////////////////////////////////////////////
88  nassertv(!_is_registered);
89  nassertv(n >= 0 && n < (int)_transforms.size());
90  _transforms.erase(_transforms.begin() + n);
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: TransformTable::add_transform
95 // Access: Published
96 // Description: Adds a new transform to the table and returns the
97 // index number of the new transform. Only valid for
98 // unregistered tables.
99 //
100 // This does not automatically uniquify the pointer; if
101 // the transform is already present in the table, it
102 // will be added twice.
103 ////////////////////////////////////////////////////////////////////
105 add_transform(const VertexTransform *transform) {
106  nassertr(!_is_registered, -1);
107  int new_index = (int)_transforms.size();
108  _transforms.push_back(transform);
109  return new_index;
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: TransformTable::write
114 // Access: Published
115 // Description:
116 ////////////////////////////////////////////////////////////////////
117 void TransformTable::
118 write(ostream &out) const {
119  for (size_t i = 0; i < _transforms.size(); ++i) {
120  out << i << ". " << *_transforms[i] << "\n";
121  }
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: TransformTable::do_register
126 // Access: Private
127 // Description: Called internally when the table is registered.
128 ////////////////////////////////////////////////////////////////////
129 void TransformTable::
130 do_register() {
131  nassertv(!_is_registered);
132 
133  Transforms::iterator ti;
134  for (ti = _transforms.begin(); ti != _transforms.end(); ++ti) {
135  VertexTransform *transform = (VertexTransform *)(*ti).p();
136  bool inserted = transform->_tables.insert(this).second;
137  nassertv(inserted);
138  }
139  _is_registered = true;
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: TransformTable::do_unregister
144 // Access: Private
145 // Description: Called internally when the table is unregistered
146 // (i.e. right before destruction).
147 ////////////////////////////////////////////////////////////////////
148 void TransformTable::
149 do_unregister() {
150  nassertv(_is_registered);
151 
152  Transforms::iterator ti;
153  for (ti = _transforms.begin(); ti != _transforms.end(); ++ti) {
154  VertexTransform *transform = (VertexTransform *)(*ti).p();
155  transform->_tables.erase(this);
156  }
157  _is_registered = false;
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: TransformTable::register_with_read_factory
162 // Access: Public, Static
163 // Description: Tells the BamReader how to create objects of type
164 // TransformTable.
165 ////////////////////////////////////////////////////////////////////
166 void TransformTable::
168  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
169 }
170 
171 ////////////////////////////////////////////////////////////////////
172 // Function: TransformTable::write_datagram
173 // Access: Public, Virtual
174 // Description: Writes the contents of this object to the datagram
175 // for shipping out to a Bam file.
176 ////////////////////////////////////////////////////////////////////
177 void TransformTable::
180 
181  dg.add_uint16(_transforms.size());
182  for (Transforms::const_iterator ti = _transforms.begin();
183  ti != _transforms.end();
184  ++ti) {
185  manager->write_pointer(dg, *ti);
186  }
187 
188  manager->write_cdata(dg, _cycler);
189 }
190 
191 ////////////////////////////////////////////////////////////////////
192 // Function: TransformTable::complete_pointers
193 // Access: Public, Virtual
194 // Description: Receives an array of pointers, one for each time
195 // manager->read_pointer() was called in fillin().
196 // Returns the number of pointers processed.
197 ////////////////////////////////////////////////////////////////////
200  int pi = TypedWritableReferenceCount::complete_pointers(p_list, manager);
201 
202  for (Transforms::iterator ti = _transforms.begin();
203  ti != _transforms.end();
204  ++ti) {
205  (*ti) = DCAST(VertexTransform, p_list[pi++]);
206  }
207 
208  return pi;
209 }
210 
211 ////////////////////////////////////////////////////////////////////
212 // Function: TransformTable::make_from_bam
213 // Access: Protected, Static
214 // Description: This function is called by the BamReader's factory
215 // when a new object of type TransformTable is encountered
216 // in the Bam file. It should create the TransformTable
217 // and extract its information from the file.
218 ////////////////////////////////////////////////////////////////////
219 TypedWritable *TransformTable::
220 make_from_bam(const FactoryParams &params) {
221  TransformTable *object = new TransformTable;
222  DatagramIterator scan;
223  BamReader *manager;
224 
225  parse_params(params, scan, manager);
226  object->fillin(scan, manager);
227 
228  return object;
229 }
230 
231 ////////////////////////////////////////////////////////////////////
232 // Function: TransformTable::fillin
233 // Access: Protected
234 // Description: This internal function is called by make_from_bam to
235 // read in all of the relevant data from the BamFile for
236 // the new TransformTable.
237 ////////////////////////////////////////////////////////////////////
238 void TransformTable::
239 fillin(DatagramIterator &scan, BamReader *manager) {
241 
242  size_t num_transforms = scan.get_uint16();
243  _transforms.reserve(num_transforms);
244  for (size_t i = 0; i < num_transforms; ++i) {
245  manager->read_pointer(scan);
246  _transforms.push_back(NULL);
247  }
248 
249  manager->read_cdata(scan, _cycler);
250 }
251 
252 ////////////////////////////////////////////////////////////////////
253 // Function: TransformTable::CData::make_copy
254 // Access: Public, Virtual
255 // Description:
256 ////////////////////////////////////////////////////////////////////
257 CycleData *TransformTable::CData::
258 make_copy() const {
259  return new CData(*this);
260 }
261 
262 ////////////////////////////////////////////////////////////////////
263 // Function: TransformTable::CData::write_datagram
264 // Access: Public, Virtual
265 // Description: Writes the contents of this object to the datagram
266 // for shipping out to a Bam file.
267 ////////////////////////////////////////////////////////////////////
268 void TransformTable::CData::
269 write_datagram(BamWriter *manager, Datagram &dg) const {
270 }
271 
272 ////////////////////////////////////////////////////////////////////
273 // Function: TransformTable::CData::fillin
274 // Access: Public, Virtual
275 // Description: This internal function is called by make_from_bam to
276 // read in all of the relevant data from the BamFile for
277 // the new TransformTable.
278 ////////////////////////////////////////////////////////////////////
279 void TransformTable::CData::
280 fillin(DatagramIterator &scan, BamReader *manager) {
281  Thread *current_thread = Thread::get_current_thread();
282  _modified = VertexTransform::get_next_modified(current_thread);
283 }
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:122
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:753
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:398
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:73
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class&#39;s make_from_bam() method to read in all...
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
Definition: thread.I:145
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.
int add_transform(const VertexTransform *transform)
Adds a new transform to the table and returns the index number of the new 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:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
Stores the total set of VertexTransforms that the vertices in a particular GeomVertexData object migh...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
A thread; that is, a lightweight process.
Definition: thread.h:51
void set_transform(int n, const VertexTransform *transform)
Replaces the nth transform.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram. ...
void remove_transform(int n)
Removes the nth transform.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:279
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:658