Panda3D
xFileDataObject.h
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.h
10  * @author drose
11  * @date 2004-10-03
12  */
13 
14 #ifndef XFILEDATAOBJECT_H
15 #define XFILEDATAOBJECT_H
16 
17 #include "pandatoolbase.h"
18 #include "referenceCount.h"
19 #include "pointerTo.h"
20 #include "dcast.h"
21 #include "luse.h"
22 
23 class XFile;
24 class XFileDataDef;
25 
26 /**
27  * The abstract base class for a number of different types of data elements
28  * that may be stored in the X file.
29  */
30 class XFileDataObject : virtual public ReferenceCount {
31 public:
32  INLINE XFileDataObject(const XFileDataDef *data_def = nullptr);
33  virtual ~XFileDataObject();
34 
35  INLINE const XFileDataDef *get_data_def() const;
36 
37  virtual bool is_complex_object() const;
38  virtual std::string get_type_name() const;
39 
40  INLINE void operator = (int int_value);
41  INLINE void operator = (double double_value);
42  INLINE void operator = (const std::string &string_value);
43  INLINE void operator = (const LVecBase2d &vec);
44  INLINE void operator = (const LVecBase3d &vec);
45  INLINE void operator = (const LVecBase4d &vec);
46  INLINE void operator = (const LMatrix4d &mat);
47 
48  INLINE void set(int int_value);
49  INLINE void set(double double_value);
50  INLINE void set(const std::string &string_value);
51  INLINE void set(const LVecBase2d &vec);
52  INLINE void set(const LVecBase3d &vec);
53  INLINE void set(const LVecBase4d &vec);
54  INLINE void set(const LMatrix4d &mat);
55 
56  INLINE int i() const;
57  INLINE double d() const;
58  INLINE std::string s() const;
59  INLINE LVecBase2d vec2() const;
60  INLINE LVecBase3d vec3() const;
61  INLINE LVecBase4d vec4() const;
62  INLINE LMatrix4d mat4() const;
63 
64  INLINE int size() const;
65  INLINE const XFileDataObject &operator [] (int n) const;
66  INLINE const XFileDataObject &operator [] (const std::string &name) const;
67 
68  INLINE XFileDataObject &operator [] (int n);
69  INLINE XFileDataObject &operator [] (const std::string &name);
70 
71  // The following methods can be used to add elements of a specific type to a
72  // complex object, e.g. an array or a template object.
73 
74  XFileDataObject &add_int(int int_value);
75  XFileDataObject &add_double(double double_value);
76  XFileDataObject &add_string(const std::string &string_value);
77 
78  // The following methods can be used to add elements of a specific type,
79  // based on one of the standard templates.
80 
81  XFileDataObject &add_Vector(XFile *x_file, const LVecBase3d &vector);
83  XFileDataObject &add_IndexedColor(XFile *x_file, int index,
84  const LColor &color);
85  XFileDataObject &add_Coords2d(XFile *x_file, const LVecBase2d &coords);
86 
87 public:
88  virtual bool add_element(XFileDataObject *element);
89 
90  virtual void output_data(std::ostream &out) const;
91  virtual void write_data(std::ostream &out, int indent_level,
92  const char *separator) const;
93 
94 protected:
95  virtual void set_int_value(int int_value);
96  virtual void set_double_value(double double_value);
97  virtual void set_string_value(const std::string &string_value);
98  void store_double_array(int num_elements, const double *values);
99 
100  virtual int get_int_value() const;
101  virtual double get_double_value() const;
102  virtual std::string get_string_value() const;
103  void get_double_array(int num_elements, double *values) const;
104 
105  virtual int get_num_elements() const;
106  virtual XFileDataObject *get_element(int n);
107  virtual XFileDataObject *get_element(const std::string &name);
108 
109  const XFileDataDef *_data_def;
110 
111 public:
112  static TypeHandle get_class_type() {
113  return _type_handle;
114  }
115  static void init_type() {
116  ReferenceCount::init_type();
117  register_type(_type_handle, "XFileDataObject",
118  ReferenceCount::get_class_type());
119  }
120  virtual TypeHandle get_type() const {
121  return get_class_type();
122  }
123  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
124 
125 private:
126  static TypeHandle _type_handle;
127 };
128 
129 INLINE std::ostream &operator << (std::ostream &out, const XFileDataObject &data_object);
130 
131 #include "xFileDataObject.I"
132 
133 #endif
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_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_MeshFace
XFileDataObject & add_MeshFace(XFile *x_file)
Appends a new MeshFace instance.
Definition: xFileDataObject.cxx:113
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
ReferenceCount
A base class for all things that want to be reference-counted.
Definition: referenceCount.h:38
XFileDataObject::operator=
void operator=(int int_value)
Stores the indicated integer value into the object, if it makes sense to do so.
Definition: xFileDataObject.I:38
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
dcast.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
XFileDataObject::add_Vector
XFileDataObject & add_Vector(XFile *x_file, const LVecBase3d &vector)
Appends a new Vector instance.
Definition: xFileDataObject.cxx:96
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
XFile
This represents the complete contents of an X file (file.x) in memory.
Definition: xFile.h:32
XFileDataObject::s
std::string s() const
Unambiguously returns the object's representation as a string, or empty string if the object has no s...
Definition: xFileDataObject.I:198
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::operator[]
const XFileDataObject & operator[](int n) const
Returns the nth nested object within this object.
Definition: xFileDataObject.I:259
XFileDataObject::vec3
LVecBase3d vec3() const
Returns the object's representation as an LVecBase3d.
Definition: xFileDataObject.I:218
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
XFileDataObject::size
int size() const
Returns the number of nested data objects within this object.
Definition: xFileDataObject.I:250
XFileDataObject::add_Coords2d
XFileDataObject & add_Coords2d(XFile *x_file, const LVecBase2d &coords)
Appends a new Coords2d instance.
Definition: xFileDataObject.cxx:146
XFileDataDef
A definition of a single data element appearing within a template record.
Definition: xFileDataDef.h:31
luse.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
XFileDataObject::vec2
LVecBase2d vec2() const
Returns the object's representation as an LVecBase2d.
Definition: xFileDataObject.I:207
xFileDataObject.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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::add_IndexedColor
XFileDataObject & add_IndexedColor(XFile *x_file, int index, const LColor &color)
Appends a new IndexedColor instance.
Definition: xFileDataObject.cxx:128
referenceCount.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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::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::d
double d() const
Unambiguously returns the object's representation as a double, or 0.0 if the object has no double rep...
Definition: xFileDataObject.I:188
pandatoolbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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::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::vec4
LVecBase4d vec4() const
Returns the object's representation as an LVecBase4d.
Definition: xFileDataObject.I:229
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_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
pointerTo.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
XFileDataObject::mat4
LMatrix4d mat4() const
Returns the object's representation as an LMatrix4d.
Definition: xFileDataObject.I:240