Changeset 7185
- Timestamp:
- Aug 19, 2010, 2:11:28 AM (14 years ago)
- Location:
- code/branches/consolecommands3/src/libraries/core
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc
r7179 r7185 80 80 _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive)); 81 81 82 _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier()) 83 { 84 this->state_ = state; 82 _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier()) 83 { 84 this->bActive_ = true; 85 this->bInitialized_ = bInitialized; 85 86 _ConsoleCommand::registerCommand(group, name, this); 86 87 } … … 110 111 } 111 112 112 void _ConsoleCommand::setActive(bool bActive) 113 { 114 if (bActive) 115 { 116 if (this->state_ == State::Inactive) 117 this->state_ = State::Active; 118 else if (this->state_ == State::UninitializedInactive) 119 this->state_ = State::UninitializedActive; 120 } 121 else 122 { 123 if (this->state_ == State::Active) 124 this->state_ = State::Inactive; 125 else if (this->state_ == State::UninitializedActive) 126 this->state_ = State::UninitializedInactive; 127 } 128 } 129 130 void _ConsoleCommand::setInitialized(bool bInitialized) 131 { 132 if (bInitialized) 133 { 134 if (this->state_ == State::UninitializedActive) 135 this->state_ = State::Active; 136 else if (this->state_ == State::UninitializedInactive) 137 this->state_ = State::Inactive; 138 } 139 else 140 { 141 if (this->state_ == State::Active) 142 this->state_ = State::UninitializedActive; 143 else if (this->state_ == State::Inactive) 144 this->state_ = State::UninitializedInactive; 145 } 146 } 147 148 void _ConsoleCommand::setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode) 113 bool _ConsoleCommand::setFunctor(Functor* functor, bool bForce) 149 114 { 150 115 if (!functor) 151 116 { 152 this-> setInitialized(false);153 return ;154 } 155 156 if (! this->functionHeaderMatches(functor))117 this->bInitialized_ = false; 118 return true; 119 } 120 121 if (!bForce && !this->functionHeaderMatches(functor)) 157 122 { 158 123 COUT(1) << "Error: Couldn't assign new function to console command with name \"" << this->getName() << "\", headers don't match." << std::endl; 159 return; 160 } 161 162 switch (mode) 163 { 164 default: 165 case _ConsoleCommand::ObjectPointer::Null: 166 { 167 this->functor_ = functor; 168 } 169 break; 170 171 case _ConsoleCommand::ObjectPointer::RawCopy: 172 { 173 void* object = (this->functor_) ? this->functor_->getRawObjectPointer() : 0; 174 175 this->functor_ = functor; 176 177 if (!this->functor_->getBaseObject()) 178 this->functor_->setRawObjectPointer(object); 179 } 180 break; 181 182 case _ConsoleCommand::ObjectPointer::CastViaBaseObject: 183 { 184 BaseObject* object = (this->functor_) ? this->functor_->getBaseObject() : 0; 185 186 this->functor_ = functor; 187 188 if (!this->functor_->getBaseObject()) 189 this->functor_->setBaseObject(object); 190 } 191 break; 192 } 124 return false; 125 } 126 127 this->functor_ = functor; 128 this->bInitialized_ = true; 129 return true; 130 } 131 132 void _ConsoleCommand::pushFunctor(Functor* functor, bool bForce) 133 { 134 Functor* oldfunctor = this->getFunctor(); 135 136 if (this->setFunctor(functor, bForce)); 137 this->functorStack_.push(oldfunctor); 138 } 139 140 void _ConsoleCommand::popFunctor() 141 { 142 Functor* newfunctor = 0; 143 if (!this->functorStack_.empty()) 144 { 145 newfunctor = this->functorStack_.top(); 146 this->functorStack_.pop(); 147 } 148 this->setFunctor(newfunctor); 149 } 150 151 Functor* _ConsoleCommand::getFunctor() const 152 { 153 if (this->bInitialized_) 154 return this->functor_; 155 else 156 return 0; 193 157 } 194 158 … … 198 162 { 199 163 assert(false); 200 return false;201 } 202 return (functor->getHeaderIdentifier() == this->funct or_->getHeaderIdentifier());164 return true; 165 } 166 return (functor->getHeaderIdentifier() == this->functionHeader_); 203 167 } 204 168 … … 207 171 if (this->functor_) 208 172 this->functor_->setRawObjectPointer(object); 209 } 210 211 void _ConsoleCommand::setObject(BaseObject* object) 173 else if (object) 174 COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl; 175 } 176 177 void _ConsoleCommand::pushObject(void* object) 212 178 { 213 179 if (this->functor_) 214 this->functor_->setBaseObject(object); 180 { 181 this->objectStack_.push(this->getObject()); 182 this->setObject(object); 183 } 184 else 185 COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl; 186 } 187 188 void _ConsoleCommand::popObject() 189 { 190 void* newobject = 0; 191 if (!this->objectStack_.empty()) 192 { 193 newobject = this->objectStack_.top(); 194 this->objectStack_.pop(); 195 } 196 this->setObject(newobject); 197 } 198 199 void* _ConsoleCommand::getObject() const 200 { 201 if (this->functor_) 202 return this->functor_->getRawObjectPointer(); 203 else 204 return 0; 215 205 } 216 206 -
code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h
r7179 r7185 32 32 #include "CorePrereqs.h" 33 33 34 #include <stack> 34 35 #include <boost/preprocessor/cat.hpp> 35 36 #include <boost/preprocessor/facilities/expand.hpp> … … 179 180 180 181 #define _DeclareConsoleCommandGeneric(group, name, functionpointer) \ 181 orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), orxonox::_ConsoleCommand::State::UninitializedActive))182 orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false)) 182 183 183 184 … … 193 194 194 195 public: 195 struct State196 {197 enum Enum198 {199 UninitializedActive,200 UninitializedInactive,201 Active,202 Inactive203 };204 };205 206 struct ObjectPointer207 {208 enum Enum209 {210 Null,211 RawCopy,212 CastViaBaseObject213 };214 };215 216 196 struct _ConsoleCommandManipulator 217 197 { … … 220 200 221 201 template <class F> 222 inline _ConsoleCommandManipulator& setFunction(F function, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null)223 { if (this->command_) { this->command_->setFunctor(createFunctor(function), mode); } return *this; }202 inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false) 203 { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; } 224 204 template <class F, class O> 225 inline _ConsoleCommandManipulator& setFunction(F function, O* object) 226 { if (this->command_) { this->command_->setFunctor(createFunctor(function, object)); } return *this; } 227 inline _ConsoleCommandManipulator& setFunction(Functor* functor) 228 { if (this->command_) { this->command_->setFunctor(functor); } return *this; } 229 inline _ConsoleCommandManipulator& setFunction(const _ConsoleCommand* command) 230 { if (this->command_) { this->command_->setFunctor(command->functor_); } return *this; } 231 inline _ConsoleCommandManipulator& setFunction(const _ConsoleCommandManipulator& manipulator) 232 { if (this->command_) { this->command_->setFunctor(manipulator.command_->functor_); } return *this; } 205 inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false) 206 { if (this->command_) { this->command_->setFunctor(createFunctor(function, object), bForce); } return *this; } 207 inline _ConsoleCommandManipulator& setFunction(Functor* functor, bool bForce = false) 208 { if (this->command_) { this->command_->setFunctor(functor, bForce); } return *this; } 233 209 234 210 template <class F> 235 inline _ConsoleCommandManipulator& pushFunction(F function, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null)236 { if (this->command_) { this->command_->pushFunctor(createFunctor(function), mode); } return *this; }211 inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false) 212 { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; } 237 213 template <class F, class O> 238 inline _ConsoleCommandManipulator& pushFunction(F function, O* object) 239 { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object)); } return *this; } 240 inline _ConsoleCommandManipulator& pushFunction(Functor* functor) 241 { if (this->command_) { this->command_->pushFunctor(functor); } return *this; } 242 inline _ConsoleCommandManipulator& pushFunction(const _ConsoleCommand* command) 243 { if (this->command_) { this->command_->pushFunctor(command->functor_); } return *this; } 244 inline _ConsoleCommandManipulator& pushFunction(const _ConsoleCommandManipulator& manipulator) 245 { if (this->command_) { this->command_->pushFunctor(manipulator.command_->functor_); } return *this; } 214 inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false) 215 { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object), bForce); } return *this; } 216 inline _ConsoleCommandManipulator& pushFunction(Functor* functor, bool bForce = false) 217 { if (this->command_) { this->command_->pushFunctor(functor, bForce); } return *this; } 246 218 247 219 inline _ConsoleCommandManipulator& popFunction() … … 250 222 inline _ConsoleCommandManipulator& setObject(void* object) 251 223 { if (this->command_) { this->command_->setObject(object); } return *this; } 252 inline _ConsoleCommandManipulator& setObject(BaseObject* object) 253 { if (this->command_) { this->command_->setObject(object); } return *this; } 224 inline _ConsoleCommandManipulator& pushObject(void* object) 225 { if (this->command_) { this->command_->pushObject(object); } return *this; } 226 inline _ConsoleCommandManipulator& popObject() 227 { if (this->command_) { this->command_->popObject(); } return *this; } 228 229 inline void* getObject() const 230 { if (this->command_) { return this->command_->getObject(); } else { return 0; } } 254 231 255 232 inline _ConsoleCommandManipulator& setActive(bool bActive) … … 261 238 262 239 public: 263 _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state = State::Active);240 _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true); 264 241 265 242 _ConsoleCommand& addShortcut(); … … 268 245 _ConsoleCommand& addGroup(const std::string& group, const std::string& name); 269 246 270 void setActive(bool bActive); 271 inline State::Enum getState() const 272 { return this->state_; } 247 inline void setActive(bool bActive) 248 { this->bActive_ = bActive; } 273 249 inline bool isActive() const 274 { return (this->state_ == State::Active); } 250 { return (this->bActive_ && this->bInitialized_); } 251 252 inline _ConsoleCommandManipulator getManipulator() const 253 { return this; } 275 254 276 255 static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands() … … 281 260 static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); 282 261 283 inline _ConsoleCommandManipulator getManipulator() const284 { return this; }285 286 262 private: 287 263 static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap(); 288 264 static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command); 289 265 290 void setInitialized(bool bInitialized); 291 292 void setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null); 293 void pushFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null); 266 bool setFunctor(Functor* functor, bool bForce = false); 267 void pushFunctor(Functor* functor, bool bForce = false); 294 268 void popFunctor(); 269 Functor* getFunctor() const; 270 295 271 bool functionHeaderMatches(Functor* functor) const; 296 272 297 273 void setObject(void* object); 298 void setObject(BaseObject* object); 299 300 State::Enum state_; 274 void pushObject(void* object); 275 void popObject(); 276 void* getObject() const; 277 278 bool bActive_; 279 bool bInitialized_; 301 280 const std::type_info& functionHeader_; 281 std::stack<Functor*> functorStack_; 282 std::stack<void*> objectStack_; 302 283 }; 303 284 304 inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, _ConsoleCommand::State::Enum state = _ConsoleCommand::State::Active)305 { 306 return new _ConsoleCommand("", name, functor, state);307 } 308 309 inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, _ConsoleCommand::State::Enum state = _ConsoleCommand::State::Active)310 { 311 return new _ConsoleCommand(group, name, functor, state);285 inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, bool bInitialized = true) 286 { 287 return new _ConsoleCommand("", name, functor, bInitialized); 288 } 289 290 inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true) 291 { 292 return new _ConsoleCommand(group, name, functor, bInitialized); 312 293 } 313 294 } -
code/branches/consolecommands3/src/libraries/core/Functor.h
r7180 r7185 38 38 #include "util/Debug.h" 39 39 #include "util/MultiType.h" 40 #include "BaseObject.h"41 40 42 41 namespace orxonox … … 116 115 virtual void evaluateParam(unsigned int index, MultiType& param) const = 0; 117 116 118 virtual void setBaseObject(BaseObject* object) {}119 virtual void setBaseObject(const BaseObject* object) {}120 virtual BaseObject* getBaseObject() const { return 0; }121 122 117 virtual void setRawObjectPointer(void* object) {} 123 118 virtual void* getRawObjectPointer() const { return 0; } … … 183 178 } 184 179 185 void setBaseObject(BaseObject* object)186 {187 this->object_ = dynamic_cast<T*>(object);188 this->constObject_ = 0;189 }190 191 void setBaseObject(const BaseObject* object)192 {193 this->object_ = 0;194 this->constObject_ = dynamic_cast<const T*>(object);195 }196 197 BaseObject* getBaseObject() const198 {199 if (this->object_)200 return upcast<BaseObject*>(this->object_);201 else202 return const_cast<BaseObject*>(upcast<const BaseObject*>(this->constObject_));203 }204 205 180 void setRawObjectPointer(void* object) 206 181 { … … 237 212 238 213 239 template < int r,class R, class P1, class P2, class P3, class P4, class P5>214 template <class R, class P1, class P2, class P3, class P4, class P5> 240 215 struct FunctorHeaderIdentifier {}; 241 216 … … 354 329 355 330 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES##numparams(returnvalue) 356 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) < returnvalue,FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>357 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) < returnvalue,FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>358 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) < returnvalue,FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>359 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) < returnvalue,FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>360 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) < returnvalue,FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>361 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) < returnvalue,FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>331 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void> 332 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void> 333 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void> 334 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void> 335 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void> 336 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5> 362 337 363 338 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue
Note: See TracChangeset
for help on using the changeset viewer.