Homepage Demos Overview Downloads Tutorials Reference
Credits

BehaviorBase.h

Go 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, and deletes if zero; Note you should still call this when you override this; Warning call this at the end 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)
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_0 $
00083  * $Revision: 1.12 $
00084  * $State: Exp $
00085  * $Date: 2004/01/18 10:16:55 $
00086  */
00087 
00088 #endif

Tekkotsu v2.0
Generated Wed Jan 21 03:20:27 2004 by Doxygen 1.3.4