BehaviorSwitchControl.h
Go 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/Factories.h"
00009
00010
00011 class BehaviorSwitchControlBase : public ControlBase {
00012 public:
00013
00014
00015
00016
00017
00018
00019
00020 class BehaviorGroup : public ReferenceCounter {
00021 public:
00022 BehaviorGroup() : curBehavior(NULL), members() { }
00023 ~BehaviorGroup() { if(curBehavior!=NULL) curBehavior->stop(); }
00024 BehaviorBase * curBehavior;
00025 std::set<BehaviorSwitchControlBase*> members;
00026 private:
00027 BehaviorGroup(const BehaviorGroup&);
00028 BehaviorGroup operator=(const BehaviorGroup&);
00029 };
00030
00031
00032
00033 BehaviorSwitchControlBase(const std::string& n, BehaviorBase* beh, BehaviorGroup* bg=NULL)
00034 : ControlBase(n), behgrp(NULL), mybeh(beh)
00035 {
00036 if(mybeh!=NULL) {
00037 mybeh->addReference();
00038 mybeh->setName(n);
00039 if(mybeh->isActive())
00040 mybeh->addReference();
00041 }
00042 setGroup(bg);
00043 }
00044
00045
00046 BehaviorSwitchControlBase(BehaviorBase* beh, BehaviorGroup* bg=NULL)
00047 : ControlBase(beh->getName()), behgrp(NULL), mybeh(beh)
00048 {
00049 mybeh->addReference();
00050 if(mybeh->isActive())
00051 mybeh->addReference();
00052 setGroup(bg);
00053 }
00054
00055
00056 virtual ~BehaviorSwitchControlBase() {
00057
00058 stop();
00059 setGroup(NULL);
00060 if(mybeh!=NULL)
00061 mybeh->removeReference();
00062 mybeh=NULL;
00063 }
00064
00065
00066
00067 virtual BehaviorSwitchControlBase* start() { if(!isRunning()) { stopother(); startmine(); } return this; }
00068
00069
00070 virtual BehaviorSwitchControlBase* stop() { if(isRunning()) stopother(); return this; }
00071
00072
00073 virtual BehaviorSwitchControlBase* toggle() { if(isRunning()) stopother(); else { stopother(); startmine(); } return this; }
00074
00075 virtual ControlBase * takeInput(const std::string& msg);
00076
00077
00078
00079 virtual void setGroup(BehaviorGroup* bg);
00080
00081
00082
00083 virtual ControlBase * activate(MC_ID display, Socket * gui);
00084
00085
00086 virtual std::string getName() const;
00087
00088 virtual std::string getDescription() const;
00089
00090
00091 virtual bool isRunning() const;
00092
00093 protected:
00094
00095 virtual void stopother();
00096
00097
00098 virtual void startmine();
00099
00100
00101 virtual void notifyGroupMembers();
00102
00103
00104 virtual void behaviorStopped() {}
00105
00106 BehaviorGroup * behgrp;
00107 BehaviorBase* mybeh;
00108
00109 private:
00110 BehaviorSwitchControlBase(const BehaviorSwitchControlBase&);
00111 BehaviorSwitchControlBase operator=(const BehaviorSwitchControlBase&);
00112 };
00113
00114
00115
00116
00117
00118 template < class B, class Al = typename Factory0Arg<B>::template Factory<B> >
00119 class BehaviorSwitchControl : public BehaviorSwitchControlBase {
00120 public:
00121
00122 BehaviorSwitchControl(const std::string& n, bool retain=false)
00123 : BehaviorSwitchControlBase(n,NULL,NULL), retained(retain), startref(NULL)
00124 {}
00125
00126 BehaviorSwitchControl(B* beh, BehaviorGroup* bg=NULL)
00127 : BehaviorSwitchControlBase(beh,bg), retained(true), startref(NULL)
00128 {}
00129
00130 BehaviorSwitchControl(const std::string& n, B* beh, BehaviorGroup* bg=NULL, bool retain=false)
00131 : BehaviorSwitchControlBase(n,beh,bg), retained(retain), startref(NULL)
00132 {
00133 if(!retained) {
00134
00135 if(!mybeh->isActive()) {
00136 startmine();
00137 }
00138 mybeh->removeReference();
00139 }
00140 }
00141
00142 BehaviorSwitchControl(const std::string& n, BehaviorGroup* bg, bool retain=false)
00143 : BehaviorSwitchControlBase(n,NULL,bg), retained(retain), startref(NULL)
00144 {}
00145
00146
00147 virtual ~BehaviorSwitchControl() {
00148 stop();
00149 setGroup(NULL);
00150 if(mybeh!=NULL && retained)
00151 mybeh->removeReference();
00152 mybeh=NULL;
00153 }
00154
00155 virtual std::string getName() const {
00156 if(!isValid())
00157 return ControlBase::getName();
00158 else
00159 return BehaviorSwitchControlBase::getName();
00160 }
00161 virtual std::string getDescription() const {
00162 if(!isValid() || mybeh->getDescription().size()==0)
00163 return B::getClassDescription();
00164 else
00165 return BehaviorSwitchControlBase::getDescription();
00166 }
00167
00168 protected:
00169
00170 virtual void startmine() {
00171 if(!retained) {
00172 Al allocator;
00173 mybeh=allocator();
00174 mybeh->setName(getName());
00175 } else {
00176 if(mybeh==NULL) {
00177 Al allocator;
00178 mybeh=allocator();
00179 mybeh->setName(getName());
00180 mybeh->addReference();
00181 }
00182 }
00183 startref=mybeh;
00184 startref->addReference();
00185 BehaviorSwitchControlBase::startmine();
00186 }
00187
00188
00189 virtual bool isRunning() const {
00190 if(BehaviorSwitchControlBase::isRunning())
00191 return true;
00192 else if(startref!=NULL)
00193 const_cast<BehaviorSwitchControl<B,Al>*>(this)->stopother();
00194 return false;
00195 }
00196
00197
00198 virtual bool isValid() const {
00199 if(isRunning())
00200 return true;
00201 return retained;
00202 }
00203
00204 virtual void behaviorStopped() {
00205 if(!retained)
00206 mybeh=NULL;
00207 if(startref!=NULL) {
00208 startref->removeReference();
00209 startref=NULL;
00210 }
00211 }
00212
00213 bool retained;
00214 BehaviorBase * startref;
00215
00216 private:
00217 BehaviorSwitchControl(const BehaviorSwitchControl&);
00218 BehaviorSwitchControl operator=(const BehaviorSwitchControl&);
00219 };
00220
00221
00222
00223
00224
00225
00226 #endif