mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Devirtualize Context
This commit is contained in:
parent
28437e1214
commit
173aa3f1f4
@ -11,49 +11,27 @@
|
|||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
class Context : public IMutableContext, private Detail::NonCopyable {
|
Context* Context::currentContext = nullptr;
|
||||||
|
|
||||||
public: // IContext
|
void cleanUpContext() {
|
||||||
IResultCapture* getResultCapture() override {
|
delete Context::currentContext;
|
||||||
return m_resultCapture;
|
Context::currentContext = nullptr;
|
||||||
}
|
}
|
||||||
|
void Context::createContext() {
|
||||||
IConfig const* getConfig() const override {
|
|
||||||
return m_config;
|
|
||||||
}
|
|
||||||
|
|
||||||
~Context() override;
|
|
||||||
|
|
||||||
public: // IMutableContext
|
|
||||||
void setResultCapture( IResultCapture* resultCapture ) override {
|
|
||||||
m_resultCapture = resultCapture;
|
|
||||||
}
|
|
||||||
void setConfig( IConfig const* config ) override {
|
|
||||||
m_config = config;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend IMutableContext& getCurrentMutableContext();
|
|
||||||
|
|
||||||
private:
|
|
||||||
IConfig const* m_config = nullptr;
|
|
||||||
IResultCapture* m_resultCapture = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
IMutableContext *IMutableContext::currentContext = nullptr;
|
|
||||||
|
|
||||||
void IMutableContext::createContext()
|
|
||||||
{
|
|
||||||
currentContext = new Context();
|
currentContext = new Context();
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanUpContext() {
|
Context& getCurrentMutableContext() {
|
||||||
delete IMutableContext::currentContext;
|
if ( !Context::currentContext ) { Context::createContext(); }
|
||||||
IMutableContext::currentContext = nullptr;
|
// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
|
||||||
|
return *Context::currentContext;
|
||||||
}
|
}
|
||||||
IContext::~IContext() = default;
|
|
||||||
IMutableContext::~IMutableContext() = default;
|
|
||||||
Context::~Context() = default;
|
|
||||||
|
|
||||||
|
void Context::setResultCapture( IResultCapture* resultCapture ) {
|
||||||
|
m_resultCapture = resultCapture;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Context::setConfig( IConfig const* config ) { m_config = config; }
|
||||||
|
|
||||||
SimplePcg32& sharedRng() {
|
SimplePcg32& sharedRng() {
|
||||||
static SimplePcg32 s_rng;
|
static SimplePcg32 s_rng;
|
||||||
|
@ -15,38 +15,31 @@ namespace Catch {
|
|||||||
class IResultCapture;
|
class IResultCapture;
|
||||||
class IConfig;
|
class IConfig;
|
||||||
|
|
||||||
class IContext {
|
class Context {
|
||||||
public:
|
IConfig const* m_config = nullptr;
|
||||||
virtual ~IContext(); // = default
|
IResultCapture* m_resultCapture = nullptr;
|
||||||
|
|
||||||
virtual IResultCapture* getResultCapture() = 0;
|
CATCH_EXPORT static Context* currentContext;
|
||||||
virtual IConfig const* getConfig() const = 0;
|
friend Context& getCurrentMutableContext();
|
||||||
};
|
friend Context const& getCurrentContext();
|
||||||
|
|
||||||
class IMutableContext : public IContext {
|
|
||||||
public:
|
|
||||||
~IMutableContext() override; // = default
|
|
||||||
virtual void setResultCapture( IResultCapture* resultCapture ) = 0;
|
|
||||||
virtual void setConfig( IConfig const* config ) = 0;
|
|
||||||
|
|
||||||
private:
|
|
||||||
CATCH_EXPORT static IMutableContext* currentContext;
|
|
||||||
friend IMutableContext& getCurrentMutableContext();
|
|
||||||
friend void cleanUpContext();
|
|
||||||
static void createContext();
|
static void createContext();
|
||||||
|
friend void cleanUpContext();
|
||||||
|
|
||||||
|
public:
|
||||||
|
IResultCapture* getResultCapture() const { return m_resultCapture; }
|
||||||
|
IConfig const* getConfig() const { return m_config; }
|
||||||
|
void setResultCapture( IResultCapture* resultCapture );
|
||||||
|
void setConfig( IConfig const* config );
|
||||||
};
|
};
|
||||||
|
|
||||||
inline IMutableContext& getCurrentMutableContext()
|
Context& getCurrentMutableContext();
|
||||||
{
|
|
||||||
if( !IMutableContext::currentContext )
|
|
||||||
IMutableContext::createContext();
|
|
||||||
// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
|
|
||||||
return *IMutableContext::currentContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline IContext& getCurrentContext()
|
inline Context const& getCurrentContext() {
|
||||||
{
|
// We duplicate the logic from `getCurrentMutableContext` here,
|
||||||
return getCurrentMutableContext();
|
// to avoid paying the call overhead in debug mode.
|
||||||
|
if ( !Context::currentContext ) { Context::createContext(); }
|
||||||
|
// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
|
||||||
|
return *Context::currentContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanUpContext();
|
void cleanUpContext();
|
||||||
|
Loading…
Reference in New Issue
Block a user