Changeset 2003 for code/branches/objecthierarchy/src/core
- Timestamp:
- Oct 24, 2008, 12:43:13 AM (16 years ago)
- Location:
- code/branches/objecthierarchy/src/core
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/core/CommandLine.cc
r1755 r2003 35 35 /** 36 36 @brief 37 Parses a value string for a command line argument. 38 It simply uses convertValue(Output, Input) to do that. 39 Bools are treated specially. That is necessary 40 so that you can have simple command line switches. 41 */ 42 void CommandLineArgument::parse(const std::string& value) 43 { 44 if (value_.getType() == MT_bool) 45 { 46 // simulate command line switch 47 bool temp; 48 if (convertValue(&temp, value)) 49 { 50 this->bHasDefaultValue_ = false; 51 this->value_ = temp; 52 } 53 else if (value == "") 54 { 55 this->bHasDefaultValue_ = false; 56 this->value_ = true; 57 } 58 else 59 { 60 ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); 61 } 62 } 63 else 64 { 65 if (!value_.setValue(value)) 66 { 67 value_.setValue(defaultValue_); 68 ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); 69 } 70 else 71 this->bHasDefaultValue_ = false; 72 } 73 } 74 75 76 /** 77 @brief 37 78 Destructor destroys all CommandLineArguments with it. 38 79 */ 39 80 CommandLine::~CommandLine() 40 81 { 41 for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();82 for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); 42 83 it != cmdLineArgs_.end(); ++it) 43 84 { … … 73 114 { 74 115 // first shove all the shortcuts in a map 75 for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();116 for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); 76 117 it != cmdLineArgs_.end(); ++it) 77 118 { … … 178 219 void CommandLine::checkFullArgument(const std::string& name, const std::string& value) 179 220 { 180 std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);221 std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); 181 222 if (it == cmdLineArgs_.end()) 182 223 ThrowException(Argument, "Command line argument '" + name + "' does not exist."); … … 195 236 void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value) 196 237 { 197 std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);238 std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); 198 239 if (it == cmdLineArgsShortcut_.end()) 199 240 ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); … … 206 247 CommandLine* inst = &_getInstance(); 207 248 std::string infoStr; 208 for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin();249 for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin(); 209 250 it != inst->cmdLineArgs_.end(); ++it) 210 251 { … … 214 255 } 215 256 257 /** 258 @brief 259 Retrieves a CommandLineArgument. 260 The method throws an exception if 'name' was not found or the value could not be converted. 261 @note 262 You shold of course not call this method before the command line has been parsed. 263 */ 264 const CommandLineArgument* CommandLine::getArgument(const std::string& name) 265 { 266 std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); 267 if (it == _getInstance().cmdLineArgs_.end()) 268 { 269 ThrowException(Argument, "Could find command line argument '" + name + "'."); 270 } 271 else 272 { 273 return it->second; 274 } 275 } 216 276 } -
code/branches/objecthierarchy/src/core/CommandLine.h
r1764 r2003 35 35 #include "util/Debug.h" 36 36 #include "util/Exception.h" 37 #include "util/MultiType.h" 37 38 38 39 #define SetCommandLineArgument(name, defaultValue) \ 39 BaseCommandLineArgument& CmdArgumentDummyBoolVar##name \40 CommandLineArgument& CmdArgumentDummyBoolVar##name \ 40 41 = orxonox::CommandLine::addArgument(#name, defaultValue) 41 42 #define SetCommandLineSwitch(name) \ 42 BaseCommandLineArgument& CmdArgumentDummyBoolVar##name \43 CommandLineArgument& CmdArgumentDummyBoolVar##name \ 43 44 = orxonox::CommandLine::addArgument(#name, false) 44 45 … … 62 63 /** 63 64 @brief 64 Base class for CommandLineArguments. 65 */ 66 class _CoreExport BaseCommandLineArgument 67 { 68 friend class CommandLine; 69 70 public: 71 //! Tells whether the value has been changed by the command line. 72 bool hasDefaultValue() const { return bHasDefaultValue_; } 73 //! Returns the name of the argument. 74 const std::string& getName() const { return name_; } 75 76 //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument. 77 //! Evaluates to "" if none there is none. 78 const std::string& getShortcut() const { return shortcut_; } 79 //! Sets the shortcut for the argument 80 BaseCommandLineArgument& setShortcut(const std::string& shortcut) 81 { this->shortcut_ = shortcut; return *this; } 82 83 //! Returns the usage information 84 const std::string& getInformation() const { return this->usageInformation_; } 85 //! Sets the option information when displaying orxonox usage. 86 BaseCommandLineArgument& setInformation(const std::string& usage) 87 { this->usageInformation_ = usage; return *this; } 88 89 protected: 90 BaseCommandLineArgument(const std::string& name) 91 : bHasDefaultValue_(true) 92 , name_(name) 93 { } 94 95 //! Undefined copy constructor 96 BaseCommandLineArgument(const BaseCommandLineArgument& instance); 97 virtual ~BaseCommandLineArgument() { } 98 99 //! Parses the value string of a command line argument. 100 virtual void parse(const std::string& value) = 0; 101 102 //! Tells whether the value has been changed by the command line. 103 bool bHasDefaultValue_; 104 105 private: 106 std::string name_; //!< Name of the argument 107 std::string shortcut_; //!< Shortcut of the argument. @see getShortcut(). 108 std::string usageInformation_; //!< Tells about the usage of this parameter 109 }; 110 111 112 /** 113 @brief 114 Container class for a command line argument of type T. 65 Container class for a command line argument of any type supported by MultiType. 115 66 116 67 Whenever you want to have an option specified by a command line switch, … … 127 78 CommandLine class. It is a Singleton, but the public interface is static. 128 79 */ 129 template <class T> 130 class CommandLineArgument : public BaseCommandLineArgument 131 { 132 // Only let CommandLine change the value. 80 class _CoreExport CommandLineArgument 81 { 133 82 friend class CommandLine; 134 83 135 84 public: 85 //! Tells whether the value has been changed by the command line. 86 bool hasDefaultValue() const { return bHasDefaultValue_; } 87 //! Returns the name of the argument. 88 const std::string& getName() const { return name_; } 89 90 //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument. 91 //! Evaluates to "" if none there is none. 92 const std::string& getShortcut() const { return shortcut_; } 93 //! Sets the shortcut for the argument 94 CommandLineArgument& shortcut(const std::string& shortcut) 95 { this->shortcut_ = shortcut; return *this; } 96 97 //! Returns the usage information 98 const std::string& getInformation() const { return this->usageInformation_; } 99 //! Sets the option information when displaying orxonox usage. 100 CommandLineArgument& information(const std::string& usage) 101 { this->usageInformation_ = usage; return *this; } 102 136 103 //! Returns the actual value of the argument. Can be equal to default value. 137 TgetValue() const { return value_; }104 MultiType getValue() const { return value_; } 138 105 //! Returns the given default value as type T. 139 TgetDefaultValue() const { return defaultValue_; }106 MultiType getDefaultValue() const { return defaultValue_; } 140 107 141 108 private: 142 109 //! Constructor initialises both value_ and defaultValue_ with defaultValue. 143 CommandLineArgument(const std::string& name, const T& defaultValue) 144 : BaseCommandLineArgument(name) 110 CommandLineArgument(const std::string& name, const MultiType& defaultValue) 111 : bHasDefaultValue_(true) 112 , name_(name) 145 113 , value_(defaultValue) 146 114 , defaultValue_(defaultValue) 147 115 { } 148 116 149 virtual void parse(const std::string& value); 150 151 T value_; //!< The actual value 152 T defaultValue_; //!< Default value. Should not be changed. 117 //! Undefined copy constructor 118 CommandLineArgument(const CommandLineArgument& instance); 119 ~CommandLineArgument() { } 120 121 //! Parses the value string of a command line argument. 122 void parse(const std::string& value); 123 124 //! Tells whether the value has been changed by the command line. 125 bool bHasDefaultValue_; 126 127 private: 128 std::string name_; //!< Name of the argument 129 std::string shortcut_; //!< Shortcut of the argument. @see getShortcut(). 130 std::string usageInformation_; //!< Tells about the usage of this parameter 131 132 MultiType value_; //!< The actual value 133 MultiType defaultValue_; //!< Default value. Should not be changed. 153 134 }; 154 155 /**156 @brief157 Parses a value string for a command line argument.158 It simply uses convertValue(Output, Input) to do that.159 */160 template <class T>161 void CommandLineArgument<T>::parse(const std::string& value)162 {163 if (convertValue(&this->value_, value))164 {165 this->bHasDefaultValue_ = false;166 }167 else168 {169 ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");170 }171 }172 173 /**174 @brief175 Parses a value string for a command line argument.176 It simply uses convertValue(Output, Input) to do that.177 This is a template specialisation for bool type. That is necessary178 so that you can have simple command line switches.179 */180 template <>181 inline void CommandLineArgument<bool>::parse(const std::string& value)182 {183 if (convertValue(&this->value_, value))184 {185 this->bHasDefaultValue_ = false;186 }187 else if (value == "")188 {189 this->bHasDefaultValue_ = false;190 this->value_ = true;191 }192 else193 {194 ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");195 }196 }197 135 198 136 … … 215 153 static std::string getUsageInformation(); 216 154 217 template <class T> 218 static const CommandLineArgument<T>* getArgument(const std::string& name); 155 static const CommandLineArgument* getArgument(const std::string& name); 219 156 //! Writes the argument value in the given parameter. 220 157 template <class T> 221 158 static void getValue(const std::string& name, T* value) 222 { *value = getArgument<T>(name)->getValue(); } 159 { *value = (T)(getArgument(name)->getValue()); } 160 static MultiType getValue(const std::string& name) 161 { return getArgument(name)->getValue(); } 223 162 template <class T> 224 static BaseCommandLineArgument& addArgument(const std::string& name, T defaultValue); 163 static CommandLineArgument& addArgument(const std::string& name, T defaultValue); 164 165 static bool existsArgument(const std::string& name) 166 { 167 std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); 168 return !(it == _getInstance().cmdLineArgs_.end()); 169 } 170 225 171 226 172 private: … … 237 183 void checkShortcut(const std::string& shortcut, const std::string& value); 238 184 239 template <class T>240 BaseCommandLineArgument* createArg(const std::string& name, T defaultValue, Int2Type<0> isString);241 template <class T>242 BaseCommandLineArgument* createArg(const std::string& name, T defaultValue, Int2Type<1> isString);243 244 185 /** 245 Tells whether we parse the first expression. The CommmandLineArguments are added before main().186 Tells whether we parsed for the first time. The CommmandLineArguments are added before main(). 246 187 So when we call parse() the first time, we need to create a map with all shortcuts since these 247 188 get added after addCommandLineArgument(). … … 250 191 251 192 //! Holds all pointers to the arguments and serves as a search map by name. 252 std::map<std::string, BaseCommandLineArgument*> cmdLineArgs_;193 std::map<std::string, CommandLineArgument*> cmdLineArgs_; 253 194 //! Search map by chortcut for the arguments. 254 std::map<std::string, BaseCommandLineArgument*> cmdLineArgsShortcut_;195 std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_; 255 196 }; 256 197 257 258 /**259 @brief260 Retrieves a CommandLineArgument.261 The method throws an exception if 'name' was not found or the value could not be converted.262 @note263 You shold of course not call this method before the command line has been parsed.264 */265 template <class T>266 const CommandLineArgument<T>* CommandLine::getArgument(const std::string& name)267 {268 std::map<std::string, BaseCommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);269 if (it == _getInstance().cmdLineArgs_.end())270 {271 ThrowException(Argument, "Could find command line argument '" + name + "'.");272 }273 else274 {275 CommandLineArgument<T>* arg = dynamic_cast<CommandLineArgument<T>* >(it->second);276 if (arg == 0)277 {278 ThrowException(Argument, "Could not convert command line argument value to requested type. " \279 "You should use exactly the same type as predefined.");280 }281 else282 {283 return arg;284 }285 }286 }287 198 288 199 /** … … 294 205 @param defaultValue 295 206 Default value that is used when argument was not given. 296 @note297 In order to store char* strings as std::string too, there's298 little bit of template programming involved:299 StaticConversions::exists determines whether T converts to std::string.300 Int2Type<int> is then used to call the right function. One returns301 a CommandLineArgument<T> and the other CommandLineArgument<std::string>.302 207 */ 303 208 template <class T> 304 BaseCommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue)305 { 306 std::map<std::string, BaseCommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);209 CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue) 210 { 211 std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); 307 212 OrxAssert(it == _getInstance().cmdLineArgs_.end(), 308 213 "Cannot add a command line argument with name '" + name + "' twice."); 309 214 310 return *(_getInstance().cmdLineArgs_[name] = 311 _getInstance().createArg(name, defaultValue, Int2Type<StaticConversion<T, std::string>::exists>())); 312 } 313 314 /** 315 Returns a new CommandLineArgument<T>. 316 */ 317 template <class T> 318 BaseCommandLineArgument* CommandLine::createArg(const std::string& name, T defaultValue, Int2Type<0> isPrime) 319 { 320 return new CommandLineArgument<T>(name, defaultValue); 321 } 322 323 /** 324 Returns a new CommandLineArgument<std::string>. 325 */ 326 template <class T> 327 BaseCommandLineArgument* CommandLine::createArg(const std::string& name, T defaultValue, Int2Type<1> isPrime) 328 { 329 return new CommandLineArgument<std::string>(name, defaultValue); 215 return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue)); 330 216 } 331 217 } -
code/branches/objecthierarchy/src/core/CorePrereqs.h
r1989 r2003 103 103 class CommandExecutor; 104 104 class CommandLine; 105 class BaseCommandLineArgument;106 template <class T>107 105 class CommandLineArgument; 108 106 class ConfigFile; -
code/branches/objecthierarchy/src/core/RootGameState.cc
r1824 r2003 39 39 namespace orxonox 40 40 { 41 SetCommandLineArgument(state, "gui").s etShortcut("s");41 SetCommandLineArgument(state, "gui").shortcut("s"); 42 42 43 43 RootGameState::RootGameState(const std::string& name) … … 147 147 148 148 // get initial state from command line 149 std::string initialState; 150 CommandLine::getValue<std::string>("state", &initialState); 151 gotoState(initialState); 149 gotoState(CommandLine::getValue("state")); 152 150 153 151 while (this->activeChild_)
Note: See TracChangeset
for help on using the changeset viewer.