Panda3D
dcClassParameter.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 dcClassParameter.cxx
10  * @author drose
11  * @date 2004-06-18
12  */
13 
14 #include "dcClassParameter.h"
15 #include "dcClass.h"
16 #include "dcArrayParameter.h"
17 #include "hashGenerator.h"
18 
19 /**
20  *
21  */
22 DCClassParameter::
23 DCClassParameter(const DCClass *dclass) :
24  _dclass(dclass)
25 {
26  set_name(dclass->get_name());
27 
28  int num_fields = _dclass->get_num_inherited_fields();
29 
30  _has_nested_fields = true;
31  _pack_type = PT_class;
32 
33  if (_dclass->has_constructor()) {
34  DCField *field = _dclass->get_constructor();
35  _nested_fields.push_back(field);
36  _has_default_value = _has_default_value || field->has_default_value();
37  }
38  int i;
39  for (i = 0 ; i < num_fields; i++) {
40  DCField *field = _dclass->get_inherited_field(i);
41  if (!field->as_molecular_field()) {
42  _nested_fields.push_back(field);
43  _has_default_value = _has_default_value || field->has_default_value();
44  }
45  }
46  _num_nested_fields = _nested_fields.size();
47 
48  // If all of the nested fields have a fixed byte size, then so does the
49  // class (and its byte size is the sum of all of the nested fields).
50  _has_fixed_byte_size = true;
51  _fixed_byte_size = 0;
52  _has_fixed_structure = true;
53  for (i = 0; i < _num_nested_fields; i++) {
55  _has_fixed_byte_size = _has_fixed_byte_size && field->has_fixed_byte_size();
56  _fixed_byte_size += field->get_fixed_byte_size();
57  _has_fixed_structure = _has_fixed_structure && field->has_fixed_structure();
58 
59  _has_range_limits = _has_range_limits || field->has_range_limits();
60  }
61 }
62 
63 /**
64  *
65  */
66 DCClassParameter::
67 DCClassParameter(const DCClassParameter &copy) :
68  DCParameter(copy),
69  _nested_fields(copy._nested_fields),
70  _dclass(copy._dclass)
71 {
72 }
73 
74 /**
75  *
76  */
77 DCClassParameter *DCClassParameter::
78 as_class_parameter() {
79  return this;
80 }
81 
82 /**
83  *
84  */
85 const DCClassParameter *DCClassParameter::
86 as_class_parameter() const {
87  return this;
88 }
89 
90 /**
91  *
92  */
93 DCParameter *DCClassParameter::
94 make_copy() const {
95  return new DCClassParameter(*this);
96 }
97 
98 /**
99  * Returns false if the type is an invalid type (e.g. declared from an
100  * undefined typedef), true if it is valid.
101  */
103 is_valid() const {
104  return !_dclass->is_bogus_class();
105 }
106 
107 /**
108  * Returns the class object this parameter represents.
109  */
111 get_class() const {
112  return _dclass;
113 }
114 
115 /**
116  * Returns the DCPackerInterface object that represents the nth nested field.
117  * This may return NULL if there is no such field (but it shouldn't do this if
118  * n is in the range 0 <= n < get_num_nested_fields()).
119  */
121 get_nested_field(int n) const {
122  nassertr(n >= 0 && n < (int)_nested_fields.size(), nullptr);
123  return _nested_fields[n];
124 }
125 
126 /**
127  * Formats the parameter in the C++-like dc syntax as a typename and
128  * identifier.
129  */
131 output_instance(std::ostream &out, bool brief, const std::string &prename,
132  const std::string &name, const std::string &postname) const {
133  if (get_typedef() != nullptr) {
134  output_typedef_name(out, brief, prename, name, postname);
135 
136  } else {
137  _dclass->output_instance(out, brief, prename, name, postname);
138  }
139 }
140 
141 /**
142  * Accumulates the properties of this type into the hash.
143  */
145 generate_hash(HashGenerator &hashgen) const {
147  _dclass->generate_hash(hashgen);
148 }
149 
150 /**
151  * Returns true if the other interface is bitwise the same as this one--that
152  * is, a uint32 only matches a uint32, etc. Names of components, and range
153  * limits, are not compared.
154  */
155 bool DCClassParameter::
156 do_check_match(const DCPackerInterface *other) const {
157  return other->do_check_match_class_parameter(this);
158 }
159 
160 /**
161  * Returns true if this field matches the indicated class parameter, false
162  * otherwise.
163  */
164 bool DCClassParameter::
165 do_check_match_class_parameter(const DCClassParameter *other) const {
166  if (_nested_fields.size() != other->_nested_fields.size()) {
167  return false;
168  }
169  for (size_t i = 0; i < _nested_fields.size(); i++) {
170  if (!_nested_fields[i]->check_match(other->_nested_fields[i])) {
171  return false;
172  }
173  }
174 
175  return true;
176 }
177 
178 /**
179  * Returns true if this field matches the indicated array parameter, false
180  * otherwise.
181  */
182 bool DCClassParameter::
183 do_check_match_array_parameter(const DCArrayParameter *other) const {
184  if ((int)_nested_fields.size() != other->get_array_size()) {
185  // We can only match a fixed-size array whose size happens to exactly
186  // match our number of fields.
187  return false;
188  }
189 
190  const DCPackerInterface *element_type = other->get_element_type();
191  for (size_t i = 0; i < _nested_fields.size(); i++) {
192  if (!_nested_fields[i]->check_match(element_type)) {
193  return false;
194  }
195  }
196 
197  return true;
198 }
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this type into the hash.
virtual void output_instance(std::ostream &out, bool brief, const std::string &prename, const std::string &name, const std::string &postname) const
Formats the parameter in the C++-like dc syntax as a typename and identifier.
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:1135
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:37
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this type into the hash.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:44
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
const DCClass * get_class() const
Returns the class object this parameter represents.
virtual void set_name(const std::string &name)
Sets the name of this field.
Definition: dcField.cxx:502
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:130
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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(std::ostream &out, bool brief, const std::string &prename, const std::string &name, const std::string &postname) const
Generates a parseable description of the object to the indicated output stream.
Definition: dcClass.cxx:1089
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...
Represents the type specification for a single parameter within a field specification.
Definition: dcParameter.h:35
This represents an array of some other kind of object, meaning this parameter type accepts an arbitra...
void output_typedef_name(std::ostream &out, bool brief, const std::string &prename, const std::string &name, const std::string &postname) const
Formats the instance like output_instance, but uses the typedef name instead.
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:36
This class generates an arbitrary hash number from a sequence of ints.
Definition: hashGenerator.h:24
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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 ...
const std::string & get_name() const
Returns the name of this class.
Definition: dcClass.I:26
DCParameter * get_element_type() const
Returns the type of the individual elements of this array.
bool is_bogus_class() const
Returns true if the class has been flagged as a bogus class.
Definition: dcClass.I:55
This defines the internal interface for packing values into a DCField.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.