Homepage | Demos | Overview | Downloads | Tutorials | Reference | Credits |
BehaviorBase.hGo to the documentation of this file.00001 //-*-c++-*- 00002 #ifndef INCLUDED_BehaviorBase_h_ 00003 #define INCLUDED_BehaviorBase_h_ 00004 00005 #include "Events/EventListener.h" 00006 #include "Shared/ReferenceCounter.h" 00007 #include <string> 00008 00009 //! The basis from which all other Behaviors should inherit 00010 /*! Makes use of ReferenceCounter so that behaviors can automatically delete themselves if wanted\n 00011 * Make sure your own DoStart and DoStop call BehaviorBase::DoStart (or Stop) to allow this behavior... otherwise you'll get memory leaks */ 00012 class BehaviorBase : public ReferenceCounter, public EventListener { 00013 public: 00014 //! constructor 00015 BehaviorBase() : ReferenceCounter(), EventListener(), started(false) {} 00016 //! copy constructor; assumes subclass handles copying approriately - i.e. if @a b is active, the copy will be as well, even though DoStart was never called.. 00017 BehaviorBase(const BehaviorBase& b) : ReferenceCounter(b), EventListener(b), started(b.started) {} 00018 //! assignment operator; assumes subclass handles assignment appropriately - i.e. if @a b is active, the copy will be as well, even though DoStart was never called.. 00019 BehaviorBase& operator=(const BehaviorBase& b) { ReferenceCounter::operator=(b); EventListener::operator=(b); started=b.started; return *this; } 00020 00021 //! destructor - if is active when deleted, will call DoStop() first 00022 virtual ~BehaviorBase() { 00023 SetAutoDelete(false); 00024 if(started) 00025 DoStop(); 00026 //{ if(started) { references++; DoStop(); references--; } } 00027 } 00028 00029 //! By default, merely adds to the reference counter (through AddReference()); Note you should still call this from your overriding methods 00030 virtual void DoStart() { 00031 //std::cout << getName() << " started " << this << std::endl; 00032 if(!started) { 00033 started=true; 00034 AddReference(); 00035 } 00036 } 00037 00038 //! By default, subtracts from the reference counter (RemoveReference()), and thus may deletex if zero; Don't forget to still call this when you override this; <b>Warning:</b> call this at the <i>end</i> of your DoStop(), not beginning (it might @c delete @c this ) 00039 virtual void DoStop() { 00040 //std::cout << getName() << " stopped " << this << std::endl; 00041 if(started) { 00042 started=false; 00043 RemoveReference(); 00044 } 00045 } 00046 00047 //! By defining here, allows you to get away with not supplying a processEvent() function for the EventListener interface. By default, does nothing. 00048 virtual void processEvent(const EventBase& /*event*/) {}; 00049 00050 /* virtual void AddReference() { 00051 std::cout << getName() << " AddReference()==" << GetReferences() << ' ' << this << std::endl; 00052 ReferenceCounter::AddReference(); 00053 } 00054 00055 virtual void RemoveReference() { 00056 std::cout << getName() << " RemoveReference()==" << GetReferences() << ' ' << this << std::endl; 00057 ReferenceCounter::RemoveReference(); 00058 } 00059 */ 00060 00061 //! Identifies the behavior in menus and such 00062 virtual std::string getName() const =0; 00063 00064 //! Gives a short description of what this class of behaviors does... you should override this (but don't have to) 00065 static std::string getClassDescription() { return ""; } 00066 00067 //! Gives a short description of what this particular instantiation does (in case a more specific description is needed on an individual basis) By default simply returns getClassDescription() 00068 virtual std::string getDescription() const { return getClassDescription(); } 00069 00070 //! Returns true if the behavior is currently running 00071 virtual bool isActive() const { return started; } 00072 00073 protected: 00074 bool started; //!< true when the behavior is active 00075 }; 00076 00077 /*! @file 00078 * @brief Defines BehaviorBase from which all Behaviors should inherit 00079 * @author ejt (Creator) 00080 * 00081 * $Author: ejt $ 00082 * $Name: tekkotsu-2_1 $ 00083 * $Revision: 1.13 $ 00084 * $State: Exp $ 00085 * $Date: 2004/02/09 22:45:28 $ 00086 */ 00087 00088 #endif |
Tekkotsu v2.1 |
Generated Tue Mar 16 23:19:12 2004 by Doxygen 1.3.5 |