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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void output_data(std::ostream &out) const
Writes a suitable representation of this node to an .x file in text mode.
std::string s() const
Unambiguously returns the object's representation as a string, or empty string if the object has no s...
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int i() const
Unambiguously returns the object's representation as an integer, or 0 if the object has no integer re...
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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_MeshFace(XFile *x_file)
Appends a new MeshFace instance.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A definition of a single data element appearing within a template record.
Definition: xFileDataDef.h:31
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_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.
LVecBase4d vec4() const
Returns the object's representation as an LVecBase4d.
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.
virtual std::string get_type_name() const
Returns a string that represents the type of object this data object represents.
int size() const
Returns the number of nested data objects within this object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for all things that want to be reference-counted.
LVecBase2d vec2() const
Returns the object's representation as an LVecBase2d.
LMatrix4d mat4() const
Returns the object's representation as an LMatrix4d.
XFileDataObject & add_string(const std::string &string_value)
Appends a new string value to the data object, if it makes sense to do so.
void operator=(int int_value)
Stores the indicated integer value into the object, if it makes sense to do so.
This represents the complete contents of an X file (file.x) in memory.
Definition: xFile.h:32
virtual bool is_complex_object() const
Returns true if this kind of data object is a complex object that can hold nested data elements,...
double d() const
Unambiguously returns the object's representation as a double, or 0.0 if the object has no double rep...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
LVecBase3d vec3() const
Returns the object's representation as an LVecBase3d.
const XFileDataObject & operator [](int n) const
Returns the nth nested object within this object.
The abstract base class for a number of different types of data elements that may be stored in the X ...