Panda3D
xFileDataObjectArray.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 xFileDataObjectArray.cxx
10  * @author drose
11  * @date 2004-10-07
12  */
13 
14 #include "xFileDataObjectArray.h"
15 #include "string_utils.h"
16 #include "indent.h"
17 
18 TypeHandle XFileDataObjectArray::_type_handle;
19 
20 /**
21  * Returns true if this kind of data object is a complex object that can hold
22  * nested data elements, false otherwise.
23  */
26  return true;
27 }
28 
29 /**
30  * Adds the indicated element as a nested data element, if this data object
31  * type supports it. Returns true if added successfully, false if the data
32  * object type does not support nested data elements.
33  */
36  _nested_elements.push_back(element);
37  return true;
38 }
39 
40 /**
41  * Writes a suitable representation of this node to an .x file in text mode.
42  */
44 write_data(std::ostream &out, int indent_level, const char *separator) const {
45  if (!_nested_elements.empty()) {
46  bool indented = false;
47  for (size_t i = 0; i < _nested_elements.size() - 1; i++) {
48  XFileDataObject *object = _nested_elements[i];
49  if (object->is_complex_object() ||
50  _nested_elements.size() > 16) {
51  // If we have a "complex" nested object, or more than 16 elements in
52  // the array, output it on its own line.
53  if (indented) {
54  out << "\n";
55  indented = false;
56  }
57  object->write_data(out, indent_level, ",");
58 
59  } else {
60  // Otherwise, output them all on the same line.
61  if (!indented) {
62  indent(out, indent_level);
63  indented = true;
64  }
65  out << *object << ", ";
66  }
67  }
68 
69  // The last object in the set is different, because it gets separator
70  // instead of a semicolon, and it always gets a newline.
71  XFileDataObject *object = _nested_elements.back();
72  if (object->is_complex_object()) {
73  if (indented) {
74  out << "\n";
75  }
76  object->write_data(out, indent_level, separator);
77 
78  } else {
79  if (!indented) {
80  indent(out, indent_level);
81  }
82  out << *object << separator << "\n";
83  }
84  }
85 }
86 
87 /**
88  * Returns the number of nested data elements within the object. This may be,
89  * e.g. the size of the array, if it is an array.
90  */
91 int XFileDataObjectArray::
92 get_num_elements() const {
93  return _nested_elements.size();
94 }
95 
96 /**
97  * Returns the nth nested data element within the object.
98  */
99 XFileDataObject *XFileDataObjectArray::
100 get_element(int n) {
101  nassertr(n >= 0 && n < (int)_nested_elements.size(), nullptr);
102  return _nested_elements[n];
103 }
int i() const
Unambiguously returns the object'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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:81
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 ...