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"
17#include "xFileDataNodeTemplate.h"
18#include "xFileDataObjectInteger.h"
19#include "xFileDataObjectDouble.h"
20#include "xFileDataObjectString.h"
21#include "config_xfile.h"
22#include "indent.h"
23
24 using std::string;
25
26 TypeHandle 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 */
39 bool XFileDataObject::
40 is_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 */
48 string XFileDataObject::
49 get_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 */
58XFileDataObject &XFileDataObject::
59 add_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 */
71XFileDataObject &XFileDataObject::
72 add_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 */
84XFileDataObject &XFileDataObject::
85 add_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 */
95XFileDataObject &XFileDataObject::
96 add_Vector(XFile *x_file, const LVecBase3d &vector) {
97 XFileTemplate *xtemplate = XFile::find_standard_template("Vector");
98 nassertr(xtemplate != nullptr, *this);
99 XFileDataNodeTemplate *node =
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 */
112XFileDataObject &XFileDataObject::
113 add_MeshFace(XFile *x_file) {
114 XFileTemplate *xtemplate = XFile::find_standard_template("MeshFace");
115 nassertr(xtemplate != nullptr, *this);
116 XFileDataNodeTemplate *node =
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 */
127XFileDataObject &XFileDataObject::
128 add_IndexedColor(XFile *x_file, int index, const LColor &color) {
129 XFileTemplate *xtemplate = XFile::find_standard_template("IndexedColor");
130 nassertr(xtemplate != nullptr, *this);
131 XFileDataNodeTemplate *node =
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 */
145XFileDataObject &XFileDataObject::
146 add_Coords2d(XFile *x_file, const LVecBase2d &coords) {
147 XFileTemplate *xtemplate = XFile::find_standard_template("Coords2d");
148 nassertr(xtemplate != nullptr, *this);
149 XFileDataNodeTemplate *node =
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 */
164 bool XFileDataObject::
165 add_element(XFileDataObject *element) {
166 return false;
167}
168
169/**
170 * Writes a suitable representation of this node to an .x file in text mode.
171 */
172 void XFileDataObject::
173 output_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 */
180 void XFileDataObject::
181 write_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 */
189 void 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 */
198 void 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 */
207 void 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 */
218 void 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 */
236 int 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 */
244 double 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 */
252 string 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 */
262 void 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 */
280 int XFileDataObject::
281get_num_elements() const {
282 return 0;
283}
284
285/**
286 * Returns the nth nested data element within the object.
287 */
288 XFileDataObject *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 */
300 XFileDataObject *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
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
XFileDataNodeTemplate
This is a node which contains all of the data elements defined by a template.
Definition xFileDataNodeTemplate.h:30
XFileDataNodeTemplate::zero_fill
void zero_fill()
Fills the data node with zero-valued elements appropriate to the template.
Definition xFileDataNodeTemplate.cxx:38
XFileDataObjectDouble
An double-valued data element.
Definition xFileDataObjectDouble.h:24
XFileDataObjectInteger
An integer-valued data element.
Definition xFileDataObjectInteger.h:24
XFileDataObjectString
An string-valued data element.
Definition xFileDataObjectString.h:24
XFileDataObject
The abstract base class for a number of different types of data elements that may be stored in the X ...
Definition xFileDataObject.h:30
XFileDataObject::get_data_def
const XFileDataDef * get_data_def() const
Returns the data object that this object is represented by, if any, or NULL if there is none.
Definition xFileDataObject.I:28
XFileDataObject::set
void set(int int_value)
Stores the indicated integer value into the object, if it makes sense to do so.
Definition xFileDataObject.I:108
XFileDataObject::get_type_name
virtual std::string get_type_name() const
Returns a string that represents the type of object this data object represents.
Definition xFileDataObject.cxx:49
XFileDataObject::add_int
XFileDataObject & add_int(int int_value)
Appends a new integer value to the data object, if it makes sense to do so.
Definition xFileDataObject.cxx:59
XFileDataObject::is_complex_object
virtual bool is_complex_object() const
Returns true if this kind of data object is a complex object that can hold nested data elements,...
Definition xFileDataObject.cxx:40
XFileDataObject::i
int i() const
Unambiguously returns the object's representation as an integer, or 0 if the object has no integer re...
Definition xFileDataObject.I:178
XFileDataObject::write_data
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.
Definition xFileDataObject.cxx:181
XFileDataObject::add_MeshFace
XFileDataObject & add_MeshFace(XFile *x_file)
Appends a new MeshFace instance.
Definition xFileDataObject.cxx:113
XFileDataObject::output_data
virtual void output_data(std::ostream &out) const
Writes a suitable representation of this node to an .x file in text mode.
Definition xFileDataObject.cxx:173
XFileDataObject::add_Vector
XFileDataObject & add_Vector(XFile *x_file, const LVecBase3d &vector)
Appends a new Vector instance.
Definition xFileDataObject.cxx:96
XFileDataObject::add_double
XFileDataObject & add_double(double double_value)
Appends a new floating-point value to the data object, if it makes sense to do so.
Definition xFileDataObject.cxx:72
XFileDataObject::add_IndexedColor
XFileDataObject & add_IndexedColor(XFile *x_file, int index, const LColor &color)
Appends a new IndexedColor instance.
Definition xFileDataObject.cxx:128
XFileDataObject::add_string
XFileDataObject & add_string(const std::string &string_value)
Appends a new string value to the data object, if it makes sense to do so.
Definition xFileDataObject.cxx:85
XFileDataObject::add_element
virtual bool add_element(XFileDataObject *element)
Adds the indicated element as a nested data element, if this data object type supports it.
Definition xFileDataObject.cxx:165
XFileDataObject::add_Coords2d
XFileDataObject & add_Coords2d(XFile *x_file, const LVecBase2d &coords)
Appends a new Coords2d instance.
Definition xFileDataObject.cxx:146
XFileTemplate
A template definition in the X file.
Definition xFileTemplate.h:27
XFile
This represents the complete contents of an X file (file.x) in memory.
Definition xFile.h:32
XFile::find_standard_template
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
vector
STL class.
config_xfile.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
indent.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFileDataNodeTemplate.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFileDataObjectDouble.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFileDataObjectInteger.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFileDataObjectString.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFileDataObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFileTemplate.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
xFile.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.