Panda3D

dcClassParameter.cxx

00001 // Filename: dcClassParameter.cxx
00002 // Created by:  drose (18Jun04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "dcClassParameter.h"
00016 #include "dcClass.h"
00017 #include "dcArrayParameter.h"
00018 #include "hashGenerator.h"
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: DCClassParameter::Constructor
00022 //       Access: Public
00023 //  Description: 
00024 ////////////////////////////////////////////////////////////////////
00025 DCClassParameter::
00026 DCClassParameter(const DCClass *dclass) :
00027   _dclass(dclass)
00028 {
00029   set_name(dclass->get_name());
00030 
00031   int num_fields = _dclass->get_num_inherited_fields();
00032 
00033   _has_nested_fields = true;
00034   _pack_type = PT_class;
00035 
00036   if (_dclass->has_constructor()) {
00037     DCField *field = _dclass->get_constructor();
00038     _nested_fields.push_back(field);
00039     _has_default_value = _has_default_value || field->has_default_value();
00040   }
00041   int i;
00042   for (i = 0 ; i < num_fields; i++) {
00043     DCField *field = _dclass->get_inherited_field(i);
00044     if (!field->as_molecular_field()) {
00045       _nested_fields.push_back(field);
00046       _has_default_value = _has_default_value || field->has_default_value();
00047     }
00048   }
00049   _num_nested_fields = _nested_fields.size();
00050 
00051   // If all of the nested fields have a fixed byte size, then so does
00052   // the class (and its byte size is the sum of all of the nested
00053   // fields).
00054   _has_fixed_byte_size = true;
00055   _fixed_byte_size = 0;
00056   _has_fixed_structure = true;
00057   for (i = 0; i < _num_nested_fields; i++) {
00058     DCPackerInterface *field = get_nested_field(i);
00059     _has_fixed_byte_size = _has_fixed_byte_size && field->has_fixed_byte_size();
00060     _fixed_byte_size += field->get_fixed_byte_size();
00061     _has_fixed_structure = _has_fixed_structure && field->has_fixed_structure();
00062 
00063     _has_range_limits = _has_range_limits || field->has_range_limits();
00064   }
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: DCClassParameter::Copy Constructor
00069 //       Access: Public
00070 //  Description: 
00071 ////////////////////////////////////////////////////////////////////
00072 DCClassParameter::
00073 DCClassParameter(const DCClassParameter &copy) :
00074   DCParameter(copy),
00075   _nested_fields(copy._nested_fields),
00076   _dclass(copy._dclass)
00077 {
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: DCClassParameter::as_class_parameter
00082 //       Access: Published, Virtual
00083 //  Description: 
00084 ////////////////////////////////////////////////////////////////////
00085 DCClassParameter *DCClassParameter::
00086 as_class_parameter() {
00087   return this;
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: DCClassParameter::as_class_parameter
00092 //       Access: Published, Virtual
00093 //  Description: 
00094 ////////////////////////////////////////////////////////////////////
00095 const DCClassParameter *DCClassParameter::
00096 as_class_parameter() const {
00097   return this;
00098 }
00099 
00100 ////////////////////////////////////////////////////////////////////
00101 //     Function: DCClassParameter::make_copy
00102 //       Access: Published, Virtual
00103 //  Description: 
00104 ////////////////////////////////////////////////////////////////////
00105 DCParameter *DCClassParameter::
00106 make_copy() const {
00107   return new DCClassParameter(*this);
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: DCClassParameter::is_valid
00112 //       Access: Published, Virtual
00113 //  Description: Returns false if the type is an invalid type
00114 //               (e.g. declared from an undefined typedef), true if
00115 //               it is valid.
00116 ////////////////////////////////////////////////////////////////////
00117 bool DCClassParameter::
00118 is_valid() const {
00119   return !_dclass->is_bogus_class();
00120 }
00121 
00122 ////////////////////////////////////////////////////////////////////
00123 //     Function: DCClassParameter::get_class
00124 //       Access: Published
00125 //  Description: Returns the class object this parameter represents.
00126 ////////////////////////////////////////////////////////////////////
00127 const DCClass *DCClassParameter::
00128 get_class() const {
00129   return _dclass;
00130 }
00131 
00132 ////////////////////////////////////////////////////////////////////
00133 //     Function: DCClassParameter::get_nested_field
00134 //       Access: Public, Virtual
00135 //  Description: Returns the DCPackerInterface object that represents
00136 //               the nth nested field.  This may return NULL if there
00137 //               is no such field (but it shouldn't do this if n is in
00138 //               the range 0 <= n < get_num_nested_fields()).
00139 ////////////////////////////////////////////////////////////////////
00140 DCPackerInterface *DCClassParameter::
00141 get_nested_field(int n) const {
00142   nassertr(n >= 0 && n < (int)_nested_fields.size(), NULL);
00143   return _nested_fields[n];
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: DCClassParameter::output_instance
00148 //       Access: Public, Virtual
00149 //  Description: Formats the parameter in the C++-like dc syntax as a
00150 //               typename and identifier.
00151 ////////////////////////////////////////////////////////////////////
00152 void DCClassParameter::
00153 output_instance(ostream &out, bool brief, const string &prename, 
00154                 const string &name, const string &postname) const {
00155   if (get_typedef() != (DCTypedef *)NULL) {
00156     output_typedef_name(out, brief, prename, name, postname);
00157 
00158   } else {
00159     _dclass->output_instance(out, brief, prename, name, postname);
00160   }
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: DCClassParameter::generate_hash
00165 //       Access: Public, Virtual
00166 //  Description: Accumulates the properties of this type into the
00167 //               hash.
00168 ////////////////////////////////////////////////////////////////////
00169 void DCClassParameter::
00170 generate_hash(HashGenerator &hashgen) const {
00171   DCParameter::generate_hash(hashgen);
00172   _dclass->generate_hash(hashgen);
00173 }
00174 
00175 ////////////////////////////////////////////////////////////////////
00176 //     Function: DCClassParameter::do_check_match
00177 //       Access: Protected, Virtual
00178 //  Description: Returns true if the other interface is bitwise the
00179 //               same as this one--that is, a uint32 only matches a
00180 //               uint32, etc. Names of components, and range limits,
00181 //               are not compared.
00182 ////////////////////////////////////////////////////////////////////
00183 bool DCClassParameter::
00184 do_check_match(const DCPackerInterface *other) const {
00185   return other->do_check_match_class_parameter(this);
00186 }
00187 
00188 ////////////////////////////////////////////////////////////////////
00189 //     Function: DCClassParameter::do_check_match_class_parameter
00190 //       Access: Protected, Virtual
00191 //  Description: Returns true if this field matches the indicated
00192 //               class parameter, false otherwise.
00193 ////////////////////////////////////////////////////////////////////
00194 bool DCClassParameter::
00195 do_check_match_class_parameter(const DCClassParameter *other) const {
00196   if (_nested_fields.size() != other->_nested_fields.size()) {
00197     return false;
00198   }
00199   for (size_t i = 0; i < _nested_fields.size(); i++) {
00200     if (!_nested_fields[i]->check_match(other->_nested_fields[i])) {
00201       return false;
00202     }
00203   }
00204 
00205   return true;
00206 }
00207 
00208 ////////////////////////////////////////////////////////////////////
00209 //     Function: DCClassParameter::do_check_match_array_parameter
00210 //       Access: Protected, Virtual
00211 //  Description: Returns true if this field matches the indicated
00212 //               array parameter, false otherwise.
00213 ////////////////////////////////////////////////////////////////////
00214 bool DCClassParameter::
00215 do_check_match_array_parameter(const DCArrayParameter *other) const {
00216   if ((int)_nested_fields.size() != other->get_array_size()) {
00217     // We can only match a fixed-size array whose size happens to
00218     // exactly match our number of fields.
00219     return false;
00220   }
00221 
00222   const DCPackerInterface *element_type = other->get_element_type();
00223   for (size_t i = 0; i < _nested_fields.size(); i++) {
00224     if (!_nested_fields[i]->check_match(element_type)) {
00225       return false;
00226     }
00227   }
00228 
00229   return true;
00230 }
 All Classes Functions Variables Enumerations