Panda3D
 All Classes Functions Variables Enumerations
dcSwitchParameter.cxx
1 // Filename: dcSwitchParameter.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 "dcSwitchParameter.h"
16 #include "dcSwitch.h"
17 #include "hashGenerator.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: DCSwitchParameter::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 DCSwitchParameter::
25 DCSwitchParameter(const DCSwitch *dswitch) :
26  _dswitch(dswitch)
27 {
28  set_name(dswitch->get_name());
29 
30  _has_fixed_byte_size = true;
31  _fixed_byte_size = 0;
32  _has_fixed_structure = false;
33 
34  // The DCSwitch presents just one nested field initially, which is
35  // the key parameter. When we pack or unpack that, the DCPacker
36  // calls apply_switch(), which returns a new record that presents
37  // the remaining nested fields.
38  _has_nested_fields = true;
39  _num_nested_fields = 1;
40 
41  _pack_type = PT_switch;
42 
43  DCField *key_parameter = dswitch->get_key_parameter();
44  _has_fixed_byte_size = _has_fixed_byte_size && key_parameter->has_fixed_byte_size();
45  _has_range_limits = _has_range_limits || key_parameter->has_range_limits();
46  _has_default_value = _has_default_value || key_parameter->has_default_value();
47 
48  int num_cases = _dswitch->get_num_cases();
49  if (num_cases > 0) {
50  _fixed_byte_size = _dswitch->get_case(0)->get_fixed_byte_size();
51 
52  // Consider each case for fixed size, etc.
53  for (int i = 0; i < num_cases; i++) {
54  const DCSwitch::SwitchFields *fields =
55  (const DCSwitch::SwitchFields *)_dswitch->get_case(i);
56 
57  if (!fields->has_fixed_byte_size() ||
58  fields->get_fixed_byte_size() != _fixed_byte_size) {
59 
60  // Nope, we have a variable byte size.
61  _has_fixed_byte_size = false;
62  }
63 
64  _has_range_limits = _has_range_limits || fields->has_range_limits();
65  _has_default_value = _has_default_value || fields->_has_default_value;
66  }
67  }
68 
69  // Also consider the default case, if there is one.
70  const DCSwitch::SwitchFields *fields =
71  (DCSwitch::SwitchFields *)_dswitch->get_default_case();
72  if (fields != (DCSwitch::SwitchFields *)NULL) {
73  if (!fields->has_fixed_byte_size() ||
74  fields->get_fixed_byte_size() != _fixed_byte_size) {
75  _has_fixed_byte_size = false;
76  }
77 
78  _has_range_limits = _has_range_limits || fields->has_range_limits();
79  _has_default_value = _has_default_value || fields->_has_default_value;
80  }
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: DCSwitchParameter::Copy Constructor
85 // Access: Public
86 // Description:
87 ////////////////////////////////////////////////////////////////////
88 DCSwitchParameter::
89 DCSwitchParameter(const DCSwitchParameter &copy) :
90  DCParameter(copy),
91  _dswitch(copy._dswitch)
92 {
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: DCSwitchParameter::as_switch_parameter
97 // Access: Published, Virtual
98 // Description:
99 ////////////////////////////////////////////////////////////////////
100 DCSwitchParameter *DCSwitchParameter::
101 as_switch_parameter() {
102  return this;
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: DCSwitchParameter::as_switch_parameter
107 // Access: Published, Virtual
108 // Description:
109 ////////////////////////////////////////////////////////////////////
110 const DCSwitchParameter *DCSwitchParameter::
111 as_switch_parameter() const {
112  return this;
113 }
114 
115 ////////////////////////////////////////////////////////////////////
116 // Function: DCSwitchParameter::make_copy
117 // Access: Published, Virtual
118 // Description:
119 ////////////////////////////////////////////////////////////////////
120 DCParameter *DCSwitchParameter::
121 make_copy() const {
122  return new DCSwitchParameter(*this);
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: DCSwitchParameter::is_valid
127 // Access: Published, Virtual
128 // Description: Returns false if the type is an invalid type
129 // (e.g. declared from an undefined typedef), true if
130 // it is valid.
131 ////////////////////////////////////////////////////////////////////
133 is_valid() const {
134  return true; //_dswitch->is_valid();
135 }
136 
137 ////////////////////////////////////////////////////////////////////
138 // Function: DCSwitchParameter::get_switch
139 // Access: Published
140 // Description: Returns the switch object this parameter represents.
141 ////////////////////////////////////////////////////////////////////
143 get_switch() const {
144  return _dswitch;
145 }
146 
147 ////////////////////////////////////////////////////////////////////
148 // Function: DCSwitchParameter::get_nested_field
149 // Access: Public, Virtual
150 // Description: Returns the DCPackerInterface object that represents
151 // the nth nested field. This may return NULL if there
152 // is no such field (but it shouldn't do this if n is in
153 // the range 0 <= n < get_num_nested_fields()).
154 ////////////////////////////////////////////////////////////////////
156 get_nested_field(int) const {
157  return _dswitch->get_key_parameter();
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: DCSwitchParameter::apply_switch
162 // Access: Public
163 // Description: Returns the DCPackerInterface that presents the
164 // alternative fields for the case indicated by the
165 // given packed value string, or NULL if the value
166 // string does not match one of the expected cases.
167 ////////////////////////////////////////////////////////////////////
169 apply_switch(const char *value_data, size_t length) const {
170  return _dswitch->apply_switch(value_data, length);
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: DCSwitchParameter::output_instance
175 // Access: Public, Virtual
176 // Description: Formats the parameter in the C++-like dc syntax as a
177 // typename and identifier.
178 ////////////////////////////////////////////////////////////////////
180 output_instance(ostream &out, bool brief, const string &prename,
181  const string &name, const string &postname) const {
182  if (get_typedef() != (DCTypedef *)NULL) {
183  output_typedef_name(out, brief, prename, name, postname);
184 
185  } else {
186  _dswitch->output_instance(out, brief, prename, name, postname);
187  }
188 }
189 
190 ////////////////////////////////////////////////////////////////////
191 // Function: DCSwitchParameter::write_instance
192 // Access: Public, Virtual
193 // Description: Formats the parameter in the C++-like dc syntax as a
194 // typename and identifier.
195 ////////////////////////////////////////////////////////////////////
197 write_instance(ostream &out, bool brief, int indent_level,
198  const string &prename, const string &name,
199  const string &postname) const {
200  if (get_typedef() != (DCTypedef *)NULL) {
201  write_typedef_name(out, brief, indent_level, prename, name, postname);
202 
203  } else {
204  _dswitch->write_instance(out, brief, indent_level, prename, name, postname);
205  }
206 }
207 
208 ////////////////////////////////////////////////////////////////////
209 // Function: DCSwitchParameter::generate_hash
210 // Access: Public, Virtual
211 // Description: Accumulates the properties of this type into the
212 // hash.
213 ////////////////////////////////////////////////////////////////////
215 generate_hash(HashGenerator &hashgen) const {
217  _dswitch->generate_hash(hashgen);
218 }
219 
220 ////////////////////////////////////////////////////////////////////
221 // Function: DCSwitchParameter::pack_default_value
222 // Access: Public, Virtual
223 // Description: Packs the switchParameter's specified default value (or a
224 // sensible default if no value is specified) into the
225 // stream. Returns true if the default value is packed,
226 // false if the switchParameter doesn't know how to pack its
227 // default value.
228 ////////////////////////////////////////////////////////////////////
230 pack_default_value(DCPackData &pack_data, bool &pack_error) const {
231  if (has_default_value()) {
232  return DCField::pack_default_value(pack_data, pack_error);
233  }
234 
235  return _dswitch->pack_default_value(pack_data, pack_error);
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: DCSwitchParameter::do_check_match
240 // Access: Protected, Virtual
241 // Description: Returns true if the other interface is bitwise the
242 // same as this one--that is, a uint32 only matches a
243 // uint32, etc. Names of components, and range limits,
244 // are not compared.
245 ////////////////////////////////////////////////////////////////////
246 bool DCSwitchParameter::
247 do_check_match(const DCPackerInterface *other) const {
248  return other->do_check_match_switch_parameter(this);
249 }
250 
251 ////////////////////////////////////////////////////////////////////
252 // Function: DCSwitchParameter::do_check_match_switch_parameter
253 // Access: Protected, Virtual
254 // Description: Returns true if this field matches the indicated
255 // switch parameter, false otherwise.
256 ////////////////////////////////////////////////////////////////////
257 bool DCSwitchParameter::
258 do_check_match_switch_parameter(const DCSwitchParameter *other) const {
259  return _dswitch->do_check_match_switch(other->_dswitch);
260 }
This represents a single typedef declaration in the dc file.
Definition: dcTypedef.h:29
This is a block of data that receives the results of DCPacker.
Definition: dcPackData.h:25
virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const
Packs the field&#39;s specified default value (or a sensible default if no value is specified) into the s...
Definition: dcField.cxx:548
A single field of a Distributed Class, either atomic or molecular.
Definition: dcField.h:40
DCField * get_key_parameter() const
Returns the key parameter on which the switch is based.
Definition: dcSwitch.cxx:106
bool has_default_value() const
Returns true if a default value has been explicitly established for this field, false otherwise...
Definition: dcField.I:46
virtual bool do_check_match_switch_parameter(const DCSwitchParameter *other) const
Returns true if this field matches the indicated switch parameter, false otherwise.
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...
This represents a switch statement, which can appear inside a class body and represents two or more a...
Definition: dcSwitch.h:33
This represents a switch object used as a parameter itself, which packs the appropriate fields of the...
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...
virtual bool is_valid() const
Returns false if the type is an invalid type (e.g.
virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const
Packs the switchParameter&#39;s specified default value (or a sensible default if no value is specified) ...
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.
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this type into the hash.
Represents the type specification for a single parameter within a field specification.
Definition: dcParameter.h:39
virtual void write_instance(ostream &out, bool brief, int indent_level, const string &prename, const string &name, const string &postname) const
Formats the parameter in the C++-like dc syntax as a typename and identifier.
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this type into the hash.
This class generates an arbitrary hash number from a sequence of ints.
Definition: hashGenerator.h:26
const string & get_name() const
Returns the name of this switch.
Definition: dcSwitch.cxx:93
const DCPackerInterface * apply_switch(const char *value_data, size_t length) const
Returns the DCPackerInterface that presents the alternative fields for the case indicated by the give...
const DCSwitch * get_switch() const
Returns the switch object this parameter represents.
This defines the internal interface for packing values into a DCField.
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...