Panda3D
xFileDataNodeReference.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 xFileDataNodeReference.cxx
10  * @author drose
11  * @date 2004-10-08
12  */
13 
14 #include "xFileDataNodeReference.h"
15 #include "indent.h"
16 
17 TypeHandle XFileDataNodeReference::_type_handle;
18 
19 /**
20  *
21  */
22 XFileDataNodeReference::
23 XFileDataNodeReference(XFileDataNodeTemplate *object) :
24  XFileDataNode(object->get_x_file(), object->get_name(),
25  object->get_template()),
26  _object(object)
27 {
28  // We steal a copy of the referenced object's children. This is just a one-
29  // time copy, so if you go and change the list of children of the referenced
30  // object, it won't be reflected here in the reference. Since presumably
31  // the reference is only used when parsing static files, that shouldn't be a
32  // problem; but you do need to be aware of it.
33  _children = object->_children;
34  _objects = object->_objects;
35  _children_by_name = object->_children_by_name;
36 }
37 
38 /**
39  * Returns true if this node represents an indirect reference to an object
40  * defined previously in the file. References are generally transparent, so
41  * in most cases you never need to call this, unless you actually need to
42  * differentiate between references and instances; you can simply use the
43  * reference node as if it were itself the object it references.
44  *
45  * If this returns true, the node must be of type XFileDataNodeReference.
46  */
48 is_reference() const {
49  return true;
50 }
51 
52 /**
53  * Returns true if this kind of data object is a complex object that can hold
54  * nested data elements, false otherwise.
55  */
58  return true;
59 }
60 
61 /**
62  * Writes a suitable representation of this node to an .x file in text mode.
63  */
65 write_text(std::ostream &out, int indent_level) const {
66  indent(out, indent_level)
67  << "{ " << _object->get_name() << " }\n";
68 }
69 
70 /**
71  * Returns the number of nested data elements within the object. This may be,
72  * e.g. the size of the array, if it is an array.
73  */
74 int XFileDataNodeReference::
75 get_num_elements() const {
76  return _object->size();
77 }
78 
79 /**
80  * Returns the nth nested data element within the object.
81  */
82 XFileDataObject *XFileDataNodeReference::
83 get_element(int n) {
84  return &((*_object)[n]);
85 }
86 
87 /**
88  * Returns the nested data element within the object that has the indicated
89  * name.
90  */
91 XFileDataObject *XFileDataNodeReference::
92 get_element(const std::string &name) {
93  return &((*_object)[name]);
94 }
This is a node which contains all of the data elements defined by a template.
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
virtual bool is_reference() const
Returns true if this node represents an indirect reference to an object defined previously in the fil...
This is an abstract base class for an XFileNode which is also an XFileDataObject.
Definition: xFileDataNode.h:33
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual bool is_complex_object() const
Returns true if this kind of data object is a complex object that can hold nested data elements,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The abstract base class for a number of different types of data elements that may be stored in the X ...
virtual void write_text(std::ostream &out, int indent_level) const
Writes a suitable representation of this node to an .x file in text mode.