Panda3D
sliderTable.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 sliderTable.cxx
10  * @author drose
11  * @date 2005-03-28
12  */
13 
14 #include "sliderTable.h"
15 #include "bamReader.h"
16 #include "bamWriter.h"
17 #include "vertexTransform.h"
18 
19 SparseArray SliderTable::_empty_array;
20 TypeHandle SliderTable::_type_handle;
21 
22 /**
23  *
24  */
25 SliderTable::
26 SliderTable() :
27  _is_registered(false)
28 {
29 }
30 
31 /**
32  *
33  */
34 SliderTable::
35 SliderTable(const SliderTable &copy) :
36  _is_registered(false),
37  _sliders(copy._sliders),
38  _sliders_by_name(copy._sliders_by_name)
39 {
40 }
41 
42 /**
43  *
44  */
45 void SliderTable::
46 operator = (const SliderTable &copy) {
47  nassertv(!_is_registered);
48  _sliders = copy._sliders;
49  _sliders_by_name = copy._sliders_by_name;
50 }
51 
52 /**
53  *
54  */
55 SliderTable::
56 ~SliderTable() {
57  if (_is_registered) {
58  do_unregister();
59  }
60 }
61 
62 /**
63  * Replaces the nth slider. Only valid for unregistered tables.
64  */
65 void SliderTable::
66 set_slider(size_t n, const VertexSlider *slider) {
67  nassertv(!_is_registered);
68  nassertv(n < _sliders.size());
69 
70  if (_sliders[n]._slider->get_name() != slider->get_name()) {
71  _sliders_by_name[_sliders[n]._slider->get_name()].clear_bit(n);
72  _sliders_by_name[slider->get_name()].set_bit(n);
73  }
74 
75  _sliders[n]._slider = slider;
76 }
77 
78 /**
79  * Replaces the rows affected by the nth slider. Only valid for unregistered
80  * tables.
81  */
82 void SliderTable::
83 set_slider_rows(size_t n, const SparseArray &rows) {
84  // We don't actually enforce the registration requirement, since gee, it
85  // doesn't actually matter here; and the GeomVertexData needs to be able to
86  // change the SparseArrays in the bam reader. nassertv(!_is_registered);
87  nassertv(n < _sliders.size());
88 
89  _sliders[n]._rows = rows;
90 }
91 
92 /**
93  * Removes the nth slider. Only valid for unregistered tables.
94  */
95 void SliderTable::
96 remove_slider(size_t n) {
97  nassertv(!_is_registered);
98  nassertv(n < _sliders.size());
99 
100  _sliders_by_name[_sliders[n]._slider->get_name()].clear_bit(n);
101  _sliders.erase(_sliders.begin() + n);
102 }
103 
104 /**
105  * Adds a new slider to the table, and returns the index number of the new
106  * slider. Only valid for unregistered tables.
107  */
108 size_t SliderTable::
109 add_slider(const VertexSlider *slider, const SparseArray &rows) {
110  nassertr(!_is_registered, 0);
111 
112  size_t new_index = _sliders.size();
113 
114  SliderDef slider_def;
115  slider_def._slider = slider;
116  slider_def._rows = rows;
117  _sliders.push_back(slider_def);
118  _sliders_by_name[slider->get_name()].set_bit(new_index);
119 
120  return new_index;
121 }
122 
123 /**
124  *
125  */
126 void SliderTable::
127 write(std::ostream &out) const {
128  for (size_t i = 0; i < _sliders.size(); ++i) {
129  out << i << ". " << *_sliders[i]._slider << " "
130  << _sliders[i]._rows << "\n";
131  }
132 }
133 
134 /**
135  * Called internally when the table is registered.
136  */
137 void SliderTable::
138 do_register() {
139  nassertv(!_is_registered);
140 
141  Sliders::iterator si;
142  for (si = _sliders.begin(); si != _sliders.end(); ++si) {
143  const VertexSlider *slider = (*si)._slider;
144  bool inserted = ((VertexSlider *)slider)->_tables.insert(this).second;
145  nassertv(inserted);
146  }
147  _is_registered = true;
148 }
149 
150 /**
151  * Called internally when the table is unregistered (i.e. right before
152  * destruction).
153  */
154 void SliderTable::
155 do_unregister() {
156  nassertv(_is_registered);
157 
158  Sliders::iterator si;
159  for (si = _sliders.begin(); si != _sliders.end(); ++si) {
160  const VertexSlider *slider = (*si)._slider;
161  ((VertexSlider *)slider)->_tables.erase(this);
162  }
163  _is_registered = false;
164 }
165 
166 /**
167  * Tells the BamReader how to create objects of type SliderTable.
168  */
169 void SliderTable::
171  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
172 }
173 
174 /**
175  * Writes the contents of this object to the datagram for shipping out to a
176  * Bam file.
177  */
178 void SliderTable::
180  TypedWritable::write_datagram(manager, dg);
181 
182  dg.add_uint16(_sliders.size());
183  Sliders::const_iterator si;
184  for (si = _sliders.begin(); si != _sliders.end(); ++si) {
185  manager->write_pointer(dg, (*si)._slider->get_name());
186  manager->write_pointer(dg, (*si)._slider);
187  (*si)._rows.write_datagram(manager, dg);
188  }
189 
190  manager->write_cdata(dg, _cycler);
191 }
192 
193 /**
194  * Receives an array of pointers, one for each time manager->read_pointer()
195  * was called in fillin(). Returns the number of pointers processed.
196  */
197 int SliderTable::
199  int pi = TypedWritableReferenceCount::complete_pointers(p_list, manager);
200 
201  for (size_t n = 0; n < _sliders.size(); ++n) {
202  CPT(InternalName) name = DCAST(InternalName, p_list[pi++]);
203  PT(VertexSlider) slider = DCAST(VertexSlider, p_list[pi++]);
204 
205  _sliders[n]._slider = slider;
206  _sliders_by_name[name].set_bit(n);
207  }
208 
209  return pi;
210 }
211 
212 /**
213  * This function is called by the BamReader's factory when a new object of
214  * type SliderTable is encountered in the Bam file. It should create the
215  * SliderTable and extract its information from the file.
216  */
217 TypedWritable *SliderTable::
218 make_from_bam(const FactoryParams &params) {
219  SliderTable *object = new SliderTable;
220  DatagramIterator scan;
221  BamReader *manager;
222 
223  parse_params(params, scan, manager);
224  object->fillin(scan, manager);
225 
226  return object;
227 }
228 
229 /**
230  * This internal function is called by make_from_bam to read in all of the
231  * relevant data from the BamFile for the new SliderTable.
232  */
233 void SliderTable::
234 fillin(DatagramIterator &scan, BamReader *manager) {
235  TypedWritable::fillin(scan, manager);
236 
237  size_t num_sliders = scan.get_uint16();
238  _sliders.reserve(num_sliders);
239  for (size_t i = 0; i < num_sliders; ++i) {
240  manager->read_pointer(scan);
241  manager->read_pointer(scan);
242  _sliders.push_back(SliderDef());
243  if (manager->get_file_minor_ver() >= 7) {
244  _sliders[i]._rows.read_datagram(scan, manager);
245  } else {
246  // In this case, for bam files prior to 6.7, we must define the
247  // SparseArray with the full number of vertices. This is done in
248  // GeomVertexData::complete_pointers().
249  }
250  }
251 
252  manager->read_cdata(scan, _cycler);
253 }
254 
255 /**
256  *
257  */
258 CycleData *SliderTable::CData::
259 make_copy() const {
260  return new CData(*this);
261 }
262 
263 /**
264  * Writes the contents of this object to the datagram for shipping out to a
265  * Bam file.
266  */
267 void SliderTable::CData::
268 write_datagram(BamWriter *manager, Datagram &dg) const {
269 }
270 
271 /**
272  * This internal function is called by make_from_bam to read in all of the
273  * relevant data from the BamFile for the new SliderTable.
274  */
275 void SliderTable::CData::
276 fillin(DatagramIterator &scan, BamReader *manager) {
277  Thread *current_thread = Thread::get_current_thread();
278  _modified = VertexTransform::get_next_modified(current_thread);
279 }
This class records a set of integers, where each integer is either present or not present in the set.
Definition: sparseArray.h:42
void set_slider(size_t n, const VertexSlider *slider)
Replaces the nth slider.
Definition: sliderTable.cxx:66
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
void set_slider_rows(size_t n, const SparseArray &rows)
Replaces the rows affected by the nth slider.
Definition: sliderTable.cxx:83
get_name
Returns the name of this particular slider.
Definition: vertexSlider.h:43
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 an abstract base class that retains some slider value, which is a linear value that typically...
Definition: vertexSlider.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
void remove_slider(size_t n)
Removes the nth slider.
Definition: sliderTable.cxx:96
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
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.
size_t add_slider(const VertexSlider *slider, const SparseArray &rows)
Adds a new slider to the table, and returns the index number of the new slider.
Stores the total set of VertexSliders that the vertices in a particular GeomVertexData object might d...
Definition: sliderTable.h:37
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
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().
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.
Encodes a string name in a hash table, mapping it to a pointer.
Definition: internalName.h:38
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type SliderTable.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
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: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