Changeset 6452 for code/branches
- Timestamp:
- Jan 20, 2010, 2:21:10 PM (15 years ago)
- Location:
- code/branches/consolecommands2/src/libraries
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands2/src/libraries/core/ConsoleCommand.cc
r5781 r6452 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->getHeaderIndentifier()) 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/consolecommands2/src/libraries/core/ConsoleCommand.h
r5781 r6452 34 34 #include <boost/preprocessor/cat.hpp> 35 35 36 #include "util/VA_NARGS.h" 36 37 #include "ArgumentCompletionFunctions.h" 37 38 #include "CommandExecutor.h" … … 155 156 } 156 157 158 159 #define _SetConsoleCommand(...) \ 160 BOOST_PP_CAT(_SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__) 161 #define _SetConsoleCommand2(name, functionpointer) \ 162 _SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) 163 #define _SetConsoleCommand3(group, name, functionpointer) \ 164 _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) 165 #define _SetConsoleCommand4(group, name, functionpointer, object) \ 166 _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object)) 167 168 #define _SetConsoleCommandGeneric(group, name, functor) \ 169 orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, functor)) 170 171 172 #define _DeclareConsoleCommand(...) \ 173 BOOST_PP_CAT(_DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__) 174 #define _DeclareConsoleCommand2(name, functionpointer) \ 175 _DeclareConsoleCommandGeneric("", name, functionpointer) 176 #define _DeclareConsoleCommand3(group, name, functionpointer) \ 177 _DeclareConsoleCommandGeneric(group, name, functionpointer) 178 179 #define _DeclareConsoleCommandGeneric(group, name, functionpointer) \ 180 orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), orxonox::_ConsoleCommand::State::UninitializedActive)) 181 182 183 #define _ModifyConsoleCommand(...) \ 184 orxonox::_ConsoleCommand::getCommand(__VA_ARGS__, true)->getManipulator() 185 186 187 namespace orxonox 188 { 189 class _CoreExport _ConsoleCommand : protected Executor 190 { 191 friend struct _ConsoleCommandManipulator; 192 193 public: 194 struct State 195 { 196 enum Enum 197 { 198 UninitializedActive, 199 UninitializedInactive, 200 Active, 201 Inactive 202 }; 203 }; 204 205 struct ObjectPointer 206 { 207 enum Enum 208 { 209 Null, 210 RawCopy, 211 CastViaBaseObject 212 }; 213 }; 214 215 struct _ConsoleCommandManipulator 216 { 217 public: 218 _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {} 219 220 template <class F> 221 inline _ConsoleCommandManipulator& setFunction(F function, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null) 222 { if (this->command_) { this->command_->setFunctor(createFunctor(function), mode); } return *this; } 223 template <class F, class O> 224 inline _ConsoleCommandManipulator& setFunction(F function, O* object) 225 { if (this->command_) { this->command_->setFunctor(createFunctor(function, object)); } return *this; } 226 inline _ConsoleCommandManipulator& setFunction(Functor* functor) 227 { if (this->command_) { this->command_->setFunctor(functor); } return *this; } 228 inline _ConsoleCommandManipulator& setFunction(const _ConsoleCommand* command) 229 { if (this->command_) { this->command_->setFunctor(command->functor_); } return *this; } 230 inline _ConsoleCommandManipulator& setFunction(const _ConsoleCommandManipulator& manipulator) 231 { if (this->command_) { this->command_->setFunctor(manipulator.command_->functor_); } return *this; } 232 233 template <class F> 234 inline _ConsoleCommandManipulator& pushFunction(F function, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null) 235 { if (this->command_) { this->command_->pushFunctor(createFunctor(function), mode); } return *this; } 236 template <class F, class O> 237 inline _ConsoleCommandManipulator& pushFunction(F function, O* object) 238 { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object)); } return *this; } 239 inline _ConsoleCommandManipulator& pushFunction(Functor* functor) 240 { if (this->command_) { this->command_->pushFunctor(functor); } return *this; } 241 inline _ConsoleCommandManipulator& pushFunction(const _ConsoleCommand* command) 242 { if (this->command_) { this->command_->pushFunctor(command->functor_); } return *this; } 243 inline _ConsoleCommandManipulator& pushFunction(const _ConsoleCommandManipulator& manipulator) 244 { if (this->command_) { this->command_->pushFunctor(manipulator.command_->functor_); } return *this; } 245 246 inline _ConsoleCommandManipulator& popFunction() 247 { if (this->command_) { this->command_->popFunctor(); } return *this; } 248 249 inline _ConsoleCommandManipulator& setObject(void* object) 250 { if (this->command_) { this->command_->setObject(object); } return *this; } 251 inline _ConsoleCommandManipulator& setObject(BaseObject* object) 252 { if (this->command_) { this->command_->setObject(object); } return *this; } 253 254 inline _ConsoleCommandManipulator& setActive(bool bActive) 255 { if (this->command_) { this->command_->setActive(bActive); } return *this; } 256 257 private: 258 _ConsoleCommand* command_; 259 }; 260 261 public: 262 _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state = State::Active); 263 264 _ConsoleCommand& addShortcut(); 265 _ConsoleCommand& addShortcut(const std::string& name); 266 _ConsoleCommand& addGroup(const std::string& group); 267 _ConsoleCommand& addGroup(const std::string& group, const std::string& name); 268 269 void setActive(bool bActive); 270 inline State::Enum getState() const 271 { return this->state_; } 272 inline bool isActive() const 273 { return (this->state_ == State::Active); } 274 275 static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands() 276 { return _ConsoleCommand::getCommandMap(); } 277 278 static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false) 279 { return _ConsoleCommand::getCommand("", name, bPrintError); } 280 static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); 281 282 inline _ConsoleCommandManipulator getManipulator() const 283 { return this; } 284 285 private: 286 static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap(); 287 static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command); 288 289 void setInitialized(bool bInitialized); 290 291 void setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null); 292 void pushFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null); 293 void popFunctor(); 294 bool functionHeaderMatches(Functor* functor) const; 295 296 void setObject(void* object); 297 void setObject(BaseObject* object); 298 299 State::Enum state_; 300 const std::type_info& functionHeader_; 301 }; 302 303 inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, _ConsoleCommand::State::Enum state = _ConsoleCommand::State::Active) 304 { 305 return new _ConsoleCommand("", name, functor, state); 306 } 307 308 inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, _ConsoleCommand::State::Enum state = _ConsoleCommand::State::Active) 309 { 310 return new _ConsoleCommand(group, name, functor, state); 311 } 312 } 313 157 314 #endif /* _ConsoleCommand_H__ */ -
code/branches/consolecommands2/src/libraries/core/Functor.h
r6417 r6452 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_; … … 182 195 } 183 196 197 void setBaseObject(BaseObject* object) 198 { 199 this->bConstObject_ = false; 200 this->object_ = dynamic_cast<T*>(object); 201 } 202 203 void setBaseObject(const BaseObject* object) 204 { 205 this->bConstObject_ = true; 206 this->constObject_ = dynamic_cast<const T*>(object); 207 } 208 209 BaseObject* getBaseObject() const 210 { 211 if (this->bConstObject_) 212 return const_cast<BaseObject*>(upcast<const BaseObject*>(this->constObject_)); 213 else 214 return upcast<BaseObject*>(this->object_); 215 } 216 217 void setRawObjectPointer(void* object) 218 { 219 this->bConstObject_ = false; 220 this->object_ = (T*)object; 221 } 222 223 void* getRawObjectPointer() const 224 { 225 if (this->bConstObject_) 226 return (void*)this->constObject_; 227 else 228 return (void*)this->object_; 229 } 230 184 231 private: 185 232 const T* constObject_; … … 187 234 bool bConstObject_; 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/consolecommands2/src/libraries/core/LuaState.h
r6417 r6452 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/consolecommands2/src/libraries/util/Convert.h
r6417 r6452 115 115 } 116 116 }; 117 118 //////////// 119 // upcast // 120 //////////// 121 namespace detail 122 { 123 // perform a static cast if ToType is a base of FromType 124 template<class ToType, class FromType> 125 FORCEINLINE ToType upcast(FromType input, detail::Int2Type<true>) 126 { 127 return static_cast<ToType>(input); 128 } 129 130 // return zero if ToType is not a base of FromType 131 template<class ToType, class FromType> 132 FORCEINLINE ToType upcast(FromType input, detail::Int2Type<false>) 133 { 134 return 0; 135 } 136 } 137 138 // performs an upcast if ToType is a base of FromType, returns zero otherwise 139 template <class ToType, class FromType> 140 FORCEINLINE ToType upcast(FromType input) 141 { 142 enum { probe = ImplicitConversion<FromType, ToType>::exists }; 143 return detail::upcast<ToType, FromType>(input, detail::Int2Type<probe>()); 144 } 117 145 } 118 146
Note: See TracChangeset
for help on using the changeset viewer.