Implement and use simple singleton

Based on old boost::unit_test::singleton
This commit is contained in:
Alexander Grund 2018-11-22 14:01:52 +01:00
parent d77aad128c
commit 72e0daae5c
5 changed files with 62 additions and 19 deletions

View file

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

View file

@ -10,7 +10,7 @@
#define MOCK_MUTEX_HPP_INCLUDED
#include "../config.hpp"
#include <boost/test/utils/trivial_singleton.hpp>
#include "singleton.hpp"
#include <boost/shared_ptr.hpp>
#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 )

View file

@ -15,7 +15,7 @@
#include "context.hpp"
#include "child.hpp"
#include "mutex.hpp"
#include <boost/test/utils/trivial_singleton.hpp>
#include "singleton.hpp"
#include <boost/optional.hpp>
#include <ostream>
#include <map>
@ -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

View file

@ -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 <boost/config.hpp>
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