Panda3D

eventParameter.h

00001 // Filename: eventParameter.h
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 #ifndef EVENTPARAMETER_H
00016 #define EVENTPARAMETER_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "typedef.h"
00021 #include "typedObject.h"
00022 #include "typedWritableReferenceCount.h"
00023 #include "pointerTo.h"
00024 #include "bamReader.h"
00025 #include "bamWriter.h"
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //       Class : EventParameter
00029 // Description : An optional parameter associated with an event.  Each
00030 //               event may have zero or more of these.  Each parameter
00031 //               stores a pointer to a TypedWritableReferenceCount
00032 //               object, which of course could be pretty much
00033 //               anything.  To store a simple value like a double or a
00034 //               string, the EventParameter constructors transparently
00035 //               use the EventStoreValue template class, defined
00036 //               below.
00037 ////////////////////////////////////////////////////////////////////
00038 class EXPCL_PANDA_EVENT EventParameter {
00039 PUBLISHED:
00040   INLINE EventParameter();
00041   INLINE EventParameter(const TypedWritableReferenceCount *ptr);
00042   INLINE EventParameter(const TypedReferenceCount *ptr);
00043   INLINE EventParameter(int value);
00044   INLINE EventParameter(double value);
00045   INLINE EventParameter(const string &value);
00046   INLINE EventParameter(const wstring &value);
00047 
00048   INLINE EventParameter(const EventParameter &copy);
00049   INLINE EventParameter &operator = (const EventParameter &copy);
00050   INLINE ~EventParameter();
00051 
00052   // These functions are conveniences to easily determine if the
00053   // EventParameter is one of the predefined parameter types, and
00054   // retrieve the corresponding value.  Of course, it is possible that
00055   // the EventParameter is some user-defined type, and is none of
00056   // these.
00057   INLINE bool is_empty() const;
00058   INLINE bool is_int() const;
00059   INLINE int get_int_value() const;
00060   INLINE bool is_double() const;
00061   INLINE double get_double_value() const;
00062   INLINE bool is_string() const;
00063   INLINE string get_string_value() const;
00064   INLINE bool is_wstring() const;
00065   INLINE wstring get_wstring_value() const;
00066 
00067   INLINE bool is_typed_ref_count() const;
00068   INLINE TypedReferenceCount *get_typed_ref_count_value() const;
00069 
00070   INLINE TypedWritableReferenceCount *get_ptr() const;
00071 
00072   void output(ostream &out) const;
00073 
00074 private:
00075   PT(TypedWritableReferenceCount) _ptr;
00076 };
00077 
00078 INLINE ostream &operator << (ostream &out, const EventParameter &param);
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //       Class : EventStoreValueBase
00082 // Description : A non-template base class of EventStoreValue (below),
00083 //               which serves mainly to define the placeholder for the
00084 //               virtual output function.
00085 ////////////////////////////////////////////////////////////////////
00086 class EXPCL_PANDA_EVENT EventStoreValueBase : public TypedWritableReferenceCount {
00087 public:
00088   INLINE EventStoreValueBase();
00089 
00090 PUBLISHED:
00091   virtual ~EventStoreValueBase();
00092   virtual void output(ostream &out) const=0;
00093 
00094 public:
00095   virtual TypeHandle get_type() const {
00096     return get_class_type();
00097   }
00098   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00099   static TypeHandle get_class_type() {
00100     return _type_handle;
00101   }
00102   static void init_type() {
00103     TypedWritableReferenceCount::init_type();
00104     register_type(_type_handle, "EventStoreValueBase",
00105                   TypedWritableReferenceCount::get_class_type());
00106   }
00107 
00108 private:
00109   static TypeHandle _type_handle;
00110 };
00111 
00112 ////////////////////////////////////////////////////////////////////
00113 //       Class : EventStoreTypedRefCount
00114 // Description : A class object for storing specifically objects of
00115 //               type TypedReferenceCount, which is different than
00116 //               TypedWritableReferenceCount.
00117 ////////////////////////////////////////////////////////////////////
00118 class EXPCL_PANDA_EVENT EventStoreTypedRefCount : public EventStoreValueBase {
00119 PUBLISHED:
00120   INLINE EventStoreTypedRefCount(const TypedReferenceCount *value);
00121   virtual ~EventStoreTypedRefCount();
00122 
00123   INLINE void set_value(const TypedReferenceCount *value);
00124   INLINE TypedReferenceCount *get_value() const;
00125 
00126   virtual void output(ostream &out) const;
00127 
00128 public:
00129   PT(TypedReferenceCount) _value;
00130 
00131 public:
00132   virtual TypeHandle get_type() const {
00133     return get_class_type();
00134   }
00135   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00136   static TypeHandle get_class_type() {
00137     return _type_handle;
00138   }
00139   static void init_type() {
00140     EventStoreValueBase::init_type();
00141     register_type(_type_handle, "EventStoreTypedRefCount",
00142                   EventStoreValueBase::get_class_type());
00143   }
00144 
00145 private:
00146   static TypeHandle _type_handle;
00147 };
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //       Class : EventStoreValue
00151 // Description : A handy class object for storing simple values (like
00152 //               integers or strings) passed along with an Event.
00153 //               This is essentially just a wrapper around whatever
00154 //               data type you like, to make it a
00155 //               TypedWritableReferenceCount object which can be
00156 //               passed along inside an EventParameter.
00157 ////////////////////////////////////////////////////////////////////
00158 template<class Type>
00159 class EventStoreValue : public EventStoreValueBase {
00160 private:
00161   INLINE EventStoreValue();
00162 public:
00163   INLINE EventStoreValue(const Type &value);
00164   virtual ~EventStoreValue();
00165 
00166   INLINE void set_value(const Type &value);
00167   INLINE const Type &get_value() const;
00168 
00169   virtual void output(ostream &out) const;
00170 
00171   Type _value;
00172 
00173 public:
00174   static void register_with_read_factory();
00175   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00176 
00177 protected:
00178   static TypedWritable *make_from_bam(const FactoryParams &params);
00179   void fillin(DatagramIterator &scan, BamReader *manager);
00180 
00181 public:
00182   static TypeHandle get_class_type() {
00183     return _type_handle;
00184   }
00185   static void init_type(const string &type_name = "UndefinedEventStoreValue") {
00186     EventStoreValueBase::init_type();
00187     _type_handle = register_dynamic_type
00188       (type_name, EventStoreValueBase::get_class_type());
00189   }
00190   virtual TypeHandle get_type() const {
00191     return get_class_type();
00192   }
00193   virtual TypeHandle force_init_type() {
00194     // In this case, we can't do anything, since we don't have the
00195     // class' type_name.
00196     return get_class_type();
00197   }
00198 
00199 private:
00200   static TypeHandle _type_handle;
00201 };
00202 
00203 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_EVENT, EXPTP_PANDA_EVENT, EventStoreValue<int>);
00204 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_EVENT, EXPTP_PANDA_EVENT, EventStoreValue<double>);
00205 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_EVENT, EXPTP_PANDA_EVENT, EventStoreValue<std::string>);
00206 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_EVENT, EXPTP_PANDA_EVENT, EventStoreValue<std::wstring>);
00207 
00208 typedef EventStoreValue<int> EventStoreInt;
00209 typedef EventStoreValue<double> EventStoreDouble;
00210 typedef EventStoreValue<string> EventStoreString;
00211 typedef EventStoreValue<wstring> EventStoreWstring;
00212 
00213 #include "eventParameter.I"
00214 
00215 // Tell GCC that we'll take care of the instantiation explicitly here.
00216 #ifdef __GNUC__
00217 #pragma interface
00218 #endif
00219 
00220 #endif
00221 
 All Classes Functions Variables Enumerations