BehaviorSwitchControl.hGo to the documentation of this file.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, BehaviorBase* beh, BehaviorGroup* bg=NULL)
00033 : ControlBase(n), behgrp(bg), mybeh(beh) {
00034 if(behgrp!=NULL)
00035 behgrp->AddReference();
00036 if(mybeh!=NULL)
00037 mybeh->AddReference();
00038 }
00039
00040 BehaviorSwitchControlBase(BehaviorBase* beh, BehaviorGroup* bg=NULL)
00041 : ControlBase(), behgrp(bg), mybeh(beh) {
00042 if(behgrp!=NULL)
00043 behgrp->AddReference();
00044 mybeh->AddReference();
00045 }
00046
00047
00048 virtual ~BehaviorSwitchControlBase() {
00049
00050 if(mybeh!=NULL)
00051 stop();
00052 if(behgrp!=NULL) {
00053 behgrp->RemoveReference();
00054 behgrp=NULL;
00055 }
00056 if(mybeh!=NULL)
00057 mybeh->RemoveReference();
00058 }
00059
00060
00061
00062 virtual BehaviorSwitchControlBase* start() { if(!isRunning()) { stopother(); startmine(); } return this; }
00063
00064
00065 virtual BehaviorSwitchControlBase* stop() { if(isRunning()) stopother(); return this; }
00066
00067
00068 virtual BehaviorSwitchControlBase* toggle() { if(isRunning()) stopother(); else { stopother(); startmine(); } return this; }
00069
00070
00071
00072 virtual ControlBase * activate(MotionManager::MC_ID display, Socket * gui) {
00073 if(slotsSize()==0) {
00074 toggle();
00075 return NULL;
00076 } else
00077 return ControlBase::activate(display,gui);
00078 }
00079
00080
00081 virtual std::string getName() const {
00082 if(mybeh==NULL)
00083 return ControlBase::getName();
00084 return (mybeh->isActive()?'#':'-')+mybeh->getName();
00085 }
00086 virtual std::string getDescription() const {
00087 if(mybeh==NULL)
00088 return ControlBase::getDescription();
00089 return "Class "+mybeh->getClassName()+": "+mybeh->getDescription();
00090 }
00091
00092 protected:
00093
00094 virtual void stopother() {
00095 if(behgrp==NULL) {
00096 if(mybeh->isActive())
00097 mybeh->DoStop();
00098 } else if(behgrp->curBehavior!=NULL) {
00099 behgrp->curBehavior->DoStop();
00100 behgrp->curBehavior=NULL;
00101 }
00102 }
00103
00104
00105 virtual void startmine() {
00106 if(behgrp!=NULL)
00107 behgrp->curBehavior=mybeh;
00108 mybeh->DoStart();
00109 }
00110
00111
00112 virtual bool isRunning() const {
00113 if(mybeh==NULL)
00114 return false;
00115
00116 if(behgrp==NULL)
00117 return mybeh->isActive();
00118
00119 return (behgrp->curBehavior==mybeh);
00120 }
00121
00122 BehaviorGroup * behgrp;
00123 BehaviorBase* mybeh;
00124
00125 private:
00126 BehaviorSwitchControlBase(const BehaviorSwitchControlBase&);
00127 BehaviorSwitchControlBase operator=(const BehaviorSwitchControlBase&);
00128 };
00129
00130
00131
00132
00133
00134 template < class B, class Al = Factory< B > >
00135 class BehaviorSwitchControl : public BehaviorSwitchControlBase {
00136 public:
00137
00138 BehaviorSwitchControl(const std::string& n, bool retain=false)
00139 : BehaviorSwitchControlBase(n,NULL,NULL), retained(retain)
00140 {}
00141
00142 BehaviorSwitchControl(B* beh, BehaviorGroup* bg=NULL)
00143 : BehaviorSwitchControlBase(beh,bg), retained(true)
00144 {}
00145
00146 BehaviorSwitchControl(const std::string& n, B* beh, BehaviorGroup* bg=NULL, bool retain=false)
00147 : BehaviorSwitchControlBase(n,beh,bg), retained(retain)
00148 {
00149 if(!retained) {
00150
00151 if(!mybeh->isActive()) {
00152
00153 mybeh->DoStart();
00154 bool stopped=!mybeh->isActive();
00155 mybeh->RemoveReference();
00156 if(stopped)
00157 mybeh=NULL;
00158 } else
00159 mybeh->RemoveReference();
00160 }
00161 if(behgrp!=NULL) {
00162 if(mybeh->isActive()) {
00163 if(behgrp->curBehavior!=NULL)
00164 behgrp->curBehavior->DoStop();
00165 behgrp->curBehavior=mybeh;
00166 } else if(!retained) {
00167 if(behgrp->curBehavior!=NULL)
00168 behgrp->curBehavior->DoStop();
00169 behgrp->curBehavior=NULL;
00170 }
00171 }
00172 }
00173
00174 BehaviorSwitchControl(const std::string& n, BehaviorGroup* bg, bool retain=false)
00175 : BehaviorSwitchControlBase(n,NULL,bg), retained(retain)
00176 {}
00177
00178
00179 virtual ~BehaviorSwitchControl() {
00180 stop();
00181 if(behgrp!=NULL) {
00182 behgrp->RemoveReference();
00183 behgrp=NULL;
00184 }
00185 if(mybeh!=NULL && retained)
00186 mybeh->RemoveReference();
00187 mybeh=NULL;
00188 }
00189
00190 virtual std::string getName() const {
00191 if(!isValid())
00192 return ControlBase::getName();
00193 else
00194 return BehaviorSwitchControlBase::getName();
00195 }
00196 virtual std::string getDescription() const {
00197 if(!isValid() || mybeh->getDescription().size()==0)
00198 return B::getClassDescription();
00199 else
00200 return BehaviorSwitchControlBase::getDescription();
00201 }
00202
00203
00204 protected:
00205
00206 virtual void stopother() {
00207 if(behgrp==NULL) {
00208 if(mybeh!=NULL) {
00209 if(mybeh->isActive()) {
00210 mybeh->DoStop();
00211 if(!retained)
00212 mybeh=NULL;
00213 } else
00214 ASSERT(retained,"null group, non-null not retained beh, not active, did you call inherited DoStart/DoStop in your Behavior?");
00215 }
00216 } else if(behgrp->curBehavior!=NULL) {
00217 behgrp->curBehavior->DoStop();
00218 if(behgrp->curBehavior==mybeh)
00219 mybeh=NULL;
00220 behgrp->curBehavior=NULL;
00221 }
00222 }
00223
00224 virtual void startmine() {
00225 if(!retained) {
00226 Al allocator;
00227 mybeh=allocator.construct();
00228 mybeh->setName(getName());
00229 if(behgrp!=NULL)
00230 behgrp->curBehavior=mybeh;
00231 } else {
00232 if(mybeh==NULL) {
00233 Al allocator;
00234 mybeh=allocator.construct();
00235 mybeh->setName(getName());
00236 mybeh->AddReference();
00237 }
00238 if(behgrp!=NULL)
00239 behgrp->curBehavior=mybeh;
00240 }
00241 mybeh->AddReference();
00242 mybeh->DoStart();
00243 bool stopped=(!mybeh->isActive() && !retained);
00244 mybeh->RemoveReference();
00245 if(stopped) {
00246 if(behgrp!=NULL && behgrp->curBehavior==mybeh)
00247 behgrp->curBehavior=NULL;
00248 mybeh=NULL;
00249 }
00250 }
00251
00252
00253 virtual bool isValid() const {
00254 if(isRunning())
00255 return true;
00256 return retained;
00257 }
00258
00259 private:
00260 bool retained;
00261 BehaviorSwitchControl(const BehaviorSwitchControl&);
00262 BehaviorSwitchControl operator=(const BehaviorSwitchControl&);
00263 };
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 #endif
|