Moved context object entirely into impl file

This commit is contained in:
Phil Nash 2012-08-08 08:33:54 +01:00
parent 0477465f8d
commit fb386f458d
4 changed files with 78 additions and 100 deletions

View File

@ -127,7 +127,7 @@ namespace Catch {
}
void useStream( const std::string& streamName ) {
std::streambuf* newBuf = Context::createStreamBuf( streamName );
std::streambuf* newBuf = createStreamBuf( streamName );
setStreamBuf( newBuf );
delete m_streambuf;
m_streambuf = newBuf;

View File

@ -45,41 +45,9 @@ namespace Catch {
IContext& getCurrentContext();
IMutableContext& getCurrentMutableContext();
void cleanUpContext();
std::streambuf* createStreamBuf( const std::string& streamName );
class Context : public IMutableContext {
Context();
Context( const Context& );
void operator=( const Context& );
public: // IContext
virtual IResultCapture& getResultCapture();
virtual IRunner& getRunner();
virtual size_t getGeneratorIndex( const std::string& fileInfo, size_t totalSize );
virtual bool advanceGeneratorsForCurrentTest();
virtual const IConfig* getConfig() const;
public: // IMutableContext
virtual void setResultCapture( IResultCapture* resultCapture );
virtual void setRunner( IRunner* runner );
virtual void setConfig( const IConfig* config );
public: // Statics
static std::streambuf* createStreamBuf( const std::string& streamName );
static void cleanUp();
friend IMutableContext& getCurrentMutableContext();
private:
IGeneratorsForTest* findGeneratorsForCurrentTest();
IGeneratorsForTest& getGeneratorsForCurrentTest();
private:
IRunner* m_runner;
IResultCapture* m_resultCapture;
const IConfig* m_config;
std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName;
};
}
#endif // TWOBLUECUBES_CATCH_CONTEXT_H_INCLUDED

View File

@ -12,6 +12,74 @@
namespace Catch {
class Context : public IMutableContext {
Context() : m_config( NULL ) {}
Context( const Context& );
void operator=( const Context& );
public: // IContext
virtual IResultCapture& getResultCapture() {
return *m_resultCapture;
}
virtual IRunner& getRunner() {
return *m_runner;
}
virtual size_t getGeneratorIndex( const std::string& fileInfo, size_t totalSize ) {
return getGeneratorsForCurrentTest()
.getGeneratorInfo( fileInfo, totalSize )
.getCurrentIndex();
}
virtual bool advanceGeneratorsForCurrentTest() {
IGeneratorsForTest* generators = findGeneratorsForCurrentTest();
return generators && generators->moveNext();
}
virtual const IConfig* getConfig() const {
return m_config;
}
public: // IMutableContext
virtual void setResultCapture( IResultCapture* resultCapture ) {
m_resultCapture = resultCapture;
}
virtual void setRunner( IRunner* runner ) {
m_runner = runner;
}
virtual void setConfig( const IConfig* config ) {
m_config = config;
}
friend IMutableContext& getCurrentMutableContext();
private:
IGeneratorsForTest* findGeneratorsForCurrentTest() {
std::string testName = getResultCapture().getCurrentTestName();
std::map<std::string, IGeneratorsForTest*>::const_iterator it =
m_generatorsByTestName.find( testName );
return it != m_generatorsByTestName.end()
? it->second
: NULL;
}
IGeneratorsForTest& getGeneratorsForCurrentTest() {
IGeneratorsForTest* generators = findGeneratorsForCurrentTest();
if( !generators ) {
std::string testName = getResultCapture().getCurrentTestName();
generators = createGeneratorsForTest();
m_generatorsByTestName.insert( std::make_pair( testName, generators ) );
}
return *generators;
}
private:
IRunner* m_runner;
IResultCapture* m_resultCapture;
const IConfig* m_config;
std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName;
};
namespace {
Context* currentContext = NULL;
}
@ -24,39 +92,7 @@ namespace Catch {
return getCurrentMutableContext();
}
Context::Context()
: m_config( NULL )
{}
void Context::cleanUp() {
delete currentContext;
currentContext = NULL;
}
void Context::setRunner( IRunner* runner ) {
m_runner = runner;
}
void Context::setResultCapture( IResultCapture* resultCapture ) {
m_resultCapture = resultCapture;
}
const IConfig* Context::getConfig() const {
return m_config;
}
void Context::setConfig( const IConfig* config ) {
m_config = config;
}
IResultCapture& Context::getResultCapture() {
return *m_resultCapture;
}
IRunner& Context::getRunner() {
return *m_runner;
}
std::streambuf* Context::createStreamBuf( const std::string& streamName ) {
std::streambuf* createStreamBuf( const std::string& streamName ) {
if( streamName == "stdout" ) return std::cout.rdbuf();
if( streamName == "stderr" ) return std::cerr.rdbuf();
if( streamName == "debug" ) return new StreamBufImpl<OutputDebugWriter>;
@ -64,34 +100,8 @@ namespace Catch {
throw std::domain_error( "Unknown stream: " + streamName );
}
IGeneratorsForTest* Context::findGeneratorsForCurrentTest() {
std::string testName = getResultCapture().getCurrentTestName();
std::map<std::string, IGeneratorsForTest*>::const_iterator it =
m_generatorsByTestName.find( testName );
return it != m_generatorsByTestName.end()
? it->second
: NULL;
}
IGeneratorsForTest& Context::getGeneratorsForCurrentTest() {
IGeneratorsForTest* generators = findGeneratorsForCurrentTest();
if( !generators ) {
std::string testName = getResultCapture().getCurrentTestName();
generators = createGeneratorsForTest();
m_generatorsByTestName.insert( std::make_pair( testName, generators ) );
}
return *generators;
}
size_t Context::getGeneratorIndex( const std::string& fileInfo, size_t totalSize ) {
return getGeneratorsForCurrentTest()
.getGeneratorInfo( fileInfo, totalSize )
.getCurrentIndex();
}
bool Context::advanceGeneratorsForCurrentTest() {
IGeneratorsForTest* generators = findGeneratorsForCurrentTest();
return generators && generators->moveNext();
void cleanUpContext() {
delete currentContext;
currentContext = NULL;
}
}

View File

@ -68,7 +68,7 @@ namespace Catch {
void cleanUp() {
delete getTheRegistryHub();
getTheRegistryHub() = NULL;
Context::cleanUp();
cleanUpContext();
}
} // end namespace Catch