00001 // Filename: dcTypedef.cxx 00002 // Created by: drose (17Jun04) 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 "dcTypedef.h" 00016 #include "dcParameter.h" 00017 #include "dcSimpleParameter.h" 00018 #include "dcindent.h" 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: DCTypedef::Constructor 00022 // Access: Public 00023 // Description: The DCTypedef object becomes the owner of the 00024 // supplied parameter pointer and will delete it upon 00025 // destruction. 00026 //////////////////////////////////////////////////////////////////// 00027 DCTypedef:: 00028 DCTypedef(DCParameter *parameter, bool implicit) : 00029 _parameter(parameter), 00030 _bogus_typedef(false), 00031 _implicit_typedef(implicit), 00032 _number(-1) 00033 { 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: DCTypedef::Constructor 00038 // Access: Public 00039 // Description: Creates a bogus typedef reference. 00040 //////////////////////////////////////////////////////////////////// 00041 DCTypedef:: 00042 DCTypedef(const string &name) : 00043 _parameter(new DCSimpleParameter(ST_invalid)), 00044 _bogus_typedef(true), 00045 _implicit_typedef(false), 00046 _number(-1) 00047 { 00048 _parameter->set_name(name); 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: DCTypedef::Destructor 00053 // Access: Public, Virtual 00054 // Description: 00055 //////////////////////////////////////////////////////////////////// 00056 DCTypedef:: 00057 ~DCTypedef() { 00058 delete _parameter; 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: DCTypedef::get_number 00063 // Access: Published 00064 // Description: Returns a unique index number associated with this 00065 // typedef definition. This is defined implicitly when 00066 // the .dc file(s) are read. 00067 //////////////////////////////////////////////////////////////////// 00068 int DCTypedef:: 00069 get_number() const { 00070 return _number; 00071 } 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Function: DCTypedef::get_name 00075 // Access: Published 00076 // Description: Returns the name of this typedef. 00077 //////////////////////////////////////////////////////////////////// 00078 const string &DCTypedef:: 00079 get_name() const { 00080 return _parameter->get_name(); 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: DCTypedef::get_description 00085 // Access: Published 00086 // Description: Returns a brief decription of the typedef, useful for 00087 // human consumption. 00088 //////////////////////////////////////////////////////////////////// 00089 string DCTypedef:: 00090 get_description() const { 00091 ostringstream strm; 00092 _parameter->output(strm, true); 00093 return strm.str(); 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: DCTypedef::is_bogus_typedef 00098 // Access: Public 00099 // Description: Returns true if the typedef has been flagged as a bogus 00100 // typedef. This is set for typedefs that are generated by 00101 // the parser as placeholder for missing typedefs, as 00102 // when reading a partial file; it should not occur in a 00103 // normal valid dc file. 00104 //////////////////////////////////////////////////////////////////// 00105 bool DCTypedef:: 00106 is_bogus_typedef() const { 00107 return _bogus_typedef; 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: DCTypedef::is_implicit_typedef 00112 // Access: Public 00113 // Description: Returns true if the typedef has been flagged as an 00114 // implicit typedef, meaning it was created for a 00115 // DCClass that was referenced inline as a type. 00116 //////////////////////////////////////////////////////////////////// 00117 bool DCTypedef:: 00118 is_implicit_typedef() const { 00119 return _implicit_typedef; 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: DCTypedef::make_new_parameter 00124 // Access: Public 00125 // Description: Returns a newly-allocated DCParameter object that 00126 // uses the same type as that named by the typedef. 00127 //////////////////////////////////////////////////////////////////// 00128 DCParameter *DCTypedef:: 00129 make_new_parameter() const { 00130 DCParameter *new_parameter = _parameter->make_copy(); 00131 new_parameter->set_name(string()); 00132 new_parameter->set_typedef(this); 00133 return new_parameter; 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function: DCTypedef::set_number 00138 // Access: Public 00139 // Description: Assigns the unique number to this typedef. This is 00140 // normally called only by the DCFile interface as the 00141 // typedef is added. 00142 //////////////////////////////////////////////////////////////////// 00143 void DCTypedef:: 00144 set_number(int number) { 00145 _number = number; 00146 } 00147 00148 //////////////////////////////////////////////////////////////////// 00149 // Function : DCTypedef::output 00150 // Access : Public, Virtual 00151 // Description : Write a string representation of this instance to 00152 // <out>. 00153 //////////////////////////////////////////////////////////////////// 00154 void DCTypedef:: 00155 output(ostream &out, bool brief) const { 00156 out << "typedef "; 00157 _parameter->output(out, false); 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: DCTypedef::write 00162 // Access: Public, Virtual 00163 // Description: 00164 //////////////////////////////////////////////////////////////////// 00165 void DCTypedef:: 00166 write(ostream &out, bool brief, int indent_level) const { 00167 indent(out, indent_level) 00168 << "typedef "; 00169 00170 // We need to preserve the parameter name in the typedef (this is 00171 // the typedef name); hence, we pass brief = false to output(). 00172 _parameter->output(out, false); 00173 out << ";"; 00174 00175 if (!brief) { 00176 out << " // typedef " << _number; 00177 } 00178 out << "\n"; 00179 }