Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/event/eventParameter.h

Go to the documentation of this file.
00001 // Filename: eventParameter.h
00002 // Created by:  drose (08Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef EVENTPARAMETER_H
00020 #define EVENTPARAMETER_H
00021 
00022 #include "pandabase.h"
00023 
00024 #include "typedef.h"
00025 #include "typedObject.h"
00026 #include "typedReferenceCount.h"
00027 #include "pointerTo.h"
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //       Class : EventParameter
00031 // Description : An optional parameter associated with an event.  Each
00032 //               event may have zero or more of these.  Each parameter
00033 //               stores a pointer to a TypedReferenceCount object,
00034 //               which of course could be pretty much anything.  To
00035 //               store a simple value like a double or a string, the
00036 //               EventParameter constructors transparently use the
00037 //               EventStoreValue template class, defined below.
00038 ////////////////////////////////////////////////////////////////////
00039 class EXPCL_PANDAEXPRESS EventParameter {
00040 PUBLISHED:
00041   INLINE EventParameter();
00042   INLINE EventParameter(const TypedReferenceCount *ptr);
00043   INLINE EventParameter(int value);
00044   INLINE EventParameter(double value);
00045   INLINE EventParameter(const string &value);
00046 
00047   INLINE EventParameter(const EventParameter &copy);
00048   INLINE EventParameter &operator = (const EventParameter &copy);
00049 
00050   // These functions are conveniences to easily determine if the
00051   // EventParameter is one of the predefined parameter types, and
00052   // retrieve the corresponding value.  Of course, it is possible that
00053   // the EventParameter is some user-defined type, and is none of
00054   // these.
00055   INLINE bool is_empty() const;
00056   INLINE bool is_int() const;
00057   INLINE int get_int_value() const;
00058   INLINE bool is_double() const;
00059   INLINE double get_double_value() const;
00060   INLINE bool is_string() const;
00061   INLINE string get_string_value() const;
00062 
00063   INLINE TypedReferenceCount *get_ptr() const;
00064 
00065   void output(ostream &out) const;
00066 
00067 private:
00068   PT(TypedReferenceCount) _ptr;
00069 };
00070 
00071 INLINE ostream &operator << (ostream &out, const EventParameter &param);
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //       Class : EventStoreValueBase
00075 // Description : A non-template base class of EventStoreValue (below),
00076 //               which serves mainly to define the placeholder for the
00077 //               virtual output function.
00078 ////////////////////////////////////////////////////////////////////
00079 class EXPCL_PANDAEXPRESS EventStoreValueBase : public TypedReferenceCount {
00080 public:
00081   virtual void output(ostream &out) const=0;
00082 
00083 public:
00084   virtual TypeHandle get_type() const {
00085     return get_class_type();
00086   }
00087   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00088   static TypeHandle get_class_type() {
00089     return _type_handle;
00090   }
00091   static void init_type() {
00092     TypedReferenceCount::init_type();
00093     register_type(_type_handle, "EventStoreValueBase",
00094                   TypedReferenceCount::get_class_type());
00095   }
00096 
00097 private:
00098   static TypeHandle _type_handle;
00099 };
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //       Class : EventStoreValue
00103 // Description : A handy class object for storing simple values (like
00104 //               integers or strings) passed along with an Event.
00105 //               This is essentially just a wrapper around whatever
00106 //               data type you like, to make it a TypedReferenceCount
00107 //               object which can be passed along inside an
00108 //               EventParameter.
00109 ////////////////////////////////////////////////////////////////////
00110 template<class Type>
00111 class EventStoreValue : public EventStoreValueBase {
00112 public:
00113   EventStoreValue(const Type &value) : _value(value) { }
00114 
00115   INLINE void set_value(const Type &value);
00116   INLINE const Type &get_value() const;
00117 
00118   virtual void output(ostream &out) const;
00119 
00120   Type _value;
00121 
00122 public:
00123   static TypeHandle get_class_type() {
00124     return _type_handle;
00125   }
00126   static void init_type(const string &type_name = "UndefinedEventStoreValue") {
00127     EventStoreValueBase::init_type();
00128     _type_handle = register_dynamic_type
00129       (type_name, EventStoreValueBase::get_class_type());
00130   }
00131   virtual TypeHandle get_type() const {
00132     return get_class_type();
00133   }
00134   virtual TypeHandle force_init_type() {
00135     // In this case, we can't do anything, since we don't have the
00136     // class' type_name.
00137     return get_class_type();
00138   }
00139 
00140 private:
00141   static TypeHandle _type_handle;
00142 };
00143 
00144 EXPORT_TEMPLATE_CLASS(EXPCL_PANDAEXPRESS, EXPTP_PANDAEXPRESS, EventStoreValue<int>);
00145 EXPORT_TEMPLATE_CLASS(EXPCL_PANDAEXPRESS, EXPTP_PANDAEXPRESS, EventStoreValue<double>);
00146 EXPORT_TEMPLATE_CLASS(EXPCL_PANDAEXPRESS, EXPTP_PANDAEXPRESS, EventStoreValue<std::string>);
00147 
00148 typedef EventStoreValue<int> EventStoreInt;
00149 typedef EventStoreValue<double> EventStoreDouble;
00150 typedef EventStoreValue<string> EventStoreString;
00151 
00152 #include "eventParameter.I"
00153 
00154 // Tell GCC that we'll take care of the instantiation explicitly here.
00155 #ifdef __GNUC__
00156 #pragma interface
00157 #endif
00158 
00159 #endif
00160 

Generated on Fri May 2 00:38:17 2003 for Panda by doxygen1.3