Panda3D
Loading...
Searching...
No Matches
xFileDataObject.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 xFileDataObject.cxx
10 * @author drose
11 * @date 2004-10-03
12 */
13
14#include "xFileDataObject.h"
15#include "xFileTemplate.h"
16#include "xFile.h"
21#include "config_xfile.h"
22#include "indent.h"
23
24using std::string;
25
26TypeHandle XFileDataObject::_type_handle;
27
28/**
29 *
30 */
31XFileDataObject::
32~XFileDataObject() {
33}
34
35/**
36 * Returns true if this kind of data object is a complex object that can hold
37 * nested data elements, false otherwise.
38 */
40is_complex_object() const {
41 return false;
42}
43
44/**
45 * Returns a string that represents the type of object this data object
46 * represents.
47 */
49get_type_name() const {
50 return get_type().get_name();
51}
52
53/**
54 * Appends a new integer value to the data object, if it makes sense to do so.
55 * Normally, this is valid only for a DataObjectArray, or in certain special
56 * cases for a DataNodeTemplate.
57 */
59add_int(int int_value) {
60 XFileDataObject *object =
61 new XFileDataObjectInteger(get_data_def(), int_value);
62 add_element(object);
63 return *object;
64}
65
66/**
67 * Appends a new floating-point value to the data object, if it makes sense to
68 * do so. Normally, this is valid only for a DataObjectArray, or in certain
69 * special cases for a DataNodeTemplate.
70 */
72add_double(double double_value) {
73 XFileDataObject *object =
74 new XFileDataObjectDouble(get_data_def(), double_value);
75 add_element(object);
76 return *object;
77}
78
79/**
80 * Appends a new string value to the data object, if it makes sense to do so.
81 * Normally, this is valid only for a DataObjectArray, or in certain special
82 * cases for a DataNodeTemplate.
83 */
85add_string(const string &string_value) {
86 XFileDataObject *object =
87 new XFileDataObjectString(get_data_def(), string_value);
88 add_element(object);
89 return *object;
90}
91
92/**
93 * Appends a new Vector instance.
94 */
96add_Vector(XFile *x_file, const LVecBase3d &vector) {
97 XFileTemplate *xtemplate = XFile::find_standard_template("Vector");
98 nassertr(xtemplate != nullptr, *this);
100 new XFileDataNodeTemplate(x_file, "", xtemplate);
101 add_element(node);
102 node->zero_fill();
103
104 node->set(vector);
105
106 return *node;
107}
108
109/**
110 * Appends a new MeshFace instance.
111 */
113add_MeshFace(XFile *x_file) {
114 XFileTemplate *xtemplate = XFile::find_standard_template("MeshFace");
115 nassertr(xtemplate != nullptr, *this);
117 new XFileDataNodeTemplate(x_file, "", xtemplate);
118 add_element(node);
119 node->zero_fill();
120
121 return *node;
122}
123
124/**
125 * Appends a new IndexedColor instance.
126 */
128add_IndexedColor(XFile *x_file, int index, const LColor &color) {
129 XFileTemplate *xtemplate = XFile::find_standard_template("IndexedColor");
130 nassertr(xtemplate != nullptr, *this);
132 new XFileDataNodeTemplate(x_file, "", xtemplate);
133 add_element(node);
134 node->zero_fill();
135
136 (*node)["index"] = index;
137 (*node)["indexColor"] = LCAST(double, color);
138
139 return *node;
140}
141
142/**
143 * Appends a new Coords2d instance.
144 */
146add_Coords2d(XFile *x_file, const LVecBase2d &coords) {
147 XFileTemplate *xtemplate = XFile::find_standard_template("Coords2d");
148 nassertr(xtemplate != nullptr, *this);
150 new XFileDataNodeTemplate(x_file, "", xtemplate);
151 add_element(node);
152 node->zero_fill();
153
154 node->set(coords);
155
156 return *node;
157}
158
159/**
160 * Adds the indicated element as a nested data element, if this data object
161 * type supports it. Returns true if added successfully, false if the data
162 * object type does not support nested data elements.
163 */
166 return false;
167}
168
169/**
170 * Writes a suitable representation of this node to an .x file in text mode.
171 */
173output_data(std::ostream &out) const {
174 out << "(" << get_type() << "::output_data() not implemented.)";
175}
176
177/**
178 * Writes a suitable representation of this node to an .x file in text mode.
179 */
181write_data(std::ostream &out, int indent_level, const char *) const {
182 indent(out, indent_level)
183 << "(" << get_type() << "::write_data() not implemented.)\n";
184}
185
186/**
187 * Sets the object's value as an integer, if this is legal.
188 */
189void XFileDataObject::
190set_int_value(int int_value) {
191 xfile_cat.error()
192 << get_type_name() << " does not support integer values.\n";
193}
194
195/**
196 * Sets the object's value as a floating-point number, if this is legal.
197 */
198void XFileDataObject::
199set_double_value(double double_value) {
200 xfile_cat.error()
201 << get_type_name() << " does not support floating-point values.\n";
202}
203
204/**
205 * Sets the object's value as a string, if this is legal.
206 */
207void XFileDataObject::
208set_string_value(const string &string_value) {
209 xfile_cat.error()
210 << get_type_name() << " does not support string values.\n";
211}
212
213/**
214 * Stores the indicated array of doubles in the nested elements within this
215 * object. There must be exactly the indicated number of nested values, and
216 * they must all accept a double.
217 */
218void XFileDataObject::
219store_double_array(int num_elements, const double *values) {
220 if (get_num_elements() != num_elements) {
221 xfile_cat.error()
222 << get_type_name() << " does not accept "
223 << num_elements << " values.\n";
224 return;
225 }
226
227 for (int i = 0; i < num_elements; i++) {
228 get_element(i)->set_double_value(values[i]);
229 }
230}
231
232
233/**
234 * Returns the object's representation as an integer, if it has one.
235 */
236int XFileDataObject::
237get_int_value() const {
238 return 0;
239}
240
241/**
242 * Returns the object's representation as a double, if it has one.
243 */
244double XFileDataObject::
245get_double_value() const {
246 return 0.0;
247}
248
249/**
250 * Returns the object's representation as a string, if it has one.
251 */
252string XFileDataObject::
253get_string_value() const {
254 return string();
255}
256
257/**
258 * Fills the indicated array of doubles with the values from the nested
259 * elements within this object. There must be exactly the indicated number of
260 * nested values, and they must all return a double.
261 */
262void XFileDataObject::
263get_double_array(int num_elements, double *values) const {
264 if (get_num_elements() != num_elements) {
265 xfile_cat.error()
266 << get_type_name() << " does not contain "
267 << num_elements << " values.\n";
268 return;
269 }
270
271 for (int i = 0; i < num_elements; i++) {
272 values[i] = ((XFileDataObject *)this)->get_element(i)->get_double_value();
273 }
274}
275
276/**
277 * Returns the number of nested data elements within the object. This may be,
278 * e.g. the size of the array, if it is an array.
279 */
280int XFileDataObject::
281get_num_elements() const {
282 return 0;
283}
284
285/**
286 * Returns the nth nested data element within the object.
287 */
288XFileDataObject *XFileDataObject::
289get_element(int n) {
290 xfile_cat.warning()
291 << "Looking for [" << n << "] within data object of type "
292 << get_type_name() << ", does not support nested objects.\n";
293 return nullptr;
294}
295
296/**
297 * Returns the nested data element within the object that has the indicated
298 * name.
299 */
300XFileDataObject *XFileDataObject::
301get_element(const string &name) {
302 xfile_cat.warning()
303 << "Looking for [\"" << name << "\"] within data object of type "
304 << get_type_name() << ", does not support nested objects.\n";
305 return nullptr;
306}
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
get_name
Returns the name of the type.
Definition typeHandle.h:136
This is a node which contains all of the data elements defined by a template.
void zero_fill()
Fills the data node with zero-valued elements appropriate to the template.
An double-valued data element.
An integer-valued data element.
An string-valued data element.
The abstract base class for a number of different types of data elements that may be stored in the X ...
const XFileDataDef * get_data_def() const
Returns the data object that this object is represented by, if any, or NULL if there is none.
void set(int int_value)
Stores the indicated integer value into the object, if it makes sense to do so.
virtual std::string get_type_name() const
Returns a string that represents the type of object this data object represents.
XFileDataObject & add_int(int int_value)
Appends a new integer 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,...
int i() const
Unambiguously returns the object's representation as an integer, or 0 if the object has no integer re...
virtual void write_data(std::ostream &out, int indent_level, const char *separator) const
Writes a suitable representation of this node to an .x file in text mode.
XFileDataObject & add_MeshFace(XFile *x_file)
Appends a new MeshFace instance.
virtual void output_data(std::ostream &out) const
Writes a suitable representation of this node to an .x file in text mode.
XFileDataObject & add_Vector(XFile *x_file, const LVecBase3d &vector)
Appends a new Vector instance.
XFileDataObject & add_double(double double_value)
Appends a new floating-point value to the data object, if it makes sense to do so.
XFileDataObject & add_IndexedColor(XFile *x_file, int index, const LColor &color)
Appends a new IndexedColor instance.
XFileDataObject & add_string(const std::string &string_value)
Appends a new string value to the data object, if it makes sense to do so.
virtual bool add_element(XFileDataObject *element)
Adds the indicated element as a nested data element, if this data object type supports it.
XFileDataObject & add_Coords2d(XFile *x_file, const LVecBase2d &coords)
Appends a new Coords2d instance.
A template definition in the X file.
This represents the complete contents of an X file (file.x) in memory.
Definition xFile.h:32
static XFileTemplate * find_standard_template(const std::string &name)
Returns the standard template associated with the indicated name, if any, or NULL if none.
Definition xFile.cxx:237
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.