- Timestamp:
- Jan 20, 2010, 2:21:10 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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__ */
Note: See TracChangeset
for help on using the changeset viewer.