Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9540 for code/branches/testing


Ignore:
Timestamp:
Mar 10, 2013, 11:29:44 AM (12 years ago)
Author:
landauf
Message:

some refactoring in OutputManager - for unittests it is sometimes necessary to create a new instance of OutputManager (or a mock), hence we should avoid storing dead references

Location:
code/branches/testing
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/testing/src/libraries/util/output/LogWriter.cc

    r9538 r9540  
    118118
    119119        // request old output from MemoryWriter
    120         OutputManager::getInstance().getMemoryWriter().resendOutput(this);
     120        if (&OutputManager::getInstance().getMemoryWriter())
     121            OutputManager::getInstance().getMemoryWriter().resendOutput(this);
    121122    }
    122123
  • code/branches/testing/src/libraries/util/output/OutputDefinitions.h

    r9535 r9540  
    4848*/
    4949#define REGISTER_OUTPUT_CONTEXT(name) \
    50     const OutputContextContainer& name() { static const OutputContextContainer& context = registerContext(#name); return context; }
     50    const OutputContextContainer& name() { static OutputContextContainer context = registerContext(#name); return context; }
    5151
    5252/**
  • code/branches/testing/src/libraries/util/output/OutputManager.cc

    r9539 r9540  
    5555
    5656        this->subcontextCounter_ = 0;
     57
     58        this->isInitialized_ = false;
     59        this->memoryWriterInstance_ = 0;
     60        this->consoleWriterInstance_ = 0;
     61        this->logWriterInstance_ = 0;
    5762    }
    5863
     
    6267    OutputManager::~OutputManager()
    6368    {
     69        while (!this->listeners_.empty())
     70            this->unregisterListener(this->listeners_[0]);
     71
     72        if (this->memoryWriterInstance_)
     73            delete this->memoryWriterInstance_;
     74        if (this->consoleWriterInstance_)
     75            delete this->consoleWriterInstance_;
     76        if (this->logWriterInstance_)
     77            delete this->logWriterInstance_;
    6478    }
    6579
     
    88102    /*static*/ OutputManager& OutputManager::getInstanceAndCreateListeners()
    89103    {
    90         static OutputManager& instance = OutputManager::getInstance();
    91 
    92         static MemoryWriter& memoryWriterInstance = OutputManager::getInstance().getMemoryWriter(); (void)memoryWriterInstance;
    93         static ConsoleWriter& consoleWriterInstance = OutputManager::getInstance().getConsoleWriter(); (void)consoleWriterInstance;
    94         static LogWriter& logWriterInstance = OutputManager::getInstance().getLogWriter(); (void)logWriterInstance;
    95 
    96         return instance;
    97     }
    98 
    99     /**
    100      * @brief Returns the main instance of MemoryWriter which is managed by the OutputManager singleton.
    101      * @note If OutputManager is ever un-singletonized, this instance must not remain static.
    102      */
    103     MemoryWriter& OutputManager::getMemoryWriter()
    104     {
    105         static MemoryWriter instance;
    106         return instance;
    107     }
    108 
    109     /**
    110      * @brief Returns the main instance of ConsoleWriter which is managed by the OutputManager singleton.
    111      * @note If OutputManager is ever un-singletonized, this instance must not remain static.
    112      */
    113     ConsoleWriter& OutputManager::getConsoleWriter()
    114     {
    115         static ConsoleWriter instance(std::cout);
    116         return instance;
    117     }
    118 
    119     /**
    120      * @brief Returns the main instance of LogWriter which is managed by the OutputManager singleton.
    121      * @note If OutputManager is ever un-singletonized, this instance must not remain static.
    122      */
    123     LogWriter& OutputManager::getLogWriter()
    124     {
    125         static LogWriter instance;
     104        OutputManager& instance = *OutputManager::Testing::getInstancePointer();
     105
     106        if (!instance.isInitialized_) {
     107            instance.isInitialized_ = true;
     108            instance.memoryWriterInstance_ = new MemoryWriter();
     109            instance.consoleWriterInstance_ = new ConsoleWriter(std::cout);
     110            instance.logWriterInstance_ = new LogWriter();
     111        }
     112
    126113        return instance;
    127114    }
  • code/branches/testing/src/libraries/util/output/OutputManager.h

    r9536 r9540  
    7272            static OutputManager& getInstanceAndCreateListeners();
    7373
    74             MemoryWriter& getMemoryWriter();
    75             ConsoleWriter& getConsoleWriter();
    76             LogWriter& getLogWriter();
     74            inline MemoryWriter& getMemoryWriter()   { return *this->memoryWriterInstance_; }
     75            inline ConsoleWriter& getConsoleWriter() { return *this->consoleWriterInstance_; }
     76            inline LogWriter& getLogWriter()         { return *this->logWriterInstance_; }
    7777
    78             void pushMessage(OutputLevel level, const OutputContextContainer& context, const std::string& message);
     78            virtual void pushMessage(OutputLevel level, const OutputContextContainer& context, const std::string& message);
    7979
    8080            virtual void registerListener(OutputListener* listener);
     
    125125            OutputContextSubID subcontextCounter_;                              ///< Counts the number of sub-contexts (and generates their IDs)
    126126
     127            bool isInitialized_;                                                ///< Becomes true once the following instances were created
     128            MemoryWriter*  memoryWriterInstance_;                               ///< The main instance of MemoryWriter, managed by OutputManager
     129            ConsoleWriter* consoleWriterInstance_;                              ///< The main instance of ConsoleWriter, managed by OutputManager
     130            LogWriter*     logWriterInstance_;                                  ///< The main instance of LogWriter, managed by OutputManager
     131
    127132        public:
    128133            struct _UtilExport Testing
  • code/branches/testing/test/util/output/ConsoleWriterTest.cc

    r9538 r9540  
    6565    TEST(ConsoleWriterTest, DefaultConsoleWriterUsesCout)
    6666    {
     67        OutputManager::getInstanceAndCreateListeners();
    6768        EXPECT_EQ(std::cout, OutputManager::getInstance().getConsoleWriter().getOutputStream());
    6869    }
Note: See TracChangeset for help on using the changeset viewer.