00001
00002 #ifndef INCLUDED_BehaviorSwitchControl_h_
00003 #define INCLUDED_BehaviorSwitchControl_h_
00004
00005 #include "ControlBase.h"
00006 #include "Behaviors/BehaviorBase.h"
00007 #include "Shared/ReferenceCounter.h"
00008 #include "Shared/Factory.h"
00009 #include "Shared/debuget.h"
00010
00011
00012 class BehaviorSwitchControlBase : public ControlBase {
00013 public:
00014
00015
00016
00017
00018
00019
00020
00021 class BehaviorGroup : public ReferenceCounter {
00022 public:
00023 BehaviorGroup() : curBehavior(NULL) { }
00024 ~BehaviorGroup() { if(curBehavior!=NULL) curBehavior->DoStop(); }
00025 BehaviorBase * curBehavior;
00026 private:
00027 BehaviorGroup(const BehaviorGroup&);
00028 BehaviorGroup operator=(const BehaviorGroup&);
00029 };
00030
00031
00032 BehaviorSwitchControlBase(const std::string& n, BehaviorGroup* bg, bool retain)
00033 : ControlBase(n), retained(true), behgrp(bg) {
00034 retained=retain;
00035 if(behgrp!=NULL)
00036 behgrp->AddReference();
00037 }
00038
00039
00040 virtual ~BehaviorSwitchControlBase() {
00041 if(behgrp!=NULL)
00042 behgrp->RemoveReference();
00043 }
00044
00045
00046
00047 virtual BehaviorSwitchControlBase* start()=0;
00048 virtual BehaviorSwitchControlBase* stop()=0;
00049 virtual BehaviorSwitchControlBase* toggle()=0;
00050
00051
00052
00053 virtual ControlBase * activate(MotionManager::MC_ID display, Socket * gui) {
00054 if(slotsSize()==0) {
00055 toggle();
00056 return NULL;
00057 } else
00058 return ControlBase::activate(display,gui);
00059 }
00060
00061 protected:
00062 bool retained;
00063 BehaviorGroup * behgrp;
00064
00065 private:
00066 BehaviorSwitchControlBase(const BehaviorSwitchControlBase&);
00067 BehaviorSwitchControlBase operator=(const BehaviorSwitchControlBase&);
00068 };
00069
00070
00071
00072
00073
00074 template < class B, class Al = Factory< B > >
00075 class BehaviorSwitchControl : public BehaviorSwitchControlBase {
00076 public:
00077
00078 BehaviorSwitchControl(const std::string& n, bool retain=false)
00079 : BehaviorSwitchControlBase(n,NULL,retain), mybeh(NULL) {}
00080
00081 BehaviorSwitchControl(const std::string& n, B* beh, BehaviorGroup* bg=NULL)
00082 : BehaviorSwitchControlBase(n,bg,true), mybeh(beh) {
00083 retained=true;
00084 if(mybeh!=NULL)
00085 mybeh->AddReference();
00086 }
00087
00088 BehaviorSwitchControl(const std::string& n, BehaviorGroup* bg, bool retain=false)
00089 : BehaviorSwitchControlBase(n,bg,retain), mybeh(NULL) {}
00090
00091
00092 virtual ~BehaviorSwitchControl() {
00093 stop();
00094 if(mybeh!=NULL && retained)
00095 mybeh->RemoveReference();
00096 }
00097
00098 virtual BehaviorSwitchControl<B,Al>* start() { if(!isRunning()) { stopother(); startmine(); } return this; }
00099
00100 virtual BehaviorSwitchControl<B,Al>* stop() { if(isRunning()) stopother(); return this; }
00101
00102 virtual BehaviorSwitchControl<B,Al>* toggle() { if(isRunning()) stopother(); else { stopother(); startmine(); } return this; }
00103
00104
00105 virtual std::string getName() const {
00106 if(!isValid())
00107 return ControlBase::getName();
00108 else
00109 return (mybeh->isActive()?'#':'-')+mybeh->getName();
00110 }
00111 virtual std::string getDescription() const {
00112 if(!isValid() || mybeh->getDescription().size()==0)
00113 return B::getClassDescription();
00114 else
00115 return mybeh->getDescription();
00116 }
00117
00118
00119 protected:
00120
00121
00122 virtual void stopother() {
00123 if(behgrp==NULL) {
00124 if(mybeh!=NULL) {
00125 if(mybeh->isActive()) {
00126 mybeh->DoStop();
00127 if(!retained)
00128 mybeh=NULL;
00129 } else
00130 ASSERT(retained,"null group, non-null not retained beh, not active, did you call inherited DoStart/DoStop in your Behavior?");
00131 }
00132 } else if(behgrp->curBehavior!=NULL) {
00133 behgrp->curBehavior->DoStop();
00134 behgrp->curBehavior=NULL;
00135 }
00136 }
00137
00138
00139 virtual void startmine() {
00140 if(!retained) {
00141 mybeh=Al::construct();
00142 if(behgrp!=NULL)
00143 behgrp->curBehavior=mybeh;
00144 } else {
00145 if(mybeh==NULL) {
00146 mybeh=Al::construct();
00147 mybeh->AddReference();
00148 }
00149 if(behgrp!=NULL)
00150 behgrp->curBehavior=mybeh;
00151 }
00152 mybeh->DoStart();
00153 }
00154
00155
00156 virtual bool isRunning() const {
00157 if(mybeh==NULL)
00158 return false;
00159
00160 if(behgrp==NULL)
00161 return mybeh->isActive();
00162
00163 return (behgrp->curBehavior==mybeh);
00164 }
00165
00166
00167 virtual bool isValid() const {
00168 if(isRunning())
00169 return true;
00170 return retained;
00171 }
00172
00173 B* mybeh;
00174
00175 private:
00176 BehaviorSwitchControl(const BehaviorSwitchControl&);
00177 BehaviorSwitchControl operator=(const BehaviorSwitchControl&);
00178 };
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 #endif