Panda3D
xFileDataObjectArray.cxx
1 // Filename: xFileDataObjectArray.cxx
2 // Created by: drose (07Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "xFileDataObjectArray.h"
16 #include "string_utils.h"
17 #include "indent.h"
18 
19 TypeHandle XFileDataObjectArray::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: XFileDataObjectArray::is_complex_object
23 // Access: Public, Virtual
24 // Description: Returns true if this kind of data object is a complex
25 // object that can hold nested data elements, false
26 // otherwise.
27 ////////////////////////////////////////////////////////////////////
30  return true;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: XFileDataObjectArray::add_element
35 // Access: Public, Virtual
36 // Description: Adds the indicated element as a nested data element,
37 // if this data object type supports it. Returns true
38 // if added successfully, false if the data object type
39 // does not support nested data elements.
40 ////////////////////////////////////////////////////////////////////
43  _nested_elements.push_back(element);
44  return true;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: XFileDataObjectArray::write_data
49 // Access: Public, Virtual
50 // Description: Writes a suitable representation of this node to an
51 // .x file in text mode.
52 ////////////////////////////////////////////////////////////////////
54 write_data(ostream &out, int indent_level, const char *separator) const {
55  if (!_nested_elements.empty()) {
56  bool indented = false;
57  for (size_t i = 0; i < _nested_elements.size() - 1; i++) {
58  XFileDataObject *object = _nested_elements[i];
59  if (object->is_complex_object() ||
60  _nested_elements.size() > 16) {
61  // If we have a "complex" nested object, or more than 16
62  // elements in the array, output it on its own line.
63  if (indented) {
64  out << "\n";
65  indented = false;
66  }
67  object->write_data(out, indent_level, ",");
68 
69  } else {
70  // Otherwise, output them all on the same line.
71  if (!indented) {
72  indent(out, indent_level);
73  indented = true;
74  }
75  out << *object << ", ";
76  }
77  }
78 
79  // The last object in the set is different, because it gets
80  // separator instead of a semicolon, and it always gets a newline.
81  XFileDataObject *object = _nested_elements.back();
82  if (object->is_complex_object()) {
83  if (indented) {
84  out << "\n";
85  }
86  object->write_data(out, indent_level, separator);
87 
88  } else {
89  if (!indented) {
90  indent(out, indent_level);
91  }
92  out << *object << separator << "\n";
93  }
94  }
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: XFileDataObjectArray::get_num_elements
99 // Access: Protected, Virtual
100 // Description: Returns the number of nested data elements within the
101 // object. This may be, e.g. the size of the array, if
102 // it is an array.
103 ////////////////////////////////////////////////////////////////////
104 int XFileDataObjectArray::
105 get_num_elements() const {
106  return _nested_elements.size();
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: XFileDataObjectArray::get_element
111 // Access: Protected, Virtual
112 // Description: Returns the nth nested data element within the
113 // object.
114 ////////////////////////////////////////////////////////////////////
115 XFileDataObject *XFileDataObjectArray::
116 get_element(int n) {
117  nassertr(n >= 0 && n < (int)_nested_elements.size(), NULL);
118  return _nested_elements[n];
119 }
int i() const
Unambiguously returns the object&#39;s representation as an integer, or 0 if the object has no integer re...
virtual bool add_element(XFileDataObject *element)
Adds the indicated element as a nested data element, if this data object type supports it...
virtual bool is_complex_object() const
Returns true if this kind of data object is a complex object that can hold nested data elements...
virtual bool is_complex_object() const
Returns true if this kind of data object is a complex object that can hold nested data elements...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual void write_data(ostream &out, int indent_level, const char *separator) const
Writes a suitable representation of this node to an .x file in text mode.
The abstract base class for a number of different types of data elements that may be stored in the X ...