Panda3D
dcMolecularField.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 dcMolecularField.cxx
10  * @author drose
11  * @date 2000-10-05
12  */
13 
14 #include "dcMolecularField.h"
15 #include "dcAtomicField.h"
16 #include "hashGenerator.h"
17 #include "dcindent.h"
18 
19 
20 
21 /**
22  *
23  */
24 DCMolecularField::
25 DCMolecularField(const std::string &name, DCClass *dclass) : DCField(name, dclass) {
26  _got_keywords = false;
27 }
28 
29 /**
30  * Returns the same field pointer converted to a molecular field pointer, if
31  * this is in fact a molecular field; otherwise, returns NULL.
32  */
35  return this;
36 }
37 
38 /**
39  * Returns the same field pointer converted to a molecular field pointer, if
40  * this is in fact a molecular field; otherwise, returns NULL.
41  */
43 as_molecular_field() const {
44  return this;
45 }
46 
47 /**
48  * Returns the number of atomic fields that make up this molecular field.
49  */
51 get_num_atomics() const {
52  return _fields.size();
53 }
54 
55 /**
56  * Returns the nth atomic field that makes up this molecular field. This may
57  * or may not be a field of this particular class; it might be defined in a
58  * parent class.
59  */
61 get_atomic(int n) const {
62  nassertr(n >= 0 && n < (int)_fields.size(), nullptr);
63  return _fields[n];
64 }
65 
66 /**
67  * Adds the indicated atomic field to the end of the list of atomic fields
68  * that make up the molecular field. This is normally called only during
69  * parsing of the dc file. The atomic field should be fully defined by this
70  * point; you should not modify the atomic field (e.g. by adding more
71  * elements) after adding it to a molecular field.
72  */
74 add_atomic(DCAtomicField *atomic) {
75  if (!atomic->is_bogus_field()) {
76  if (!_got_keywords) {
77  // The first non-bogus atomic field determines our keywords.
78  copy_keywords(*atomic);
79  _got_keywords = true;
80  }
81  }
82  _fields.push_back(atomic);
83 
84  int num_atomic_fields = atomic->get_num_nested_fields();
85  for (int i = 0; i < num_atomic_fields; i++) {
86  _nested_fields.push_back(atomic->get_nested_field(i));
87  }
88 
89  _num_nested_fields = _nested_fields.size();
90 
91  // See if we still have a fixed byte size.
92  if (_has_fixed_byte_size) {
93  _has_fixed_byte_size = atomic->has_fixed_byte_size();
94  _fixed_byte_size += atomic->get_fixed_byte_size();
95  }
96  if (_has_fixed_structure) {
97  _has_fixed_structure = atomic->has_fixed_structure();
98  }
99  if (!_has_range_limits) {
100  _has_range_limits = atomic->has_range_limits();
101  }
102  if (!_has_default_value) {
103  _has_default_value = atomic->has_default_value();
104  }
105  _default_value_stale = true;
106 }
107 
108 /**
109  *
110  */
111 void DCMolecularField::
112 output(std::ostream &out, bool brief) const {
113  out << _name;
114 
115  if (!_fields.empty()) {
116  Fields::const_iterator fi = _fields.begin();
117  out << " : " << (*fi)->get_name();
118  ++fi;
119  while (fi != _fields.end()) {
120  out << ", " << (*fi)->get_name();
121  ++fi;
122  }
123  }
124 
125  out << ";";
126 }
127 
128 /**
129  * Generates a parseable description of the object to the indicated output
130  * stream.
131  */
133 write(std::ostream &out, bool brief, int indent_level) const {
134  indent(out, indent_level);
135  output(out, brief);
136  if (!brief) {
137  out << " // field " << _number;
138  }
139  out << "\n";
140 }
141 
142 
143 /**
144  * Accumulates the properties of this field into the hash.
145  */
147 generate_hash(HashGenerator &hashgen) const {
148  DCField::generate_hash(hashgen);
149 
150  hashgen.add_int(_fields.size());
151  Fields::const_iterator fi;
152  for (fi = _fields.begin(); fi != _fields.end(); ++fi) {
153  (*fi)->generate_hash(hashgen);
154  }
155 }
156 
157 /**
158  * Returns the DCPackerInterface object that represents the nth nested field.
159  * This may return NULL if there is no such field (but it shouldn't do this if
160  * n is in the range 0 <= n < get_num_nested_fields()).
161  */
163 get_nested_field(int n) const {
164  nassertr(n >= 0 && n < (int)_nested_fields.size(), nullptr);
165  return _nested_fields[n];
166 }
167 
168 /**
169  * Returns true if the other interface is bitwise the same as this one--that
170  * is, a uint32 only matches a uint32, etc. Names of components, and range
171  * limits, are not compared.
172  */
173 bool DCMolecularField::
174 do_check_match(const DCPackerInterface *other) const {
175  return other->do_check_match_molecular_field(this);
176 }
177 
178 /**
179  * Returns true if this field matches the indicated molecular field, false
180  * otherwise.
181  */
182 bool DCMolecularField::
183 do_check_match_molecular_field(const DCMolecularField *other) const {
184  if (_nested_fields.size() != other->_nested_fields.size()) {
185  return false;
186  }
187  for (size_t i = 0; i < _nested_fields.size(); i++) {
188  if (!_nested_fields[i]->check_match(other->_nested_fields[i])) {
189  return false;
190  }
191  }
192 
193  return true;
194 }
A single atomic field of a Distributed Class, as read from a .dc file.
Definition: dcAtomicField.h:30
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
Defines a particular DistributedClass as read from an input .dc file.
Definition: dcClass.h:44
A single field of a Distributed Class, either atomic or molecular.
Definition: dcField.h:37
bool has_default_value() const
Returns true if a default value has been explicitly established for this field, false otherwise.
Definition: dcField.I:36
bool is_bogus_field() const
Returns true if the field has been flagged as a bogus field.
Definition: dcField.I:60
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this field into the hash.
Definition: dcField.cxx:467
void copy_keywords(const DCKeywordList &other)
Replaces this keyword list with those from the other list.
A single molecular field of a Distributed Class, as read from a .dc file.
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this field into the hash.
virtual void write(std::ostream &out, bool brief, int indent_level) const
Generates a parseable description of the object to the indicated output stream.
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
virtual DCMolecularField * as_molecular_field()
Returns the same field pointer converted to a molecular field pointer, if this is in fact a molecular...
int get_num_atomics() const
Returns the number of atomic fields that make up this molecular field.
DCAtomicField * get_atomic(int n) const
Returns the nth atomic field that makes up this molecular field.
void add_atomic(DCAtomicField *atomic)
Adds the indicated atomic field to the end of the list of atomic fields that make up the molecular fi...
This defines the internal interface for packing values into a DCField.
bool has_fixed_structure() const
Returns true if this field type always has the same structure regardless of the data in the stream,...
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.
int get_num_nested_fields() const
Returns the number of nested fields required by this field type.
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.
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 ...
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 do_check_match_molecular_field(const DCMolecularField *other) const
Returns true if this field matches the indicated molecular field, false otherwise.
This class generates an arbitrary hash number from a sequence of ints.
Definition: hashGenerator.h:23
void add_int(int num)
Adds another integer to the hash so far.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20