- Timestamp:
- Aug 18, 2010, 3:39:01 PM (14 years ago)
- Location:
- code/branches/consolecommands3
- Files:
-
- 6 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3
- Property svn:mergeinfo changed
/code/branches/consolecommands2 (added) merged: 6451-6454
- Property svn:mergeinfo changed
-
code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc
r5781 r7179 28 28 29 29 #include "ConsoleCommand.h" 30 #include <cassert> 30 31 31 32 namespace orxonox … … 71 72 } 72 73 } 74 75 #include "BaseObject.h" // remove this 76 77 namespace orxonox 78 { 79 _SetConsoleCommand("BaseObject", "setName", &BaseObject::setName, (BaseObject*)0); 80 _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive)); 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; 85 _ConsoleCommand::registerCommand(group, name, this); 86 } 87 88 _ConsoleCommand& _ConsoleCommand::addShortcut() 89 { 90 _ConsoleCommand::registerCommand("", this->getName(), this); 91 return *this; 92 } 93 94 _ConsoleCommand& _ConsoleCommand::addShortcut(const std::string& name) 95 { 96 _ConsoleCommand::registerCommand("", name, this); 97 return *this; 98 } 99 100 _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group) 101 { 102 _ConsoleCommand::registerCommand(group, this->getName(), this); 103 return *this; 104 } 105 106 _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group, const std::string& name) 107 { 108 _ConsoleCommand::registerCommand(group, name, this); 109 return *this; 110 } 111 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) 149 { 150 if (!functor) 151 { 152 this->setInitialized(false); 153 return; 154 } 155 156 if (!this->functionHeaderMatches(functor)) 157 { 158 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 } 193 } 194 195 bool _ConsoleCommand::functionHeaderMatches(Functor* functor) const 196 { 197 if (!this->functor_) 198 { 199 assert(false); 200 return false; 201 } 202 return (functor->getHeaderIdentifier() == this->functor_->getHeaderIdentifier()); 203 } 204 205 void _ConsoleCommand::setObject(void* object) 206 { 207 if (this->functor_) 208 this->functor_->setRawObjectPointer(object); 209 } 210 211 void _ConsoleCommand::setObject(BaseObject* object) 212 { 213 if (this->functor_) 214 this->functor_->setBaseObject(object); 215 } 216 217 /* static */ const _ConsoleCommand* _ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError) 218 { 219 std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommandMap().find(group); 220 if (it_group != _ConsoleCommand::getCommandMap().end()) 221 { 222 std::map<std::string, _ConsoleCommand*>::const_iterator it_name = it_group->second.find(name); 223 if (it_name != it_group->second.end()) 224 { 225 return it_name->second; 226 } 227 } 228 if (bPrintError) 229 { 230 if (group == "") 231 COUT(0) << "Error: Couldn't find console command with shortcut \"" << name << "\"" << std::endl; 232 else 233 COUT(0) << "Error: Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << std::endl; 234 } 235 return 0; 236 } 237 238 /* static */ std::map<std::string, std::map<std::string, _ConsoleCommand*> >& _ConsoleCommand::getCommandMap() 239 { 240 static std::map<std::string, std::map<std::string, _ConsoleCommand*> > commandMap; 241 return commandMap; 242 } 243 244 /* static */ void _ConsoleCommand::registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command) 245 { 246 if (name == "") 247 return; 248 249 if (_ConsoleCommand::getCommand(group, name) != 0) 250 { 251 if (group == "") 252 COUT(2) << "Warning: A console command with shortcut name \"" << name << "\" already exists." << std::endl; 253 else 254 COUT(2) << "Warning: A console command with group \"" << group << "\" and name \"" << name << "\" already exists." << std::endl; 255 } 256 else 257 { 258 _ConsoleCommand::getCommandMap()[group][name] = command; 259 } 260 } 261 } -
code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h
r5781 r7179 33 33 34 34 #include <boost/preprocessor/cat.hpp> 35 35 #include <boost/preprocessor/facilities/expand.hpp> 36 37 #include "util/VA_NARGS.h" 36 38 #include "ArgumentCompletionFunctions.h" 37 39 #include "CommandExecutor.h" … … 155 157 } 156 158 159 160 #define _SetConsoleCommand(...) \ 161 BOOST_PP_EXPAND(BOOST_PP_CAT(_SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)) 162 #define _SetConsoleCommand2(name, functionpointer) \ 163 _SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) 164 #define _SetConsoleCommand3(group, name, functionpointer) \ 165 _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) 166 #define _SetConsoleCommand4(group, name, functionpointer, object) \ 167 _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object)) 168 169 #define _SetConsoleCommandGeneric(group, name, functor) \ 170 orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, functor)) 171 172 173 #define _DeclareConsoleCommand(...) \ 174 BOOST_PP_CAT(_DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__) 175 #define _DeclareConsoleCommand2(name, functionpointer) \ 176 _DeclareConsoleCommandGeneric("", name, functionpointer) 177 #define _DeclareConsoleCommand3(group, name, functionpointer) \ 178 _DeclareConsoleCommandGeneric(group, name, functionpointer) 179 180 #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 183 184 #define _ModifyConsoleCommand(...) \ 185 orxonox::_ConsoleCommand::getCommand(__VA_ARGS__, true)->getManipulator() 186 187 188 namespace orxonox 189 { 190 class _CoreExport _ConsoleCommand : protected Executor 191 { 192 friend struct _ConsoleCommandManipulator; 193 194 public: 195 struct State 196 { 197 enum Enum 198 { 199 UninitializedActive, 200 UninitializedInactive, 201 Active, 202 Inactive 203 }; 204 }; 205 206 struct ObjectPointer 207 { 208 enum Enum 209 { 210 Null, 211 RawCopy, 212 CastViaBaseObject 213 }; 214 }; 215 216 struct _ConsoleCommandManipulator 217 { 218 public: 219 _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {} 220 221 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; } 224 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; } 233 234 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; } 237 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; } 246 247 inline _ConsoleCommandManipulator& popFunction() 248 { if (this->command_) { this->command_->popFunctor(); } return *this; } 249 250 inline _ConsoleCommandManipulator& setObject(void* object) 251 { 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; } 254 255 inline _ConsoleCommandManipulator& setActive(bool bActive) 256 { if (this->command_) { this->command_->setActive(bActive); } return *this; } 257 258 private: 259 _ConsoleCommand* command_; 260 }; 261 262 public: 263 _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state = State::Active); 264 265 _ConsoleCommand& addShortcut(); 266 _ConsoleCommand& addShortcut(const std::string& name); 267 _ConsoleCommand& addGroup(const std::string& group); 268 _ConsoleCommand& addGroup(const std::string& group, const std::string& name); 269 270 void setActive(bool bActive); 271 inline State::Enum getState() const 272 { return this->state_; } 273 inline bool isActive() const 274 { return (this->state_ == State::Active); } 275 276 static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands() 277 { return _ConsoleCommand::getCommandMap(); } 278 279 static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false) 280 { return _ConsoleCommand::getCommand("", name, bPrintError); } 281 static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); 282 283 inline _ConsoleCommandManipulator getManipulator() const 284 { return this; } 285 286 private: 287 static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap(); 288 static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command); 289 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); 294 void popFunctor(); 295 bool functionHeaderMatches(Functor* functor) const; 296 297 void setObject(void* object); 298 void setObject(BaseObject* object); 299 300 State::Enum state_; 301 const std::type_info& functionHeader_; 302 }; 303 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); 312 } 313 } 314 157 315 #endif /* _ConsoleCommand_H__ */ -
code/branches/consolecommands3/src/libraries/core/Functor.h
r7177 r7179 31 31 #define _Functor_H__ 32 32 33 #include <typeinfo> 34 33 35 #include "CorePrereqs.h" 34 36 37 #include "util/Convert.h" 35 38 #include "util/Debug.h" 36 39 #include "util/MultiType.h" 40 #include "BaseObject.h" 37 41 38 42 namespace orxonox … … 112 116 virtual void evaluateParam(unsigned int index, MultiType& param) const = 0; 113 117 118 virtual void setBaseObject(BaseObject* object) {} 119 virtual void setBaseObject(const BaseObject* object) {} 120 virtual BaseObject* getBaseObject() const { return 0; } 121 122 virtual void setRawObjectPointer(void* object) {} 123 virtual void* getRawObjectPointer() const { return 0; } 124 125 virtual const std::type_info& getHeaderIdentifier() const = 0; 126 114 127 protected: 115 128 unsigned int numParams_; … … 170 183 } 171 184 185 void setBaseObject(BaseObject* object) 186 { 187 this->bConstObject_ = false; 188 this->object_ = dynamic_cast<T*>(object); 189 } 190 191 void setBaseObject(const BaseObject* object) 192 { 193 this->bConstObject_ = true; 194 this->constObject_ = dynamic_cast<const T*>(object); 195 } 196 197 BaseObject* getBaseObject() const 198 { 199 if (this->bConstObject_) 200 return const_cast<BaseObject*>(upcast<const BaseObject*>(this->constObject_)); 201 else 202 return upcast<BaseObject*>(this->object_); 203 } 204 205 void setRawObjectPointer(void* object) 206 { 207 this->bConstObject_ = false; 208 this->object_ = (T*)object; 209 } 210 211 void* getRawObjectPointer() const 212 { 213 if (this->bConstObject_) 214 return (void*)this->constObject_; 215 else 216 return (void*)this->object_; 217 } 218 172 219 typedef std::pair<T*, const T*> Objects; 173 220 … … 187 234 const T* constObject_; 188 235 }; 236 237 238 239 template <int r, class R, class P1, class P2, class P3, class P4, class P5> 240 struct FunctorHeaderIdentifier {}; 241 242 243 244 inline Functor* createFunctor(Functor* functor) 245 { 246 return functor; 247 } 189 248 190 249 … … 294 353 295 354 355 #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> 362 363 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue 364 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE0 void 365 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE1 R 366 367 368 296 369 #define FUNCTOR_EVALUATE_PARAM(numparams) FUNCTOR_EVALUATE_PARAM##numparams 297 370 #define FUNCTOR_EVALUATE_PARAM0 … … 320 393 321 394 322 323 395 #define CREATE_STATIC_FUNCTOR(returnvalue, numparams) \ 324 396 FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \ … … 342 414 } \ 343 415 \ 344 v irtual void evaluateParam(unsigned int index, MultiType& param) const \416 void evaluateParam(unsigned int index, MultiType& param) const \ 345 417 { \ 346 418 FUNCTOR_EVALUATE_PARAM(numparams); \ 419 } \ 420 \ 421 const std::type_info& getHeaderIdentifier() const \ 422 { \ 423 return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \ 347 424 } \ 348 425 \ … … 386 463 } \ 387 464 \ 388 v irtual void evaluateParam(unsigned int index, MultiType& param) const \465 void evaluateParam(unsigned int index, MultiType& param) const \ 389 466 { \ 390 467 FUNCTOR_EVALUATE_PARAM(numparams); \ 468 } \ 469 \ 470 const std::type_info& getHeaderIdentifier() const \ 471 { \ 472 return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \ 391 473 } \ 392 474 \ … … 418 500 } \ 419 501 \ 420 v irtual void evaluateParam(unsigned int index, MultiType& param) const \502 void evaluateParam(unsigned int index, MultiType& param) const \ 421 503 { \ 422 504 FUNCTOR_EVALUATE_PARAM(numparams); \ 505 } \ 506 \ 507 const std::type_info& getHeaderIdentifier() const \ 508 { \ 509 return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \ 423 510 } \ 424 511 \ … … 457 544 return functor; \ 458 545 } 546 459 547 460 548 -
code/branches/consolecommands3/src/libraries/core/LuaState.h
r6763 r7179 54 54 void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null); 55 55 void evaluateParam(unsigned int index, MultiType& param) const {} 56 const std::type_info& getHeaderIdentifier() const { return typeid(this); } 56 57 57 58 private: -
code/branches/consolecommands3/src/libraries/util/Convert.h
r7163 r7179 116 116 } 117 117 }; 118 119 //////////// 120 // upcast // 121 //////////// 122 namespace detail 123 { 124 // perform a static cast if ToType is a base of FromType 125 template<class ToType, class FromType> 126 FORCEINLINE ToType upcast(FromType input, detail::Int2Type<true>) 127 { 128 return static_cast<ToType>(input); 129 } 130 131 // return zero if ToType is not a base of FromType 132 template<class ToType, class FromType> 133 FORCEINLINE ToType upcast(FromType input, detail::Int2Type<false>) 134 { 135 return 0; 136 } 137 } 138 139 // performs an upcast if ToType is a base of FromType, returns zero otherwise 140 template <class ToType, class FromType> 141 FORCEINLINE ToType upcast(FromType input) 142 { 143 enum { probe = ImplicitConversion<FromType, ToType>::exists }; 144 return detail::upcast<ToType, FromType>(input, detail::Int2Type<probe>()); 145 } 118 146 } 119 147
Note: See TracChangeset
for help on using the changeset viewer.