Panda3D
 All Classes Functions Variables Enumerations
sliderTable.cxx
1 // Filename: sliderTable.cxx
2 // Created by: drose (28Mar05)
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 "sliderTable.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 #include "vertexTransform.h"
19 
20 SparseArray SliderTable::_empty_array;
21 TypeHandle SliderTable::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: SliderTable::Constructor
25 // Access: Published
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 SliderTable::
29 SliderTable() :
30  _is_registered(false)
31 {
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: SliderTable::Copy Constructor
36 // Access: Published
37 // Description:
38 ////////////////////////////////////////////////////////////////////
39 SliderTable::
40 SliderTable(const SliderTable &copy) :
41  _is_registered(false),
42  _sliders(copy._sliders),
43  _sliders_by_name(copy._sliders_by_name)
44 {
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: SliderTable::Copy Assignment Operator
49 // Access: Published
50 // Description:
51 ////////////////////////////////////////////////////////////////////
52 void SliderTable::
53 operator = (const SliderTable &copy) {
54  nassertv(!_is_registered);
55  _sliders = copy._sliders;
56  _sliders_by_name = copy._sliders_by_name;
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: SliderTable::Destructor
61 // Access: Published, Virtual
62 // Description:
63 ////////////////////////////////////////////////////////////////////
64 SliderTable::
65 ~SliderTable() {
66  if (_is_registered) {
67  do_unregister();
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: SliderTable::set_slider
73 // Access: Published
74 // Description: Replaces the nth slider. Only valid for
75 // unregistered tables.
76 ////////////////////////////////////////////////////////////////////
77 void SliderTable::
78 set_slider(int n, const VertexSlider *slider) {
79  nassertv(!_is_registered);
80  nassertv(n >= 0 && n < (int)_sliders.size());
81 
82  if (_sliders[n]._slider->get_name() != slider->get_name()) {
83  _sliders_by_name[_sliders[n]._slider->get_name()].clear_bit(n);
84  _sliders_by_name[slider->get_name()].set_bit(n);
85  }
86 
87  _sliders[n]._slider = slider;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: SliderTable::set_slider_rows
92 // Access: Published
93 // Description: Replaces the rows affected by the nth slider. Only
94 // valid for unregistered tables.
95 ////////////////////////////////////////////////////////////////////
96 void SliderTable::
97 set_slider_rows(int n, const SparseArray &rows) {
98  // We don't actually enforce the registration requirement, since
99  // gee, it doesn't actually matter here; and the GeomVertexData
100  // needs to be able to change the SparseArrays in the bam reader.
101  // nassertv(!_is_registered);
102  nassertv(n >= 0 && n < (int)_sliders.size());
103 
104  _sliders[n]._rows = rows;
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: SliderTable::remove_slider
109 // Access: Published
110 // Description: Removes the nth slider. Only valid for
111 // unregistered tables.
112 ////////////////////////////////////////////////////////////////////
113 void SliderTable::
115  nassertv(!_is_registered);
116  nassertv(n >= 0 && n < (int)_sliders.size());
117 
118  _sliders_by_name[_sliders[n]._slider->get_name()].clear_bit(n);
119  _sliders.erase(_sliders.begin() + n);
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: SliderTable::add_slider
124 // Access: Published
125 // Description: Adds a new slider to the table, and returns the
126 // index number of the new slider. Only valid for
127 // unregistered tables.
128 ////////////////////////////////////////////////////////////////////
129 int SliderTable::
130 add_slider(const VertexSlider *slider, const SparseArray &rows) {
131  nassertr(!_is_registered, -1);
132 
133  int new_index = (int)_sliders.size();
134 
135  SliderDef slider_def;
136  slider_def._slider = slider;
137  slider_def._rows = rows;
138  _sliders.push_back(slider_def);
139  _sliders_by_name[slider->get_name()].set_bit(new_index);
140 
141  return new_index;
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: SliderTable::write
146 // Access: Published
147 // Description:
148 ////////////////////////////////////////////////////////////////////
149 void SliderTable::
150 write(ostream &out) const {
151  for (size_t i = 0; i < _sliders.size(); ++i) {
152  out << i << ". " << *_sliders[i]._slider << " "
153  << _sliders[i]._rows << "\n";
154  }
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: SliderTable::do_register
159 // Access: Private
160 // Description: Called internally when the table is registered.
161 ////////////////////////////////////////////////////////////////////
162 void SliderTable::
163 do_register() {
164  nassertv(!_is_registered);
165 
166  Sliders::iterator si;
167  for (si = _sliders.begin(); si != _sliders.end(); ++si) {
168  const VertexSlider *slider = (*si)._slider;
169  bool inserted = ((VertexSlider *)slider)->_tables.insert(this).second;
170  nassertv(inserted);
171  }
172  _is_registered = true;
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: SliderTable::do_unregister
177 // Access: Private
178 // Description: Called internally when the table is unregistered
179 // (i.e. right before destruction).
180 ////////////////////////////////////////////////////////////////////
181 void SliderTable::
182 do_unregister() {
183  nassertv(_is_registered);
184 
185  Sliders::iterator si;
186  for (si = _sliders.begin(); si != _sliders.end(); ++si) {
187  const VertexSlider *slider = (*si)._slider;
188  ((VertexSlider *)slider)->_tables.erase(this);
189  }
190  _is_registered = false;
191 }
192 
193 ////////////////////////////////////////////////////////////////////
194 // Function: SliderTable::register_with_read_factory
195 // Access: Public, Static
196 // Description: Tells the BamReader how to create objects of type
197 // SliderTable.
198 ////////////////////////////////////////////////////////////////////
199 void SliderTable::
201  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
202 }
203 
204 ////////////////////////////////////////////////////////////////////
205 // Function: SliderTable::write_datagram
206 // Access: Public, Virtual
207 // Description: Writes the contents of this object to the datagram
208 // for shipping out to a Bam file.
209 ////////////////////////////////////////////////////////////////////
210 void SliderTable::
212  TypedWritable::write_datagram(manager, dg);
213 
214  dg.add_uint16(_sliders.size());
215  Sliders::const_iterator si;
216  for (si = _sliders.begin(); si != _sliders.end(); ++si) {
217  manager->write_pointer(dg, (*si)._slider->get_name());
218  manager->write_pointer(dg, (*si)._slider);
219  (*si)._rows.write_datagram(manager, dg);
220  }
221 
222  manager->write_cdata(dg, _cycler);
223 }
224 
225 ////////////////////////////////////////////////////////////////////
226 // Function: SliderTable::complete_pointers
227 // Access: Public, Virtual
228 // Description: Receives an array of pointers, one for each time
229 // manager->read_pointer() was called in fillin().
230 // Returns the number of pointers processed.
231 ////////////////////////////////////////////////////////////////////
232 int SliderTable::
234  int pi = TypedWritableReferenceCount::complete_pointers(p_list, manager);
235 
236  for (size_t n = 0; n < _sliders.size(); ++n) {
237  CPT(InternalName) name = DCAST(InternalName, p_list[pi++]);
238  PT(VertexSlider) slider = DCAST(VertexSlider, p_list[pi++]);
239 
240  _sliders[n]._slider = slider;
241  _sliders_by_name[name].set_bit(n);
242  }
243 
244  return pi;
245 }
246 
247 ////////////////////////////////////////////////////////////////////
248 // Function: SliderTable::make_from_bam
249 // Access: Protected, Static
250 // Description: This function is called by the BamReader's factory
251 // when a new object of type SliderTable is encountered
252 // in the Bam file. It should create the SliderTable
253 // and extract its information from the file.
254 ////////////////////////////////////////////////////////////////////
255 TypedWritable *SliderTable::
256 make_from_bam(const FactoryParams &params) {
257  SliderTable *object = new SliderTable;
258  DatagramIterator scan;
259  BamReader *manager;
260 
261  parse_params(params, scan, manager);
262  object->fillin(scan, manager);
263 
264  return object;
265 }
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function: SliderTable::fillin
269 // Access: Protected
270 // Description: This internal function is called by make_from_bam to
271 // read in all of the relevant data from the BamFile for
272 // the new SliderTable.
273 ////////////////////////////////////////////////////////////////////
274 void SliderTable::
275 fillin(DatagramIterator &scan, BamReader *manager) {
276  TypedWritable::fillin(scan, manager);
277 
278  size_t num_sliders = scan.get_uint16();
279  _sliders.reserve(num_sliders);
280  for (size_t i = 0; i < num_sliders; ++i) {
281  manager->read_pointer(scan);
282  manager->read_pointer(scan);
283  _sliders.push_back(SliderDef());
284  if (manager->get_file_minor_ver() >= 7) {
285  _sliders[i]._rows.read_datagram(scan, manager);
286  } else {
287  // In this case, for bam files prior to 6.7, we must define the
288  // SparseArray with the full number of vertices. This is done
289  // in GeomVertexData::complete_pointers().
290  }
291  }
292 
293  manager->read_cdata(scan, _cycler);
294 }
295 
296 ////////////////////////////////////////////////////////////////////
297 // Function: SliderTable::CData::make_copy
298 // Access: Public, Virtual
299 // Description:
300 ////////////////////////////////////////////////////////////////////
301 CycleData *SliderTable::CData::
302 make_copy() const {
303  return new CData(*this);
304 }
305 
306 ////////////////////////////////////////////////////////////////////
307 // Function: SliderTable::CData::write_datagram
308 // Access: Public, Virtual
309 // Description: Writes the contents of this object to the datagram
310 // for shipping out to a Bam file.
311 ////////////////////////////////////////////////////////////////////
312 void SliderTable::CData::
313 write_datagram(BamWriter *manager, Datagram &dg) const {
314 }
315 
316 ////////////////////////////////////////////////////////////////////
317 // Function: SliderTable::CData::fillin
318 // Access: Public, Virtual
319 // Description: This internal function is called by make_from_bam to
320 // read in all of the relevant data from the BamFile for
321 // the new SliderTable.
322 ////////////////////////////////////////////////////////////////////
323 void SliderTable::CData::
324 fillin(DatagramIterator &scan, BamReader *manager) {
325  Thread *current_thread = Thread::get_current_thread();
326  _modified = VertexTransform::get_next_modified(current_thread);
327 }
This class records a set of integers, where each integer is either present or not present in the set...
Definition: sparseArray.h:49
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:747
void set_slider(int n, const VertexSlider *slider)
Replaces the nth slider.
Definition: sliderTable.cxx:78
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 an abstract base class that retains some slider value, which is a linear value that typically...
Definition: vertexSlider.h:41
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager-&gt;read_pointer() was called in fillin()...
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.
Stores the total set of VertexSliders that the vertices in a particular GeomVertexData object might d...
Definition: sliderTable.h:42
void remove_slider(int n)
Removes the nth slider.
void set_slider_rows(int n, const SparseArray &rows)
Replaces the rows affected by the nth slider.
Definition: sliderTable.cxx:97
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager-&gt;read_pointer() was called in fillin()...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
int add_slider(const VertexSlider *slider, const SparseArray &rows)
Adds a new slider to the table, and returns the index number of the new slider.
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
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type SliderTable.
A class to retrieve the individual data elements previously stored in a Datagram. ...
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:105
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
const InternalName * get_name() const
Returns the name of this particular slider.
Definition: vertexSlider.I:25
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:652