From 72e0daae5ce2b6a0cc27da5dfd9b1fd1b258cf87 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Thu, 22 Nov 2018 14:01:52 +0100 Subject: [PATCH] Implement and use simple singleton Based on old boost::unit_test::singleton --- include/turtle/detail/functor.hpp | 8 ++--- include/turtle/detail/mutex.hpp | 9 +++--- include/turtle/detail/root.hpp | 9 +++--- include/turtle/detail/singleton.hpp | 46 +++++++++++++++++++++++++++++ test/mock_error.hpp | 9 +++--- 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 include/turtle/detail/singleton.hpp diff --git a/include/turtle/detail/functor.hpp b/include/turtle/detail/functor.hpp index 232c7c9..53e776f 100644 --- a/include/turtle/detail/functor.hpp +++ b/include/turtle/detail/functor.hpp @@ -12,19 +12,19 @@ #include "../config.hpp" #include "function.hpp" #include "mutex.hpp" +#include "singleton.hpp" namespace mock { namespace detail { class functor_mutex_t : - public boost::unit_test::singleton< functor_mutex_t >, + public singleton< functor_mutex_t >, public mutex { - private: - BOOST_TEST_SINGLETON_CONS( functor_mutex_t ); + MOCK_SINGLETON_CONS( functor_mutex_t ); }; - BOOST_TEST_SINGLETON_INST( functor_mutex ) + MOCK_SINGLETON_INST( functor_mutex ) template< typename Signature > struct functor : function< Signature > diff --git a/include/turtle/detail/mutex.hpp b/include/turtle/detail/mutex.hpp index 4837c6c..4884da9 100644 --- a/include/turtle/detail/mutex.hpp +++ b/include/turtle/detail/mutex.hpp @@ -10,7 +10,7 @@ #define MOCK_MUTEX_HPP_INCLUDED #include "../config.hpp" -#include +#include "singleton.hpp" #include #ifdef MOCK_THREAD_SAFE @@ -93,13 +93,12 @@ namespace mock { namespace detail { - class error_mutex_t : public boost::unit_test::singleton< error_mutex_t >, + class error_mutex_t : public singleton< error_mutex_t >, public mutex { - private: - BOOST_TEST_SINGLETON_CONS( error_mutex_t ); + MOCK_SINGLETON_CONS( error_mutex_t ); }; - BOOST_TEST_SINGLETON_INST( error_mutex ) + MOCK_SINGLETON_INST( error_mutex ) #ifdef BOOST_MSVC # pragma warning( push ) diff --git a/include/turtle/detail/root.hpp b/include/turtle/detail/root.hpp index 2006a28..dae3790 100644 --- a/include/turtle/detail/root.hpp +++ b/include/turtle/detail/root.hpp @@ -15,7 +15,7 @@ #include "context.hpp" #include "child.hpp" #include "mutex.hpp" -#include +#include "singleton.hpp" #include #include #include @@ -24,7 +24,7 @@ namespace mock { namespace detail { - class root_t : public boost::unit_test::singleton< root_t >, public context + class root_t : public singleton< root_t >, public context { public: virtual void add( const void* p, verifiable& v, @@ -128,10 +128,9 @@ namespace detail group group_; mutable mutex mutex_; - private: - BOOST_TEST_SINGLETON_CONS( root_t ); + MOCK_SINGLETON_CONS( root_t ); }; - BOOST_TEST_SINGLETON_INST( root ) + MOCK_SINGLETON_INST( root ) } } // mock diff --git a/include/turtle/detail/singleton.hpp b/include/turtle/detail/singleton.hpp new file mode 100644 index 0000000..2080750 --- /dev/null +++ b/include/turtle/detail/singleton.hpp @@ -0,0 +1,46 @@ +// http://turtle.sourceforge.net +// +// Copyright Mathieu Champlon 2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef MOCK_SINGLETON_HPP +#define MOCK_SINGLETON_HPP + +#include + +namespace mock { +namespace detail { + +template< typename Derived > +class singleton { +public: + static Derived& instance() + { + static Derived the_inst; + return the_inst; + } + + BOOST_DELETED_FUNCTION(singleton(singleton const&)) + BOOST_DELETED_FUNCTION(singleton& operator=(singleton const&)) + +protected: + BOOST_DEFAULTED_FUNCTION(singleton(), {}) + BOOST_DEFAULTED_FUNCTION(~singleton(), {}) +}; + +} // detail +} // mock + +// Add a private ctor to the type to prevent misuse +#define MOCK_SINGLETON_CONS( type ) \ +private: \ +friend class mock::detail::singleton< type >; \ +type() {} + +#define MOCK_SINGLETON_INST( inst ) \ +static BOOST_JOIN( inst, _t )& inst = BOOST_JOIN( inst, _t )::instance(); + +#endif // MOCK_SINGLETON_HPP diff --git a/test/mock_error.hpp b/test/mock_error.hpp index ae95ca8..bd3fbe9 100644 --- a/test/mock_error.hpp +++ b/test/mock_error.hpp @@ -10,12 +10,12 @@ #define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED #define MOCK_ERROR_POLICY mock_error +#include #include #include -#include #include -struct mock_error_data_t : boost::unit_test::singleton< mock_error_data_t > +struct mock_error_data_t : mock::detail::singleton< mock_error_data_t > { void reset() { @@ -52,10 +52,9 @@ struct mock_error_data_t : boost::unit_test::singleton< mock_error_data_t > std::string last_context; std::string last_file; int last_line; -private: - BOOST_TEST_SINGLETON_CONS( mock_error_data_t ); + MOCK_SINGLETON_CONS( mock_error_data_t ); }; -BOOST_TEST_SINGLETON_INST( mock_error_data ) +MOCK_SINGLETON_INST( mock_error_data ) template< typename Result > struct mock_error