Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-03-16 10:04:03 +0100
committerMartin Hořeňovský2023-03-20 20:46:41 +0100
commit173aa3f1f401ce1165ec9307d249dc506a976c3e (patch)
treeb8c0e8e4dde24a8e23a599b6325db3f6541f1663 /src/catch2
parent28437e1214fec5ffc4d744c75a894f9cddf8d635 (diff)
downloadcatch3-173aa3f1f401ce1165ec9307d249dc506a976c3e.tar.gz
Devirtualize Context
Diffstat (limited to 'src/catch2')
-rw-r--r--src/catch2/internal/catch_context.cpp52
-rw-r--r--src/catch2/internal/catch_context.hpp45
2 files changed, 34 insertions, 63 deletions
diff --git a/src/catch2/internal/catch_context.cpp b/src/catch2/internal/catch_context.cpp
index 17f28509..3b1cc277 100644
--- a/src/catch2/internal/catch_context.cpp
+++ b/src/catch2/internal/catch_context.cpp
@@ -11,49 +11,27 @@
namespace Catch {
- class Context : public IMutableContext, private Detail::NonCopyable {
+ Context* Context::currentContext = nullptr;
- public: // IContext
- IResultCapture* getResultCapture() override {
- return m_resultCapture;
- }
-
- 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()
- {
+ void cleanUpContext() {
+ delete Context::currentContext;
+ Context::currentContext = nullptr;
+ }
+ void Context::createContext() {
currentContext = new Context();
}
- void cleanUpContext() {
- delete IMutableContext::currentContext;
- IMutableContext::currentContext = nullptr;
+ Context& getCurrentMutableContext() {
+ if ( !Context::currentContext ) { Context::createContext(); }
+ // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
+ return *Context::currentContext;
+ }
+
+ void Context::setResultCapture( IResultCapture* resultCapture ) {
+ m_resultCapture = resultCapture;
}
- IContext::~IContext() = default;
- IMutableContext::~IMutableContext() = default;
- Context::~Context() = default;
+ void Context::setConfig( IConfig const* config ) { m_config = config; }
SimplePcg32& sharedRng() {
static SimplePcg32 s_rng;
diff --git a/src/catch2/internal/catch_context.hpp b/src/catch2/internal/catch_context.hpp
index a9d1b394..6ccb3b31 100644
--- a/src/catch2/internal/catch_context.hpp
+++ b/src/catch2/internal/catch_context.hpp
@@ -15,38 +15,31 @@ namespace Catch {
class IResultCapture;
class IConfig;
- class IContext {
- public:
- virtual ~IContext(); // = default
+ class Context {
+ IConfig const* m_config = nullptr;
+ IResultCapture* m_resultCapture = nullptr;
- virtual IResultCapture* getResultCapture() = 0;
- virtual IConfig const* getConfig() const = 0;
- };
+ CATCH_EXPORT static Context* currentContext;
+ friend Context& getCurrentMutableContext();
+ friend Context const& getCurrentContext();
+ static void createContext();
+ friend void cleanUpContext();
- 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();
+ 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()
- {
- if( !IMutableContext::currentContext )
- IMutableContext::createContext();
- // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
- return *IMutableContext::currentContext;
- }
+ Context& getCurrentMutableContext();
- inline IContext& getCurrentContext()
- {
- return getCurrentMutableContext();
+ inline Context const& getCurrentContext() {
+ // We duplicate the logic from `getCurrentMutableContext` here,
+ // 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();