Panda3D
dcTypedef.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 dcTypedef.cxx
10  * @author drose
11  * @date 2004-06-17
12  */
13 
14 #include "dcTypedef.h"
15 #include "dcParameter.h"
16 #include "dcSimpleParameter.h"
17 #include "dcindent.h"
18 
19 using std::string;
20 
21 /**
22  * The DCTypedef object becomes the owner of the supplied parameter pointer
23  * and will delete it upon destruction.
24  */
26 DCTypedef(DCParameter *parameter, bool implicit) :
27  _parameter(parameter),
28  _bogus_typedef(false),
29  _implicit_typedef(implicit),
30  _number(-1)
31 {
32 }
33 
34 /**
35  * Creates a bogus typedef reference.
36  */
38 DCTypedef(const string &name) :
39  _parameter(new DCSimpleParameter(ST_invalid)),
40  _bogus_typedef(true),
41  _implicit_typedef(false),
42  _number(-1)
43 {
44  _parameter->set_name(name);
45 }
46 
47 /**
48  *
49  */
50 DCTypedef::
51 ~DCTypedef() {
52  delete _parameter;
53 }
54 
55 /**
56  * Returns a unique index number associated with this typedef definition.
57  * This is defined implicitly when the .dc file(s) are read.
58  */
59 int DCTypedef::
60 get_number() const {
61  return _number;
62 }
63 
64 /**
65  * Returns the name of this typedef.
66  */
67 const string &DCTypedef::
68 get_name() const {
69  return _parameter->get_name();
70 }
71 
72 /**
73  * Returns a brief decription of the typedef, useful for human consumption.
74  */
75 string DCTypedef::
76 get_description() const {
77  std::ostringstream strm;
78  _parameter->output(strm, true);
79  return strm.str();
80 }
81 
82 /**
83  * Returns true if the typedef has been flagged as a bogus typedef. This is
84  * set for typedefs that are generated by the parser as placeholder for
85  * missing typedefs, as when reading a partial file; it should not occur in a
86  * normal valid dc file.
87  */
88 bool DCTypedef::
90  return _bogus_typedef;
91 }
92 
93 /**
94  * Returns true if the typedef has been flagged as an implicit typedef,
95  * meaning it was created for a DCClass that was referenced inline as a type.
96  */
97 bool DCTypedef::
99  return _implicit_typedef;
100 }
101 
102 /**
103  * Returns a newly-allocated DCParameter object that uses the same type as
104  * that named by the typedef.
105  */
108  DCParameter *new_parameter = _parameter->make_copy();
109  new_parameter->set_name(string());
110  new_parameter->set_typedef(this);
111  return new_parameter;
112 }
113 
114 /**
115  * Assigns the unique number to this typedef. This is normally called only by
116  * the DCFile interface as the typedef is added.
117  */
118 void DCTypedef::
119 set_number(int number) {
120  _number = number;
121 }
122 
123 /**
124  * Write a string representation of this instance to <out>.
125  */
126 void DCTypedef::
127 output(std::ostream &out, bool brief) const {
128  out << "typedef ";
129  _parameter->output(out, false);
130 }
131 
132 /**
133  *
134  */
135 void DCTypedef::
136 write(std::ostream &out, bool brief, int indent_level) const {
137  indent(out, indent_level)
138  << "typedef ";
139 
140  // We need to preserve the parameter name in the typedef (this is the
141  // typedef name); hence, we pass brief = false to output().
142  _parameter->output(out, false);
143  out << ";";
144 
145  if (!brief) {
146  out << " // typedef " << _number;
147  }
148  out << "\n";
149 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_number(int number)
Assigns the unique number to this typedef.
Definition: dcTypedef.cxx:119
DCTypedef(DCParameter *parameter, bool implicit=false)
The DCTypedef object becomes the owner of the supplied parameter pointer and will delete it upon dest...
Definition: dcTypedef.cxx:26
const std::string & get_name() const
Returns the name of this field, or empty string if the field is unnamed.
std::string get_description() const
Returns a brief decription of the typedef, useful for human consumption.
Definition: dcTypedef.cxx:76
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the most fundamental kind of parameter type: a single number or string, one of the DCSubatomi...
void set_typedef(const DCTypedef *dtypedef)
Records the DCTypedef object that generated this parameter.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DCParameter * make_new_parameter() const
Returns a newly-allocated DCParameter object that uses the same type as that named by the typedef.
Definition: dcTypedef.cxx:107
virtual void set_name(const std::string &name)
Sets the name of this field.
Definition: dcField.cxx:502
Represents the type specification for a single parameter within a field specification.
Definition: dcParameter.h:35
bool is_implicit_typedef() const
Returns true if the typedef has been flagged as an implicit typedef, meaning it was created for a DCC...
Definition: dcTypedef.cxx:98
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
bool is_bogus_typedef() const
Returns true if the typedef has been flagged as a bogus typedef.
Definition: dcTypedef.cxx:89
const std::string & get_name() const
Returns the name of this typedef.
Definition: dcTypedef.cxx:68
int get_number() const
Returns a unique index number associated with this typedef definition.
Definition: dcTypedef.cxx:60
virtual void output(std::ostream &out, bool brief) const
Write a string representation of this instance to <out>.
Definition: dcTypedef.cxx:127
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.