Devirtualize Context

This commit is contained in:
Martin Hořeňovský 2023-03-16 10:04:03 +01:00
parent 28437e1214
commit 173aa3f1f4
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
2 changed files with 35 additions and 64 deletions

View File

@ -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;

View File

@ -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();