Panda3D
xFileTemplate.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 xFileTemplate.cxx
10  * @author drose
11  * @date 2004-10-03
12  */
13 
14 #include "xFileTemplate.h"
15 #include "indent.h"
16 
17 TypeHandle XFileTemplate::_type_handle;
18 
19 /**
20  *
21  */
22 XFileTemplate::
23 XFileTemplate(XFile *x_file, const std::string &name, const WindowsGuid &guid) :
24  XFileNode(x_file, name),
25  _guid(guid),
26  _is_standard(false),
27  _open(false)
28 {
29 }
30 
31 /**
32  *
33  */
34 XFileTemplate::
35 ~XFileTemplate() {
36  clear();
37 }
38 
39 /**
40  * Returns true if this node has a GUID associated.
41  */
43 has_guid() const {
44  return true;
45 }
46 
47 /**
48  * Returns the GUID associated with this template.
49  */
51 get_guid() const {
52  return _guid;
53 }
54 
55 /**
56  * Returns true if this node represents the definition of some template. This
57  * is the template definition, not an actual data object that represents an
58  * instance of the template. If the file strictly uses standard templates,
59  * the presence of template definitions is optional.
60  *
61  * If this returns true, the node must be of type XFileTemplate.
62  */
64 is_template_def() const {
65  return true;
66 }
67 
68 /**
69  * Removes all children from the node, and otherwise resets it to its initial
70  * state.
71  */
73 clear() {
75  _options.clear();
76 }
77 
78 /**
79  * Writes a suitable representation of this node to an .x file in text mode.
80  */
82 write_text(std::ostream &out, int indent_level) const {
83  indent(out, indent_level)
84  << "template " << get_name() << " {\n";
85  indent(out, indent_level + 2)
86  << "<" << _guid << ">\n";
87 
88  XFileNode::write_text(out, indent_level + 2);
89 
90  if (get_open()) {
91  // An open template
92  indent(out, indent_level + 2)
93  << "[ ... ]\n";
94 
95  } else if (!_options.empty()) {
96  // A restricted template
97  indent(out, indent_level + 2);
98 
99  char delimiter = '[';
100  Options::const_iterator ri;
101  for (ri = _options.begin(); ri != _options.end(); ++ri) {
102  XFileTemplate *option = (*ri);
103  out << delimiter << " "
104  << option->get_name() << " <" << option->get_guid()
105  << ">";
106  delimiter = ',';
107  }
108  out << " ]\n";
109  }
110 
111  indent(out, indent_level)
112  << "}\n";
113 }
114 
115 /**
116  * Returns true if the node, particularly a template node, is structurally
117  * equivalent to the other node (which must be of the same type). This checks
118  * data element types, but does not compare data element names.
119  */
121 matches(const XFileNode *other) const {
122  if (!XFileNode::matches(other)) {
123  return false;
124  }
125 
126  // We *could* compare the openclosedoptions associated with the template,
127  // but since this is only used for validating the set of children for the
128  // instances of this template (which we don't even bother to do anyway), it
129  // doesn't seem to matter.
130  return true;
131 }
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
This is an implementation of the Windows GUID object, used everywhere as a world-unique identifier fo...
Definition: windowsGuid.h:26
A single node of an X file.
Definition: xFileNode.h:40
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.
Definition: xFileNode.cxx:219
virtual void clear()
Removes all children from the node, and otherwise resets it to its initial state.
Definition: xFileNode.cxx:209
virtual bool matches(const XFileNode *other) const
Returns true if the node, particularly a template node, is structurally equivalent to the other node ...
Definition: xFileNode.cxx:278
A template definition in the X file.
Definition: xFileTemplate.h:27
virtual bool matches(const XFileNode *other) const
Returns true if the node, particularly a template node, is structurally equivalent to the other node ...
virtual bool has_guid() const
Returns true if this node has a GUID associated.
virtual bool is_template_def() const
Returns true if this node represents the definition of some template.
virtual const WindowsGuid & get_guid() const
Returns the GUID associated with this template.
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.
virtual void clear()
Removes all children from the node, and otherwise resets it to its initial state.
bool get_open() const
Returns whether the template is considered "open" or not.
Definition: xFileTemplate.I:42
This represents the complete contents of an X file (file.x) in memory.
Definition: xFile.h:32
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.