Panda3D
dcClassParameter.cxx
1 // Filename: dcClassParameter.cxx
2 // Created by: drose (18Jun04)
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 "dcClassParameter.h"
16 #include "dcClass.h"
17 #include "dcArrayParameter.h"
18 #include "hashGenerator.h"
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: DCClassParameter::Constructor
22 // Access: Public
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 DCClassParameter::
26 DCClassParameter(const DCClass *dclass) :
27  _dclass(dclass)
28 {
29  set_name(dclass->get_name());
30 
31  int num_fields = _dclass->get_num_inherited_fields();
32 
33  _has_nested_fields = true;
34  _pack_type = PT_class;
35 
36  if (_dclass->has_constructor()) {
37  DCField *field = _dclass->get_constructor();
38  _nested_fields.push_back(field);
39  _has_default_value = _has_default_value || field->has_default_value();
40  }
41  int i;
42  for (i = 0 ; i < num_fields; i++) {
43  DCField *field = _dclass->get_inherited_field(i);
44  if (!field->as_molecular_field()) {
45  _nested_fields.push_back(field);
46  _has_default_value = _has_default_value || field->has_default_value();
47  }
48  }
49  _num_nested_fields = _nested_fields.size();
50 
51  // If all of the nested fields have a fixed byte size, then so does
52  // the class (and its byte size is the sum of all of the nested
53  // fields).
54  _has_fixed_byte_size = true;
55  _fixed_byte_size = 0;
56  _has_fixed_structure = true;
57  for (i = 0; i < _num_nested_fields; i++) {
59  _has_fixed_byte_size = _has_fixed_byte_size && field->has_fixed_byte_size();
60  _fixed_byte_size += field->get_fixed_byte_size();
61  _has_fixed_structure = _has_fixed_structure && field->has_fixed_structure();
62 
63  _has_range_limits = _has_range_limits || field->has_range_limits();
64  }
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: DCClassParameter::Copy Constructor
69 // Access: Public
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 DCClassParameter::
73 DCClassParameter(const DCClassParameter &copy) :
74  DCParameter(copy),
75  _nested_fields(copy._nested_fields),
76  _dclass(copy._dclass)
77 {
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: DCClassParameter::as_class_parameter
82 // Access: Published, Virtual
83 // Description:
84 ////////////////////////////////////////////////////////////////////
85 DCClassParameter *DCClassParameter::
86 as_class_parameter() {
87  return this;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: DCClassParameter::as_class_parameter
92 // Access: Published, Virtual
93 // Description:
94 ////////////////////////////////////////////////////////////////////
95 const DCClassParameter *DCClassParameter::
96 as_class_parameter() const {
97  return this;
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: DCClassParameter::make_copy
102 // Access: Published, Virtual
103 // Description:
104 ////////////////////////////////////////////////////////////////////
105 DCParameter *DCClassParameter::
106 make_copy() const {
107  return new DCClassParameter(*this);
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: DCClassParameter::is_valid
112 // Access: Published, Virtual
113 // Description: Returns false if the type is an invalid type
114 // (e.g. declared from an undefined typedef), true if
115 // it is valid.
116 ////////////////////////////////////////////////////////////////////
118 is_valid() const {
119  return !_dclass->is_bogus_class();
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: DCClassParameter::get_class
124 // Access: Published
125 // Description: Returns the class object this parameter represents.
126 ////////////////////////////////////////////////////////////////////
128 get_class() const {
129  return _dclass;
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: DCClassParameter::get_nested_field
134 // Access: Public, Virtual
135 // Description: Returns the DCPackerInterface object that represents
136 // the nth nested field. This may return NULL if there
137 // is no such field (but it shouldn't do this if n is in
138 // the range 0 <= n < get_num_nested_fields()).
139 ////////////////////////////////////////////////////////////////////
141 get_nested_field(int n) const {
142  nassertr(n >= 0 && n < (int)_nested_fields.size(), NULL);
143  return _nested_fields[n];
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: DCClassParameter::output_instance
148 // Access: Public, Virtual
149 // Description: Formats the parameter in the C++-like dc syntax as a
150 // typename and identifier.
151 ////////////////////////////////////////////////////////////////////
153 output_instance(ostream &out, bool brief, const string &prename,
154  const string &name, const string &postname) const {
155  if (get_typedef() != (DCTypedef *)NULL) {
156  output_typedef_name(out, brief, prename, name, postname);
157 
158  } else {
159  _dclass->output_instance(out, brief, prename, name, postname);
160  }
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function: DCClassParameter::generate_hash
165 // Access: Public, Virtual
166 // Description: Accumulates the properties of this type into the
167 // hash.
168 ////////////////////////////////////////////////////////////////////
170 generate_hash(HashGenerator &hashgen) const {
172  _dclass->generate_hash(hashgen);
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: DCClassParameter::do_check_match
177 // Access: Protected, Virtual
178 // Description: Returns true if the other interface is bitwise the
179 // same as this one--that is, a uint32 only matches a
180 // uint32, etc. Names of components, and range limits,
181 // are not compared.
182 ////////////////////////////////////////////////////////////////////
183 bool DCClassParameter::
184 do_check_match(const DCPackerInterface *other) const {
185  return other->do_check_match_class_parameter(this);
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: DCClassParameter::do_check_match_class_parameter
190 // Access: Protected, Virtual
191 // Description: Returns true if this field matches the indicated
192 // class parameter, false otherwise.
193 ////////////////////////////////////////////////////////////////////
194 bool DCClassParameter::
195 do_check_match_class_parameter(const DCClassParameter *other) const {
196  if (_nested_fields.size() != other->_nested_fields.size()) {
197  return false;
198  }
199  for (size_t i = 0; i < _nested_fields.size(); i++) {
200  if (!_nested_fields[i]->check_match(other->_nested_fields[i])) {
201  return false;
202  }
203  }
204 
205  return true;
206 }
207 
208 ////////////////////////////////////////////////////////////////////
209 // Function: DCClassParameter::do_check_match_array_parameter
210 // Access: Protected, Virtual
211 // Description: Returns true if this field matches the indicated
212 // array parameter, false otherwise.
213 ////////////////////////////////////////////////////////////////////
214 bool DCClassParameter::
215 do_check_match_array_parameter(const DCArrayParameter *other) const {
216  if ((int)_nested_fields.size() != other->get_array_size()) {
217  // We can only match a fixed-size array whose size happens to
218  // exactly match our number of fields.
219  return false;
220  }
221 
222  const DCPackerInterface *element_type = other->get_element_type();
223  for (size_t i = 0; i < _nested_fields.size(); i++) {
224  if (!_nested_fields[i]->check_match(element_type)) {
225  return false;
226  }
227  }
228 
229  return true;
230 }
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this type into the hash.
This represents a single typedef declaration in the dc file.
Definition: dcTypedef.h:29
void output_typedef_name(ostream &out, bool brief, const string &prename, const string &name, const string &postname) const
Formats the instance like output_instance, but uses the typedef name instead.
virtual bool is_valid() const
Returns false if the type is an invalid type (e.g.
void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this class into the hash.
Definition: dcClass.cxx:1336
int get_array_size() const
Returns the fixed number of elements in this array, or -1 if the array may contain a variable number ...
bool has_fixed_structure() const
Returns true if this field type always has the same structure regardless of the data in the stream...
This represents a class (or struct) object used as a parameter itself.
A single field of a Distributed Class, either atomic or molecular.
Definition: dcField.h:40
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this type into the hash.
virtual void output_instance(ostream &out, bool brief, const string &prename, const string &name, const string &postname) const
Formats the parameter in the C++-like dc syntax as a typename and identifier.
bool has_fixed_byte_size() const
Returns true if this field type always packs to the same number of bytes, false if it is variable...
Defines a particular DistributedClass as read from an input .dc file.
Definition: dcClass.h:47
const DCClass * get_class() const
Returns the class object this parameter represents.
virtual DCMolecularField * as_molecular_field()
Returns the same field pointer converted to a molecular field pointer, if this is in fact a molecular...
Definition: dcField.cxx:148
size_t get_fixed_byte_size() const
If has_fixed_byte_size() returns true, this returns the number of bytes this field type will use...
void output_instance(ostream &out, bool brief, const string &prename, const string &name, const string &postname) const
Generates a parseable description of the object to the indicated output stream.
Definition: dcClass.cxx:1287
bool has_range_limits() const
Returns true if this field, or any sub-field of this field, has a limit imposed in the DC file on its...
DCField * get_inherited_field(int n) const
Returns the nth field field in the class and all of its ancestors.
Definition: dcClass.cxx:324
Represents the type specification for a single parameter within a field specification.
Definition: dcParameter.h:39
This represents an array of some other kind of object, meaning this parameter type accepts an arbitra...
bool check_match(const DCPackerInterface *other) const
Returns true if the other interface is bitwise the same as this one–that is, a uint32 only matches a...
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
bool has_default_value() const
Returns true if a default value has been explicitly established for this field, false otherwise...
Definition: dcField.I:46
This class generates an arbitrary hash number from a sequence of ints.
Definition: hashGenerator.h:26
DCField * get_constructor() const
Returns the constructor method for this class if it is defined, or NULL if the class uses the default...
Definition: dcClass.cxx:178
int get_num_inherited_fields() const
Returns the total number of field fields defined in this class and all ancestor classes.
Definition: dcClass.cxx:286
virtual bool do_check_match_class_parameter(const DCClassParameter *other) const
Returns true if this field matches the indicated class parameter, false otherwise.
const DCTypedef * get_typedef() const
If this type has been referenced from a typedef, returns the DCTypedef instance, or NULL if the type ...
virtual void set_name(const string &name)
Sets the name of this field.
Definition: dcField.cxx:570
bool has_constructor() const
Returns true if this class has a constructor method, false if it just uses the default constructor...
Definition: dcClass.cxx:166
DCParameter * get_element_type() const
Returns the type of the individual elements of this array.
const string & get_name() const
Returns the name of this class.
Definition: dcClass.I:32
bool is_bogus_class() const
Returns true if the class has been flagged as a bogus class.
Definition: dcClass.I:70
This defines the internal interface for packing values into a DCField.