00001 // Filename: eventParameter.I 00002 // Created by: drose (08Feb99) 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 00016 00017 template<class Type> 00018 TypeHandle EventStoreValue<Type>::_type_handle; 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: EventParameter::Default constructor 00022 // Access: Published 00023 // Description: Defines an EventParameter that stores nothing: the 00024 // "empty" parameter. 00025 //////////////////////////////////////////////////////////////////// 00026 INLINE EventParameter:: 00027 EventParameter() { 00028 } 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: EventParameter::Pointer constructor 00032 // Access: Published 00033 // Description: Defines an EventParameter that stores a pointer to 00034 // any kind of TypedWritableReferenceCount object. This 00035 // is the most general constructor. 00036 // 00037 // This accepts a const pointer, even though it stores 00038 // (and eventually returns) a non-const pointer. This 00039 // is just the simplest way to allow both const and 00040 // non-const pointers to be stored, but it does lose the 00041 // constness. Be careful. 00042 //////////////////////////////////////////////////////////////////// 00043 INLINE EventParameter:: 00044 EventParameter(const TypedWritableReferenceCount *ptr) : _ptr((TypedWritableReferenceCount *)ptr) { } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: EventParameter::Pointer constructor 00048 // Access: Published 00049 // Description: Defines an EventParameter that stores a pointer to 00050 // a TypedReferenceCount object. Note that a 00051 // TypedReferenceCount is not the same kind of pointer 00052 // as a TypedWritableReferenceCount, hence we require 00053 // both constructors. 00054 // 00055 // This accepts a const pointer, even though it stores 00056 // (and eventually returns) a non-const pointer. This 00057 // is just the simplest way to allow both const and 00058 // non-const pointers to be stored, but it does lose the 00059 // constness. Be careful. 00060 //////////////////////////////////////////////////////////////////// 00061 INLINE EventParameter:: 00062 EventParameter(const TypedReferenceCount *ptr) : _ptr(new EventStoreTypedRefCount((TypedReferenceCount *)ptr)) { } 00063 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: EventParameter::Integer constructor 00067 // Access: Published 00068 // Description: Defines an EventParameter that stores an integer 00069 // value. 00070 //////////////////////////////////////////////////////////////////// 00071 INLINE EventParameter:: 00072 EventParameter(int value) : _ptr(new EventStoreInt(value)) { } 00073 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: EventParameter::Double constructor 00077 // Access: Published 00078 // Description: Defines an EventParameter that stores a 00079 // floating-point value. 00080 //////////////////////////////////////////////////////////////////// 00081 INLINE EventParameter:: 00082 EventParameter(double value) : _ptr(new EventStoreDouble(value)) { } 00083 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: EventParameter::String constructor 00087 // Access: Published 00088 // Description: Defines an EventParameter that stores a string value. 00089 //////////////////////////////////////////////////////////////////// 00090 INLINE EventParameter:: 00091 EventParameter(const string &value) : _ptr(new EventStoreString(value)) { } 00092 00093 //////////////////////////////////////////////////////////////////// 00094 // Function: EventParameter::Wstring constructor 00095 // Access: Published 00096 // Description: Defines an EventParameter that stores a wstring value. 00097 //////////////////////////////////////////////////////////////////// 00098 INLINE EventParameter:: 00099 EventParameter(const wstring &value) : _ptr(new EventStoreWstring(value)) { } 00100 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: EventParameter::Copy constructor 00104 // Access: Published 00105 // Description: 00106 //////////////////////////////////////////////////////////////////// 00107 INLINE EventParameter:: 00108 EventParameter(const EventParameter &other) : _ptr(other._ptr) { } 00109 00110 00111 //////////////////////////////////////////////////////////////////// 00112 // Function: EventParameter::Copy assignment operator 00113 // Access: Published 00114 // Description: 00115 //////////////////////////////////////////////////////////////////// 00116 INLINE EventParameter &EventParameter:: 00117 operator = (const EventParameter &other) { 00118 _ptr = other._ptr; 00119 return *this; 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: EventParameter::Destructor 00124 // Access: Published 00125 // Description: 00126 //////////////////////////////////////////////////////////////////// 00127 INLINE EventParameter:: 00128 ~EventParameter() { 00129 } 00130 00131 //////////////////////////////////////////////////////////////////// 00132 // Function: EventParameter::is_empty 00133 // Access: Published 00134 // Description: Returns true if the EventParameter is the empty 00135 // parameter, storing nothing, or false otherwise. 00136 //////////////////////////////////////////////////////////////////// 00137 INLINE bool EventParameter:: 00138 is_empty() const { 00139 return (_ptr == (TypedWritableReferenceCount *)NULL); 00140 } 00141 00142 //////////////////////////////////////////////////////////////////// 00143 // Function: EventParameter::is_int 00144 // Access: Published 00145 // Description: Returns true if the EventParameter stores an integer 00146 // value, false otherwise. 00147 //////////////////////////////////////////////////////////////////// 00148 INLINE bool EventParameter:: 00149 is_int() const { 00150 if (is_empty()) { 00151 return false; 00152 } 00153 return _ptr->is_of_type(EventStoreInt::get_class_type()); 00154 } 00155 00156 //////////////////////////////////////////////////////////////////// 00157 // Function: EventParameter::get_int_value 00158 // Access: Published 00159 // Description: Retrieves the value stored in the EventParameter. It 00160 // is only valid to call this if is_int() has already 00161 // returned true. 00162 //////////////////////////////////////////////////////////////////// 00163 INLINE int EventParameter:: 00164 get_int_value() const { 00165 nassertr(is_int(), 0); 00166 // We can't use DCAST, because EventStoreValue::init_type() breaks 00167 // convention and takes a parameter. But the above assertion should 00168 // protect us. 00169 return ((const EventStoreInt *)_ptr.p())->get_value(); 00170 } 00171 00172 //////////////////////////////////////////////////////////////////// 00173 // Function: EventParameter::is_double 00174 // Access: Published 00175 // Description: Returns true if the EventParameter stores a double 00176 // floating-point value, false otherwise. 00177 //////////////////////////////////////////////////////////////////// 00178 INLINE bool EventParameter:: 00179 is_double() const { 00180 if (is_empty()) { 00181 return false; 00182 } 00183 return _ptr->is_of_type(EventStoreDouble::get_class_type()); 00184 } 00185 00186 //////////////////////////////////////////////////////////////////// 00187 // Function: EventParameter::get_double_value 00188 // Access: Published 00189 // Description: Retrieves the value stored in the EventParameter. It 00190 // is only valid to call this if is_double() has already 00191 // returned true. 00192 //////////////////////////////////////////////////////////////////// 00193 INLINE double EventParameter:: 00194 get_double_value() const { 00195 nassertr(is_double(), 0.0); 00196 return ((const EventStoreDouble *)_ptr.p())->get_value(); 00197 } 00198 00199 //////////////////////////////////////////////////////////////////// 00200 // Function: EventParameter::is_string 00201 // Access: Published 00202 // Description: Returns true if the EventParameter stores a string 00203 // value, false otherwise. 00204 //////////////////////////////////////////////////////////////////// 00205 INLINE bool EventParameter:: 00206 is_string() const { 00207 if (is_empty()) { 00208 return false; 00209 } 00210 return _ptr->is_of_type(EventStoreString::get_class_type()); 00211 } 00212 00213 //////////////////////////////////////////////////////////////////// 00214 // Function: EventParameter::get_string_value 00215 // Access: Published 00216 // Description: Retrieves the value stored in the EventParameter. It 00217 // is only valid to call this if is_string() has already 00218 // returned true. 00219 //////////////////////////////////////////////////////////////////// 00220 INLINE string EventParameter:: 00221 get_string_value() const { 00222 nassertr(is_string(), ""); 00223 return ((const EventStoreString *)_ptr.p())->get_value(); 00224 } 00225 00226 //////////////////////////////////////////////////////////////////// 00227 // Function: EventParameter::is_wstring 00228 // Access: Published 00229 // Description: Returns true if the EventParameter stores a wstring 00230 // value, false otherwise. 00231 //////////////////////////////////////////////////////////////////// 00232 INLINE bool EventParameter:: 00233 is_wstring() const { 00234 if (is_empty()) { 00235 return false; 00236 } 00237 return _ptr->is_of_type(EventStoreWstring::get_class_type()); 00238 } 00239 00240 //////////////////////////////////////////////////////////////////// 00241 // Function: EventParameter::get_wstring_value 00242 // Access: Published 00243 // Description: Retrieves the value stored in the EventParameter. It 00244 // is only valid to call this if is_wstring() has already 00245 // returned true. 00246 //////////////////////////////////////////////////////////////////// 00247 INLINE wstring EventParameter:: 00248 get_wstring_value() const { 00249 nassertr(is_wstring(), wstring()); 00250 return ((const EventStoreWstring *)_ptr.p())->get_value(); 00251 } 00252 00253 //////////////////////////////////////////////////////////////////// 00254 // Function: EventParameter::is_typed_ref_count 00255 // Access: Published 00256 // Description: Returns true if the EventParameter stores a 00257 // TypedReferenceCount pointer, false otherwise. Note 00258 // that a TypedReferenceCount is not exactly the same 00259 // kind of pointer as a TypedWritableReferenceCount, 00260 // hence the need for this separate call. 00261 //////////////////////////////////////////////////////////////////// 00262 INLINE bool EventParameter:: 00263 is_typed_ref_count() const { 00264 if (is_empty()) { 00265 return false; 00266 } 00267 return _ptr->is_of_type(EventStoreTypedRefCount::get_class_type()); 00268 } 00269 00270 //////////////////////////////////////////////////////////////////// 00271 // Function: EventParameter::get_typed_ref_count_value 00272 // Access: Published 00273 // Description: Retrieves the value stored in the EventParameter. It 00274 // is only valid to call this if is_typed_ref_count() 00275 // has already returned true. 00276 //////////////////////////////////////////////////////////////////// 00277 INLINE TypedReferenceCount *EventParameter:: 00278 get_typed_ref_count_value() const { 00279 nassertr(is_typed_ref_count(), NULL); 00280 return ((const EventStoreTypedRefCount *)_ptr.p())->get_value(); 00281 } 00282 00283 //////////////////////////////////////////////////////////////////// 00284 // Function: EventParameter::get_ptr 00285 // Access: Published 00286 // Description: Retrieves a pointer to the actual value stored in the 00287 // parameter. The TypeHandle of this pointer may be 00288 // examined to determine the actual type of parameter it 00289 // contains. This is the only way to retrieve the value 00290 // when it is not one of the above predefined types. 00291 //////////////////////////////////////////////////////////////////// 00292 INLINE TypedWritableReferenceCount *EventParameter:: 00293 get_ptr() const { 00294 return _ptr; 00295 } 00296 00297 INLINE ostream & 00298 operator << (ostream &out, const EventParameter ¶m) { 00299 param.output(out); 00300 return out; 00301 } 00302 00303 00304 //////////////////////////////////////////////////////////////////// 00305 // Function: EventStoreValueBase::Constructor 00306 // Access: Public 00307 // Description: 00308 //////////////////////////////////////////////////////////////////// 00309 INLINE EventStoreValueBase:: 00310 EventStoreValueBase() { 00311 } 00312 00313 //////////////////////////////////////////////////////////////////// 00314 // Function: EventStoreTypedRefCount::Constructor 00315 // Access: Published 00316 // Description: 00317 //////////////////////////////////////////////////////////////////// 00318 INLINE EventStoreTypedRefCount:: 00319 EventStoreTypedRefCount(const TypedReferenceCount *value) : 00320 _value((TypedReferenceCount *)value) 00321 { 00322 } 00323 00324 //////////////////////////////////////////////////////////////////// 00325 // Function: EventStoreTypedRefCount::set_value 00326 // Access: Published 00327 // Description: Changes the value stored in the parameter. It is 00328 // dangerous to do this for a parameter already added to 00329 // an event, since the parameters may be shared. 00330 //////////////////////////////////////////////////////////////////// 00331 INLINE void EventStoreTypedRefCount:: 00332 set_value(const TypedReferenceCount *value) { 00333 _value = (TypedReferenceCount *)value; 00334 } 00335 00336 00337 //////////////////////////////////////////////////////////////////// 00338 // Function: EventStoreTypedRefCount::get_value 00339 // Access: Published 00340 // Description: Retrieves the value stored in the parameter. 00341 //////////////////////////////////////////////////////////////////// 00342 INLINE TypedReferenceCount *EventStoreTypedRefCount:: 00343 get_value() const { 00344 return _value; 00345 } 00346 00347 //////////////////////////////////////////////////////////////////// 00348 // Function: EventStoreValue::Default Constructor 00349 // Access: Private 00350 // Description: This constructor is only for make_from_bam(). 00351 //////////////////////////////////////////////////////////////////// 00352 template<class Type> 00353 EventStoreValue<Type>:: 00354 EventStoreValue() { 00355 } 00356 00357 //////////////////////////////////////////////////////////////////// 00358 // Function: EventStoreValue::Constructor 00359 // Access: Public 00360 // Description: 00361 //////////////////////////////////////////////////////////////////// 00362 template<class Type> 00363 EventStoreValue<Type>:: 00364 EventStoreValue(const Type &value) : 00365 _value(value) 00366 { 00367 } 00368 00369 //////////////////////////////////////////////////////////////////// 00370 // Function: EventStoreValue::Destructor 00371 // Access: Public, Virtual 00372 // Description: 00373 //////////////////////////////////////////////////////////////////// 00374 template<class Type> 00375 EventStoreValue<Type>:: 00376 ~EventStoreValue() { 00377 } 00378 00379 //////////////////////////////////////////////////////////////////// 00380 // Function: EventStoreValue::set_value 00381 // Access: Public 00382 // Description: Changes the value stored in the parameter. It is 00383 // dangerous to do this for a parameter already added to 00384 // an event, since the parameters may be shared. 00385 //////////////////////////////////////////////////////////////////// 00386 template<class Type> 00387 INLINE void EventStoreValue<Type>:: 00388 set_value(const Type &value) { 00389 _value = value; 00390 } 00391 00392 00393 //////////////////////////////////////////////////////////////////// 00394 // Function: EventStoreValue::get_value 00395 // Access: Public 00396 // Description: Retrieves the value stored in the parameter. 00397 //////////////////////////////////////////////////////////////////// 00398 template<class Type> 00399 INLINE const Type &EventStoreValue<Type>:: 00400 get_value() const { 00401 return _value; 00402 } 00403 00404 //////////////////////////////////////////////////////////////////// 00405 // Function: EventStoreValue::output 00406 // Access: Public, Virtual 00407 // Description: 00408 //////////////////////////////////////////////////////////////////// 00409 template<class Type> 00410 void EventStoreValue<Type>:: 00411 output(ostream &out) const { 00412 out << _value; 00413 } 00414 00415 //////////////////////////////////////////////////////////////////// 00416 // Function: EventStoreValue::register_with_read_factory 00417 // Access: Public, Static 00418 // Description: Tells the BamReader how to create objects of type 00419 // Lens. 00420 //////////////////////////////////////////////////////////////////// 00421 template<class Type> 00422 void EventStoreValue<Type>:: 00423 register_with_read_factory() { 00424 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00425 } 00426 00427 //////////////////////////////////////////////////////////////////// 00428 // Function: EventStoreValue::write_datagram 00429 // Access: Public, Virtual 00430 // Description: Writes the contents of this object to the datagram 00431 // for shipping out to a Bam file. 00432 //////////////////////////////////////////////////////////////////// 00433 template<class Type> 00434 void EventStoreValue<Type>:: 00435 write_datagram(BamWriter *manager, Datagram &dg) { 00436 TypedWritable::write_datagram(manager, dg); 00437 generic_write_datagram(dg, _value); 00438 } 00439 00440 //////////////////////////////////////////////////////////////////// 00441 // Function: EventStoreValue::make_from_bam 00442 // Access: Protected, Static 00443 // Description: This function is called by the BamReader's factory 00444 // when a new object of type Lens is encountered 00445 // in the Bam file. It should create the Lens 00446 // and extract its information from the file. 00447 //////////////////////////////////////////////////////////////////// 00448 template<class Type> 00449 TypedWritable *EventStoreValue<Type>:: 00450 make_from_bam(const FactoryParams ¶ms) { 00451 EventStoreValue<Type> *esv = new EventStoreValue<Type>; 00452 DatagramIterator scan; 00453 BamReader *manager; 00454 00455 parse_params(params, scan, manager); 00456 esv->fillin(scan, manager); 00457 00458 return esv; 00459 } 00460 00461 //////////////////////////////////////////////////////////////////// 00462 // Function: EventStoreValue::fillin 00463 // Access: Protected 00464 // Description: This internal function is called by make_from_bam to 00465 // read in all of the relevant data from the BamFile for 00466 // the new Lens. 00467 //////////////////////////////////////////////////////////////////// 00468 template<class Type> 00469 void EventStoreValue<Type>:: 00470 fillin(DatagramIterator &scan, BamReader *manager) { 00471 TypedWritable::fillin(scan, manager); 00472 generic_read_datagram(_value, scan); 00473 }