Panda3D
dcTypedef.cxx
1 // Filename: dcTypedef.cxx
2 // Created by: drose (17Jun04)
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 "dcTypedef.h"
16 #include "dcParameter.h"
17 #include "dcSimpleParameter.h"
18 #include "dcindent.h"
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: DCTypedef::Constructor
22 // Access: Public
23 // Description: The DCTypedef object becomes the owner of the
24 // supplied parameter pointer and will delete it upon
25 // destruction.
26 ////////////////////////////////////////////////////////////////////
28 DCTypedef(DCParameter *parameter, bool implicit) :
29  _parameter(parameter),
30  _bogus_typedef(false),
31  _implicit_typedef(implicit),
32  _number(-1)
33 {
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: DCTypedef::Constructor
38 // Access: Public
39 // Description: Creates a bogus typedef reference.
40 ////////////////////////////////////////////////////////////////////
42 DCTypedef(const string &name) :
43  _parameter(new DCSimpleParameter(ST_invalid)),
44  _bogus_typedef(true),
45  _implicit_typedef(false),
46  _number(-1)
47 {
48  _parameter->set_name(name);
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: DCTypedef::Destructor
53 // Access: Public, Virtual
54 // Description:
55 ////////////////////////////////////////////////////////////////////
56 DCTypedef::
57 ~DCTypedef() {
58  delete _parameter;
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: DCTypedef::get_number
63 // Access: Published
64 // Description: Returns a unique index number associated with this
65 // typedef definition. This is defined implicitly when
66 // the .dc file(s) are read.
67 ////////////////////////////////////////////////////////////////////
68 int DCTypedef::
69 get_number() const {
70  return _number;
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: DCTypedef::get_name
75 // Access: Published
76 // Description: Returns the name of this typedef.
77 ////////////////////////////////////////////////////////////////////
78 const string &DCTypedef::
79 get_name() const {
80  return _parameter->get_name();
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: DCTypedef::get_description
85 // Access: Published
86 // Description: Returns a brief decription of the typedef, useful for
87 // human consumption.
88 ////////////////////////////////////////////////////////////////////
89 string DCTypedef::
90 get_description() const {
91  ostringstream strm;
92  _parameter->output(strm, true);
93  return strm.str();
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: DCTypedef::is_bogus_typedef
98 // Access: Public
99 // Description: Returns true if the typedef has been flagged as a bogus
100 // typedef. This is set for typedefs that are generated by
101 // the parser as placeholder for missing typedefs, as
102 // when reading a partial file; it should not occur in a
103 // normal valid dc file.
104 ////////////////////////////////////////////////////////////////////
105 bool DCTypedef::
107  return _bogus_typedef;
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: DCTypedef::is_implicit_typedef
112 // Access: Public
113 // Description: Returns true if the typedef has been flagged as an
114 // implicit typedef, meaning it was created for a
115 // DCClass that was referenced inline as a type.
116 ////////////////////////////////////////////////////////////////////
117 bool DCTypedef::
119  return _implicit_typedef;
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: DCTypedef::make_new_parameter
124 // Access: Public
125 // Description: Returns a newly-allocated DCParameter object that
126 // uses the same type as that named by the typedef.
127 ////////////////////////////////////////////////////////////////////
130  DCParameter *new_parameter = _parameter->make_copy();
131  new_parameter->set_name(string());
132  new_parameter->set_typedef(this);
133  return new_parameter;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: DCTypedef::set_number
138 // Access: Public
139 // Description: Assigns the unique number to this typedef. This is
140 // normally called only by the DCFile interface as the
141 // typedef is added.
142 ////////////////////////////////////////////////////////////////////
143 void DCTypedef::
144 set_number(int number) {
145  _number = number;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function : DCTypedef::output
150 // Access : Public, Virtual
151 // Description : Write a string representation of this instance to
152 // <out>.
153 ////////////////////////////////////////////////////////////////////
154 void DCTypedef::
155 output(ostream &out, bool brief) const {
156  out << "typedef ";
157  _parameter->output(out, false);
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: DCTypedef::write
162 // Access: Public, Virtual
163 // Description:
164 ////////////////////////////////////////////////////////////////////
165 void DCTypedef::
166 write(ostream &out, bool brief, int indent_level) const {
167  indent(out, indent_level)
168  << "typedef ";
169 
170  // We need to preserve the parameter name in the typedef (this is
171  // the typedef name); hence, we pass brief = false to output().
172  _parameter->output(out, false);
173  out << ";";
174 
175  if (!brief) {
176  out << " // typedef " << _number;
177  }
178  out << "\n";
179 }
void set_number(int number)
Assigns the unique number to this typedef.
Definition: dcTypedef.cxx:144
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:28
string get_description() const
Returns a brief decription of the typedef, useful for human consumption.
Definition: dcTypedef.cxx:90
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.
const string & get_name() const
Returns the name of this field, or empty string if the field is unnamed.
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:129
Represents the type specification for a single parameter within a field specification.
Definition: dcParameter.h:39
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:118
bool is_bogus_typedef() const
Returns true if the typedef has been flagged as a bogus typedef.
Definition: dcTypedef.cxx:106
const string & get_name() const
Returns the name of this typedef.
Definition: dcTypedef.cxx:79
int get_number() const
Returns a unique index number associated with this typedef definition.
Definition: dcTypedef.cxx:69
virtual void set_name(const string &name)
Sets the name of this field.
Definition: dcField.cxx:570
virtual void output(ostream &out, bool brief) const
Write a string representation of this instance to <out>.
Definition: dcTypedef.cxx:155