Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 20, 2008, 10:30:28 PM (16 years ago)
Author:
rgrieder
Message:

Added CommandLine class.
You can now call SetCommandLineArgument like SetConsoleCommand and hereby define a new command line argument. They are passed in main() and then they can be accessed by commandLine::getCommandLineArgument().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/orxonox/Settings.h

    r1638 r1663  
    4747namespace orxonox
    4848{
    49     /**
    50     @brief
    51         Defines a bit field structure that holds the mode as enum plus
    52         the attributes as single named bits.
    53         Every different GameMode is stored as static const, but if you wish to
    54         compare two modes, you will have to use the 'mode' member variable.
    55     */
    56     struct GameMode
     49    class _OrxonoxExport Settings : public OrxonoxClass
    5750    {
    58         enum Mode
    59         {
    60             None,
    61             Unspecified,
    62             Server,
    63             Client,
    64             Standalone,
    65             Dedicated,
    66         };
     51        friend class ClassIdentifier<Settings>;
     52        friend class GSRoot;
    6753
    68         Mode mode;
    69         bool showsGraphics;
    70         bool isMaster;
    71         bool hasServer;
    72         std::string name;
     54    public:
     55        static const std::string& getDataPath()
     56        { assert(singletonRef_s); return singletonRef_s->dataPath_; }
     57        static void tsetDataPath(const std::string& path)
     58        { assert(singletonRef_s); singletonRef_s->_tsetDataPath(path); }
    7359
    74         static const GameMode GM_None;
    75         static const GameMode GM_Unspecified;
    76         static const GameMode GM_Server;
    77         static const GameMode GM_Client;
    78         static const GameMode GM_Standalone;
    79         static const GameMode GM_Dedicated;
    80     };
     60        // an alternative to a global game mode variable
     61        static bool showsGraphics() { assert(singletonRef_s); return singletonRef_s->bShowsGraphics_; }
     62        static bool hasServer()     { assert(singletonRef_s); return singletonRef_s->bHasServer_; }
    8163
    8264
    83     class _OrxonoxExport Settings : public OrxonoxClass
    84     {
    85     public:
    86         struct CommandLineArgument
    87         {
    88             std::string name_;
    89             MultiTypeMath value_;
    90             bool bHasDefaultValue_;
    91         };
     65    private:
     66        // GSRoot has access to these
     67        static void setShowsGraphics(bool val) { assert(singletonRef_s); singletonRef_s->bShowsGraphics_ = val; }
     68        static void setHasServer    (bool val) { assert(singletonRef_s); singletonRef_s->bHasServer_     = val; }
     69
     70        Settings();
     71        Settings(const Settings& instance);
     72        ~Settings() { singletonRef_s = 0; }
     73
     74        static Settings& _getInstance() { assert(singletonRef_s); return *singletonRef_s; }
     75        void _tsetDataPath(const std::string& path);
    9276
    9377        void setConfigValues();
    9478
    95         static const std::string& getDataPath();
    96         static void tsetDataPath(const std::string& path);
     79        bool bShowsGraphics_;                                  //!< global variable that tells whether to show graphics
     80        bool bHasServer_;                                      //!< global variable that tells whether this is a server
    9781
    98         static const GameMode& getGameMode();
    99         static const GameMode& getGameMode(const std::string& name);
    100         static void setGameMode(const GameMode& mode);
    101         static void setGameMode(const std::string& mode);
    102         static bool addGameMode(const GameMode* mode);
     82        std::string dataPath_;                                 //!< Path to the game data
    10383
    104         static const CommandLineArgument* getCommandLineArgument(const std::string& name);
    105         template <class T>
    106         static bool addCommandLineArgument(const std::string &name, const std::string& valueStr, const T& defaultValue);
    107 
    108     private:
    109         Settings();
    110         Settings(const Settings& instance);
    111         ~Settings() { }
    112         static Settings& getInstance();
    113 
    114         void _tsetDataPath(const std::string& path);
    115 
    116         std::string dataPath_;                                        //!< Path to the game data
    117         GameMode gameMode_;                                           //!< Current game mode
    118         std::map<std::string, const GameMode*> gameModes_;            //!< Holds all game modes for easy string access
    119         //! holds all command line arguments (even if not given!)
    120         std::map<std::string, CommandLineArgument> commandArguments_;
     84        static Settings* singletonRef_s;                       //!< Static pointer to the only instance.
    12185    };
    12286
    123     /**
    124     @brief
    125         Returns the relative path to the game data.
    126     */
    127     inline const std::string& Settings::getDataPath()
    128     {
    129         return getInstance().dataPath_;
    130     }
    131 
    132     inline void Settings::tsetDataPath(const std::string& path)
    133     {
    134         getInstance()._tsetDataPath(path);
    135     }
    136 
    137     inline const GameMode& Settings::getGameMode()
    138     {
    139         return getInstance().gameMode_;
    140     }
    141 
    142     inline const GameMode& Settings::getGameMode(const std::string& name)
    143     {
    144         if (getInstance().gameModes_.find(name) != getInstance().gameModes_.end())
    145             return *getInstance().gameModes_[name];
    146         else
    147         {
    148             COUT(2) << "Warning: GameMode '" << name << "' doesn't exist." << std::endl;
    149             return GameMode::GM_None;
    150         }
    151     }
    152 
    153     inline void Settings::setGameMode(const GameMode& mode)
    154     {
    155         getInstance().gameMode_ = mode;
    156     }
    157 
    158     /**
    159     @brief
    160         Adds one argument of the command line to the map of command line arguments.
    161     @param name
    162         Name of the command line option.
    163     @param valueStr
    164         The value of the command line option as string
    165     @param defaultValue
    166         Default value for the option (marked when used).
    167     @return
    168         Dummy return value to enable code execution before main().
    169     */
    170     template <class T>
    171     bool Settings::addCommandLineArgument(const std::string &name, const std::string& valueStr, const T& defaultValue)
    172     {
    173         T value;
    174         bool useDefault = false;
    175         if (valueStr == "")
    176         {
    177             // note: ArgReader only returns "" for not found arguments, " " otherwise for empty ones.
    178             value = defaultValue;
    179             useDefault = true;
    180         }
    181         else if (!convertValue(&value, valueStr))
    182         {
    183             COUT(1) << "Command Line: Couldn't read option '" << name << "'." << std::endl;
    184             return false;
    185         }
    186         CommandLineArgument arg = { name, MultiTypeMath(value), useDefault };
    187         getInstance().commandArguments_[name] = arg;
    188         return true;
    189     }
    19087}
    19188
Note: See TracChangeset for help on using the changeset viewer.