Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 7, 2015, 2:16:55 PM (10 years ago)
Author:
landauf
Message:

clean and explicit setup/shutdown of singletons that are used by statically initialized instances

Location:
code/branches/core7/src/libraries/core/commandline
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc

    r10520 r10542  
    4040namespace orxonox
    4141{
     42    CommandLineParser* CommandLineParser::singletonPtr_s = 0;
     43
    4244    /**
    4345    @brief
     
    8587    CommandLineParser::~CommandLineParser()
    8688    {
    87     }
    88 
    89     /**
    90     @brief
    91         Returns a unique instance (Meyers Singleton).
    92     */
    93     CommandLineParser& CommandLineParser::_getInstance()
    94     {
    95         static CommandLineParser instance;
    96         return instance;
    9789    }
    9890
     
    260252    std::string CommandLineParser::getUsageInformation()
    261253    {
    262         CommandLineParser& inst = _getInstance();
     254        CommandLineParser& inst = getInstance();
    263255        std::ostringstream infoStr;
    264256
     
    313305    const CommandLineArgument* CommandLineParser::getArgument(const std::string& name)
    314306    {
    315         std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
    316         if (it == _getInstance().cmdLineArgs_.end())
     307        std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name);
     308        if (it == getInstance().cmdLineArgs_.end())
    317309        {
    318310            ThrowException(Argument, "Could find command line argument '" + name + "'.");
     
    331323    void CommandLineParser::addArgument(CommandLineArgument* argument)
    332324    {
    333         OrxAssert(!_getInstance().existsArgument(argument->getName()),
     325        OrxAssert(!getInstance().existsArgument(argument->getName()),
    334326            "Cannot add a command line argument with name '" + argument->getName() + "' twice.");
    335327        OrxAssert(!argument->getDefaultValue().isType<bool>() || argument->getDefaultValue().get<bool>() != true,
     
    337329            << "Please use SetCommandLineSwitch and adjust your argument: " << argument->getName());
    338330
    339         _getInstance().cmdLineArgs_[argument->getName()] = argument;
     331        getInstance().cmdLineArgs_[argument->getName()] = argument;
    340332    }
    341333
     
    345337    void CommandLineParser::removeArgument(CommandLineArgument* argument)
    346338    {
    347         _getInstance().cmdLineArgs_.erase(argument->getName());
     339        getInstance().cmdLineArgs_.erase(argument->getName());
    348340    }
    349341}
  • code/branches/core7/src/libraries/core/commandline/CommandLineParser.h

    r10520 r10542  
    4848#include "util/OrxAssert.h"
    4949#include "util/MultiType.h"
     50#include "util/Singleton.h"
    5051
    5152namespace orxonox
     
    134135        CommandLineArgument
    135136    */
    136     class _CoreExport CommandLineParser
     137    class _CoreExport CommandLineParser : public Singleton<CommandLineParser>
    137138    {
     139        friend class Singleton<CommandLineParser>;
     140
    138141    public:
     142        //! Constructor initialises bFirstTimeParse_ with true.
     143        CommandLineParser() : bFirstTimeParse_(true) { }
     144        ~CommandLineParser();
    139145
    140146        //! Parse redirection to internal member method.
    141147        static void parse(const std::string& cmdLine)
    142         { _getInstance()._parse(cmdLine); }
     148        { getInstance()._parse(cmdLine); }
    143149
    144150        static std::string getUsageInformation();
     
    157163        static bool existsArgument(const std::string& name)
    158164        {
    159             std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
    160             return !(it == _getInstance().cmdLineArgs_.end());
     165            std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name);
     166            return !(it == getInstance().cmdLineArgs_.end());
    161167        }
    162168
     
    164170
    165171    private:
    166         //! Constructor initialises bFirstTimeParse_ with true.
    167         CommandLineParser() : bFirstTimeParse_(true) { }
    168172        //! Undefined copy constructor
    169173        CommandLineParser(const CommandLineParser& instance);
    170         ~CommandLineParser();
    171 
    172         static CommandLineParser& _getInstance();
    173174
    174175        void _parse(const std::string& cmdLine);
     
    187188        //! Search map by shortcut for the arguments.
    188189        std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
     190
     191        static CommandLineParser* singletonPtr_s;
    189192    };
    190193
Note: See TracChangeset for help on using the changeset viewer.