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
00011  *  wanted.
00012  *
00013  *  Make sure your own DoStart and DoStop call BehaviorBase::DoStart (or Stop) to allow
00014  *  the auto-deletion from reference counting... otherwise you'll get memory leaks if you
00015  *  rely on the reference counting.
00016  *
00017  *  For an empty behavior boilerplate file to help you get started quickly, try
00018  *  <a href="http://cvs.tekkotsu.org/cgi-bin/viewcvs.cgi/Tekkotsu/docs/behavior_header.h?rev=HEAD&content-type=text/vnd.viewcvs-markup"><i>Tekkotsu</i><tt>/docs/behavior_header.h</tt></a>:
00019  * 
00020  *  But it would probably still be a good idea to go through the "<a
00021  *  href="../FirstBehavior.html">First Behavior</a>" tutorial to get a better idea of
00022  *  what's going on.
00023  */
00024 class BehaviorBase : public ReferenceCounter, public EventListener {
00025  public:
00026   //! constructor
00027   BehaviorBase() : ReferenceCounter(), EventListener(), started(false) {}
00028   //! 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..
00029   BehaviorBase(const BehaviorBase& b) : ReferenceCounter(b), EventListener(b), started(b.started) {}
00030   //! 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..
00031   BehaviorBase& operator=(const BehaviorBase& b) { ReferenceCounter::operator=(b); EventListener::operator=(b); started=b.started; return *this; }
00032 
00033   //! destructor - if is active when deleted, will call DoStop() first
00034   virtual ~BehaviorBase() {
00035     SetAutoDelete(false);
00036     if(started)
00037       DoStop();
00038     //{ if(started) { references++; DoStop(); references--; } }
00039   }
00040   
00041   //! By default, merely adds to the reference counter (through AddReference()); Note you should still call this from your overriding methods
00042   virtual void DoStart() {
00043     //std::cout << getName() << " started " << this << std::endl;
00044     if(!started) {
00045       started=true;
00046       AddReference();
00047     }
00048   }
00049 
00050   //! 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 )
00051   virtual void DoStop() {
00052     //std::cout << getName() << " stopped " << this << std::endl;
00053     if(started) {
00054       started=false;
00055       RemoveReference();
00056     }
00057   }
00058   
00059   //! By defining here, allows you to get away with not supplying a processEvent() function for the EventListener interface.  By default, does nothing.
00060   virtual void processEvent(const EventBase& /*event*/) {};
00061 
00062   /*  virtual void AddReference() {
00063       std::cout << getName() << " AddReference()==" << GetReferences() << ' ' << this << std::endl;
00064       ReferenceCounter::AddReference();
00065       }
00066       
00067       virtual void RemoveReference() {
00068       std::cout << getName() << " RemoveReference()==" << GetReferences() << ' ' << this << std::endl;
00069       ReferenceCounter::RemoveReference();
00070       }
00071   */
00072   
00073   //! Identifies the behavior in menus and such
00074   virtual std::string getName() const =0;
00075 
00076   //! Gives a short description of what this class of behaviors does... you should override this (but don't have to)
00077   static std::string getClassDescription() { return ""; }
00078 
00079   //! 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()
00080   virtual std::string getDescription() const { return getClassDescription(); }
00081 
00082   //! Returns true if the behavior is currently running
00083   virtual bool isActive() const { return started; }
00084 
00085  protected:
00086   bool started; //!< true when the behavior is active
00087 };
00088 
00089 /*! @file
00090  * @brief Defines BehaviorBase from which all Behaviors should inherit
00091  * @author ejt (Creator)
00092  *
00093  * $Author: ejt $
00094  * $Name: tekkotsu-2_2 $
00095  * $Revision: 1.14 $
00096  * $State: Exp $
00097  * $Date: 2004/03/24 06:38:21 $
00098  */
00099 
00100 #endif

Tekkotsu v2.2
Generated Tue Oct 19 14:19:13 2004 by Doxygen 1.3.9.1