Panda3D
 All Classes Functions Variables Enumerations
xFileDataObject.cxx
1 // Filename: xFileDataObject.cxx
2 // Created by: drose (03Oct04)
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 "xFileDataObject.h"
16 #include "xFileTemplate.h"
17 #include "xFile.h"
18 #include "xFileDataNodeTemplate.h"
19 #include "xFileDataObjectInteger.h"
20 #include "xFileDataObjectDouble.h"
21 #include "xFileDataObjectString.h"
22 #include "config_xfile.h"
23 #include "indent.h"
24 
25 TypeHandle XFileDataObject::_type_handle;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: XFileDataObject::Destructor
29 // Access: Public, Virtual
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 XFileDataObject::
33 ~XFileDataObject() {
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: XFileDataObject::is_complex_object
38 // Access: Public, Virtual
39 // Description: Returns true if this kind of data object is a complex
40 // object that can hold nested data elements, false
41 // otherwise.
42 ////////////////////////////////////////////////////////////////////
45  return false;
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: XFileDataObject::get_type_name
50 // Access: Public, Virtual
51 // Description: Returns a string that represents the type of object
52 // this data object represents.
53 ////////////////////////////////////////////////////////////////////
54 string XFileDataObject::
55 get_type_name() const {
56  return get_type().get_name();
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: XFileDataObject::add_int
61 // Access: Public
62 // Description: Appends a new integer value to the data object, if it
63 // makes sense to do so. Normally, this is valid only
64 // for a DataObjectArray, or in certain special cases for
65 // a DataNodeTemplate.
66 ////////////////////////////////////////////////////////////////////
68 add_int(int int_value) {
69  XFileDataObject *object =
70  new XFileDataObjectInteger(get_data_def(), int_value);
71  add_element(object);
72  return *object;
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: XFileDataObject::add_double
77 // Access: Public
78 // Description: Appends a new floating-point value to the data
79 // object, if it makes sense to do so. Normally, this
80 // is valid only for a DataObjectArray, or in certain
81 // special cases for a DataNodeTemplate.
82 ////////////////////////////////////////////////////////////////////
84 add_double(double double_value) {
85  XFileDataObject *object =
86  new XFileDataObjectDouble(get_data_def(), double_value);
87  add_element(object);
88  return *object;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: XFileDataObject::add_string
93 // Access: Public
94 // Description: Appends a new string value to the data object, if it
95 // makes sense to do so. Normally, this is valid only
96 // for a DataObjectArray, or in certain special cases for
97 // a DataNodeTemplate.
98 ////////////////////////////////////////////////////////////////////
100 add_string(const string &string_value) {
101  XFileDataObject *object =
102  new XFileDataObjectString(get_data_def(), string_value);
103  add_element(object);
104  return *object;
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: XFileDataObject::add_Vector
109 // Access: Public
110 // Description: Appends a new Vector instance.
111 ////////////////////////////////////////////////////////////////////
113 add_Vector(XFile *x_file, const LVecBase3d &vector) {
114  XFileTemplate *xtemplate = XFile::find_standard_template("Vector");
115  nassertr(xtemplate != (XFileTemplate *)NULL, *this);
116  XFileDataNodeTemplate *node =
117  new XFileDataNodeTemplate(x_file, "", xtemplate);
118  add_element(node);
119  node->zero_fill();
120 
121  node->set(vector);
122 
123  return *node;
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: XFileDataObject::add_MeshFace
128 // Access: Public
129 // Description: Appends a new MeshFace instance.
130 ////////////////////////////////////////////////////////////////////
132 add_MeshFace(XFile *x_file) {
133  XFileTemplate *xtemplate = XFile::find_standard_template("MeshFace");
134  nassertr(xtemplate != (XFileTemplate *)NULL, *this);
135  XFileDataNodeTemplate *node =
136  new XFileDataNodeTemplate(x_file, "", xtemplate);
137  add_element(node);
138  node->zero_fill();
139 
140  return *node;
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function: XFileDataObject::add_IndexedColor
145 // Access: Public
146 // Description: Appends a new IndexedColor instance.
147 ////////////////////////////////////////////////////////////////////
149 add_IndexedColor(XFile *x_file, int index, const LColor &color) {
150  XFileTemplate *xtemplate = XFile::find_standard_template("IndexedColor");
151  nassertr(xtemplate != (XFileTemplate *)NULL, *this);
152  XFileDataNodeTemplate *node =
153  new XFileDataNodeTemplate(x_file, "", xtemplate);
154  add_element(node);
155  node->zero_fill();
156 
157  (*node)["index"] = index;
158  (*node)["indexColor"] = LCAST(double, color);
159 
160  return *node;
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function: XFileDataObject::add_Coords2d
165 // Access: Public
166 // Description: Appends a new Coords2d instance.
167 ////////////////////////////////////////////////////////////////////
169 add_Coords2d(XFile *x_file, const LVecBase2d &coords) {
170  XFileTemplate *xtemplate = XFile::find_standard_template("Coords2d");
171  nassertr(xtemplate != (XFileTemplate *)NULL, *this);
172  XFileDataNodeTemplate *node =
173  new XFileDataNodeTemplate(x_file, "", xtemplate);
174  add_element(node);
175  node->zero_fill();
176 
177  node->set(coords);
178 
179  return *node;
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: XFileDataObject::add_element
184 // Access: Public, Virtual
185 // Description: Adds the indicated element as a nested data element,
186 // if this data object type supports it. Returns true
187 // if added successfully, false if the data object type
188 // does not support nested data elements.
189 ////////////////////////////////////////////////////////////////////
192  return false;
193 }
194 
195 ////////////////////////////////////////////////////////////////////
196 // Function: XFileDataObject::output_data
197 // Access: Public, Virtual
198 // Description: Writes a suitable representation of this node to an
199 // .x file in text mode.
200 ////////////////////////////////////////////////////////////////////
202 output_data(ostream &out) const {
203  out << "(" << get_type() << "::output_data() not implemented.)";
204 }
205 
206 ////////////////////////////////////////////////////////////////////
207 // Function: XFileDataObject::write_data
208 // Access: Public, Virtual
209 // Description: Writes a suitable representation of this node to an
210 // .x file in text mode.
211 ////////////////////////////////////////////////////////////////////
213 write_data(ostream &out, int indent_level, const char *) const {
214  indent(out, indent_level)
215  << "(" << get_type() << "::write_data() not implemented.)\n";
216 }
217 
218 ////////////////////////////////////////////////////////////////////
219 // Function: XFileDataObject::set_int_value
220 // Access: Protected, Virtual
221 // Description: Sets the object's value as an integer, if this is
222 // legal.
223 ////////////////////////////////////////////////////////////////////
224 void XFileDataObject::
225 set_int_value(int int_value) {
226  xfile_cat.error()
227  << get_type_name() << " does not support integer values.\n";
228 }
229 
230 ////////////////////////////////////////////////////////////////////
231 // Function: XFileDataObject::set_double_value
232 // Access: Protected, Virtual
233 // Description: Sets the object's value as a floating-point number,
234 // if this is legal.
235 ////////////////////////////////////////////////////////////////////
236 void XFileDataObject::
237 set_double_value(double double_value) {
238  xfile_cat.error()
239  << get_type_name() << " does not support floating-point values.\n";
240 }
241 
242 ////////////////////////////////////////////////////////////////////
243 // Function: XFileDataObject::set_string_value
244 // Access: Protected, Virtual
245 // Description: Sets the object's value as a string, if this is
246 // legal.
247 ////////////////////////////////////////////////////////////////////
248 void XFileDataObject::
249 set_string_value(const string &string_value) {
250  xfile_cat.error()
251  << get_type_name() << " does not support string values.\n";
252 }
253 
254 ////////////////////////////////////////////////////////////////////
255 // Function: XFileDataObject::store_double_array
256 // Access: Protected
257 // Description: Stores the indicated array of doubles in the nested
258 // elements within this object. There must be exactly
259 // the indicated number of nested values, and they must
260 // all accept a double.
261 ////////////////////////////////////////////////////////////////////
262 void XFileDataObject::
263 store_double_array(int num_elements, const double *values) {
264  if (get_num_elements() != num_elements) {
265  xfile_cat.error()
266  << get_type_name() << " does not accept "
267  << num_elements << " values.\n";
268  return;
269  }
270 
271  for (int i = 0; i < num_elements; i++) {
272  get_element(i)->set_double_value(values[i]);
273  }
274 }
275 
276 
277 ////////////////////////////////////////////////////////////////////
278 // Function: XFileDataObject::get_int_value
279 // Access: Protected, Virtual
280 // Description: Returns the object's representation as an integer, if
281 // it has one.
282 ////////////////////////////////////////////////////////////////////
283 int XFileDataObject::
284 get_int_value() const {
285  return 0;
286 }
287 
288 ////////////////////////////////////////////////////////////////////
289 // Function: XFileDataObject::get_double_value
290 // Access: Protected, Virtual
291 // Description: Returns the object's representation as a double, if
292 // it has one.
293 ////////////////////////////////////////////////////////////////////
294 double XFileDataObject::
295 get_double_value() const {
296  return 0.0;
297 }
298 
299 ////////////////////////////////////////////////////////////////////
300 // Function: XFileDataObject::get_string_value
301 // Access: Protected, Virtual
302 // Description: Returns the object's representation as a string, if
303 // it has one.
304 ////////////////////////////////////////////////////////////////////
305 string XFileDataObject::
306 get_string_value() const {
307  return string();
308 }
309 
310 ////////////////////////////////////////////////////////////////////
311 // Function: XFileDataObject::get_double_array
312 // Access: Protected
313 // Description: Fills the indicated array of doubles with the values
314 // from the nested elements within this object. There
315 // must be exactly the indicated number of nested
316 // values, and they must all return a double.
317 ////////////////////////////////////////////////////////////////////
318 void XFileDataObject::
319 get_double_array(int num_elements, double *values) const {
320  if (get_num_elements() != num_elements) {
321  xfile_cat.error()
322  << get_type_name() << " does not contain "
323  << num_elements << " values.\n";
324  return;
325  }
326 
327  for (int i = 0; i < num_elements; i++) {
328  values[i] = ((XFileDataObject *)this)->get_element(i)->get_double_value();
329  }
330 }
331 
332 ////////////////////////////////////////////////////////////////////
333 // Function: XFileDataObject::get_num_elements
334 // Access: Protected, Virtual
335 // Description: Returns the number of nested data elements within the
336 // object. This may be, e.g. the size of the array, if
337 // it is an array.
338 ////////////////////////////////////////////////////////////////////
339 int XFileDataObject::
340 get_num_elements() const {
341  return 0;
342 }
343 
344 ////////////////////////////////////////////////////////////////////
345 // Function: XFileDataObject::get_element
346 // Access: Protected, Virtual
347 // Description: Returns the nth nested data element within the
348 // object.
349 ////////////////////////////////////////////////////////////////////
350 XFileDataObject *XFileDataObject::
351 get_element(int n) {
352  xfile_cat.warning()
353  << "Looking for [" << n << "] within data object of type "
354  << get_type_name() << ", does not support nested objects.\n";
355  return NULL;
356 }
357 
358 ////////////////////////////////////////////////////////////////////
359 // Function: XFileDataObject::get_element
360 // Access: Protected, Virtual
361 // Description: Returns the nested data element within the
362 // object that has the indicated name.
363 ////////////////////////////////////////////////////////////////////
364 XFileDataObject *XFileDataObject::
365 get_element(const string &name) {
366  xfile_cat.warning()
367  << "Looking for [\"" << name << "\"] within data object of type "
368  << get_type_name() << ", does not support nested objects.\n";
369  return NULL;
370 }
virtual void write_data(ostream &out, int indent_level, const char *separator) const
Writes a suitable representation of this node to an .x file in text mode.
virtual bool add_element(XFileDataObject *element)
Adds the indicated element as a nested data element, if this data object type supports it...
XFileDataObject & add_int(int int_value)
Appends a new integer value to the data object, if it makes sense to do so.
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:1241
virtual string get_type_name() const
Returns a string that represents the type of object this data object represents.
virtual void output_data(ostream &out) const
Writes a suitable representation of this node to an .x file in text mode.
string get_name(TypedObject *object=(TypedObject *) NULL) const
Returns the name of the type.
Definition: typeHandle.I:132
An string-valued data element.
XFileDataObject & add_MeshFace(XFile *x_file)
Appends a new MeshFace instance.
void zero_fill()
Fills the data node with zero-valued elements appropriate to the template.
static XFileTemplate * find_standard_template(const string &name)
Returns the standard template associated with the indicated name, if any, or NULL if none...
Definition: xFile.cxx:262
This is a node which contains all of the data elements defined by a template.
const XFileDataDef * get_data_def() const
Returns the data object that this object is represented by, if any, or NULL if there is none...
XFileDataObject & add_Vector(XFile *x_file, const LVecBase3d &vector)
Appends a new Vector instance.
XFileDataObject & add_Coords2d(XFile *x_file, const LVecBase2d &coords)
Appends a new Coords2d instance.
XFileDataObject & add_IndexedColor(XFile *x_file, int index, const LColor &color)
Appends a new IndexedColor instance.
void set(int int_value)
Stores the indicated integer value into the object, if it makes sense to do so.
XFileDataObject & add_double(double double_value)
Appends a new floating-point value to the data object, if it makes sense to do so.
An integer-valued data element.
XFileDataObject & add_string(const string &string_value)
Appends a new string value to the data object, if it makes sense to do so.
virtual bool is_complex_object() const
Returns true if this kind of data object is a complex object that can hold nested data elements...
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1455
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
An double-valued data element.
This represents the complete contents of an X file (file.x) in memory.
Definition: xFile.h:35
A template definition in the X file.
Definition: xFileTemplate.h:29
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
The abstract base class for a number of different types of data elements that may be stored in the X ...
int i() const
Unambiguously returns the object&#39;s representation as an integer, or 0 if the object has no integer re...