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   //! Identifies the behavior in menus and such
00051   virtual std::string getName() const =0;
00052 
00053   //! Gives a short description of what this class of behaviors does... you should override this (but don't have to)
00054   static std::string getClassDescription() { return ""; }
00055 
00056   //! Gives a short description of what this particular instantiation does (in case a more specific description is needed)
00057   virtual std::string getDescription() const { return getClassDescription(); }
00058 
00059   //! Returns true if the behavior is currently running
00060   virtual bool isActive() const { return started; }
00061 
00062  protected:
00063   bool started; //!< true when the behavior is active
00064 };
00065 
00066 /*! @file
00067  * @brief Defines BehaviorBase from which all Behaviors should inherit
00068  * @author ejt (Creator)
00069  *
00070  * $Author: ejt $
00071  * $Name: tekkotsu-1_5 $
00072  * $Revision: 1.9 $
00073  * $State: Rel $
00074  * $Date: 2003/10/10 15:54:04 $
00075  */
00076 
00077 #endif

Tekkotsu v1.5
Generated Fri Oct 10 15:51:58 2003 by Doxygen 1.3.4