Resolve dependency on trivial_singleton.hpp from boost library

This commit is contained in:
David Vojtek 2018-11-11 18:29:55 +01:00
parent e92f52821d
commit a3e954c0c9
4 changed files with 301 additions and 220 deletions

View file

@ -15,25 +15,25 @@
namespace mock namespace mock
{ {
namespace detail namespace detail
{ {
class functor_mutex_t : class functor_mutex_t :
public boost::unit_test::singleton< functor_mutex_t >, public boost::unit_test::singleton< functor_mutex_t >,
public mutex public mutex
{ {
private: 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 > template< typename Signature >
struct functor : function< Signature > struct functor : function< Signature >
{ {
functor() functor()
{ {
scoped_lock _( functor_mutex ); scoped_lock _(functor_mutex);
static functor* f = 0; static functor* f = 0;
if( f ) if (f)
{ {
*this = *f; *this = *f;
f = 0; f = 0;
@ -46,7 +46,7 @@ namespace detail
} }
} }
}; };
} }
} // mock } // mock
#endif // MOCK_FUNCTOR_HPP_INCLUDED #endif // MOCK_FUNCTOR_HPP_INCLUDED

View file

@ -10,7 +10,8 @@
#define MOCK_MUTEX_HPP_INCLUDED #define MOCK_MUTEX_HPP_INCLUDED
#include "../config.hpp" #include "../config.hpp"
#include <boost/test/utils/trivial_singleton.hpp> #include <turtle/detail/singleton.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#ifdef MOCK_THREAD_SAFE #ifdef MOCK_THREAD_SAFE
@ -24,8 +25,8 @@
namespace mock namespace mock
{ {
namespace detail namespace detail
{ {
#ifdef MOCK_HDR_MUTEX #ifdef MOCK_HDR_MUTEX
typedef std::recursive_mutex mutex; typedef std::recursive_mutex mutex;
typedef std::lock_guard< mutex > scoped_lock; typedef std::lock_guard< mutex > scoped_lock;
@ -36,35 +37,35 @@ namespace detail
struct lock struct lock
{ {
lock( const boost::shared_ptr< mutex >& m ) lock(const boost::shared_ptr< mutex >& m)
: m_( m ) : m_(m)
{ {
m_->lock(); m_->lock();
} }
lock( const lock& rhs ) lock(const lock& rhs)
{ {
m_.swap( rhs.m_ ); m_.swap(rhs.m_);
} }
~lock() ~lock()
{ {
if( m_ ) if (m_)
m_->unlock(); m_->unlock();
} }
private: private:
lock& operator=( const lock& rhs ); lock& operator=(const lock& rhs);
mutable boost::shared_ptr< mutex > m_; mutable boost::shared_ptr< mutex > m_;
}; };
} }
} // mock } // mock
#else // MOCK_THREAD_SAFE #else // MOCK_THREAD_SAFE
namespace mock namespace mock
{ {
namespace detail namespace detail
{ {
struct mutex struct mutex
{ {
mutex() mutex()
@ -76,30 +77,30 @@ namespace detail
}; };
struct scoped_lock struct scoped_lock
{ {
scoped_lock( mutex& ) scoped_lock(mutex&)
{} {}
}; };
struct lock struct lock
{ {
lock( const boost::shared_ptr< mutex >& ) lock(const boost::shared_ptr< mutex >&)
{} {}
}; };
} }
} // mock } // mock
#endif // MOCK_THREAD_SAFE #endif // MOCK_THREAD_SAFE
namespace mock namespace mock
{ {
namespace detail namespace detail
{ {
class error_mutex_t : public boost::unit_test::singleton< error_mutex_t >, class error_mutex_t : public boost::unit_test::singleton< error_mutex_t >,
public mutex public mutex
{ {
private: 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 #ifdef BOOST_MSVC
# pragma warning( push ) # pragma warning( push )
@ -110,32 +111,32 @@ namespace detail
{ {
static Result abort() static Result abort()
{ {
scoped_lock _( error_mutex ); scoped_lock _(error_mutex);
return Error::abort(); return Error::abort();
} }
template< typename Context > template< typename Context >
static void fail( const char* message, const Context& context, static void fail(const char* message, const Context& context,
const char* file = "unknown location", int line = 0 ) const char* file = "unknown location", int line = 0)
{ {
scoped_lock _( error_mutex ); scoped_lock _(error_mutex);
Error::fail( message, context, file, line ); Error::fail(message, context, file, line);
} }
template< typename Context > template< typename Context >
static void call( const Context& context, const char* file, int line ) static void call(const Context& context, const char* file, int line)
{ {
scoped_lock _( error_mutex ); scoped_lock _(error_mutex);
Error::call( context, file, line ); Error::call(context, file, line);
} }
static void pass( const char* file, int line ) static void pass(const char* file, int line)
{ {
scoped_lock _( error_mutex ); scoped_lock _(error_mutex);
Error::pass( file, line ); Error::pass(file, line);
} }
}; };
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
# pragma warning( pop ) # pragma warning( pop )
#endif #endif
} }
} // mock } // mock
#endif // MOCK_MUTEX_HPP_INCLUDED #endif // MOCK_MUTEX_HPP_INCLUDED

View file

@ -15,60 +15,60 @@
#include "context.hpp" #include "context.hpp"
#include "child.hpp" #include "child.hpp"
#include "mutex.hpp" #include "mutex.hpp"
#include <boost/test/utils/trivial_singleton.hpp> #include "singleton.hpp"
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <ostream> #include <ostream>
#include <map> #include <map>
namespace mock namespace mock
{ {
namespace detail namespace detail
{ {
class root_t : public boost::unit_test::singleton< root_t >, public context class root_t : public boost::unit_test::singleton< root_t >, public context
{ {
public: public:
virtual void add( const void* p, verifiable& v, virtual void add(const void* p, verifiable& v,
boost::unit_test::const_string instance, boost::unit_test::const_string instance,
boost::optional< type_name > type, boost::optional< type_name > type,
boost::unit_test::const_string name ) boost::unit_test::const_string name)
{ {
scoped_lock _( mutex_ ); scoped_lock _(mutex_);
children_t::iterator it = children_.lower_bound( &v ); children_t::iterator it = children_.lower_bound(&v);
if( it == children_.end() || if (it == children_.end() ||
children_.key_comp()( &v, it->first ) ) children_.key_comp()(&v, it->first))
it = children_.insert( it, it = children_.insert(it,
std::make_pair( &v, counter_child( parents_, p ) ) ); std::make_pair(&v, counter_child(parents_, p)));
it->second.update( instance, type, name ); it->second.update(instance, type, name);
} }
virtual void add( verifiable& v ) virtual void add(verifiable& v)
{ {
scoped_lock _( mutex_ ); scoped_lock _(mutex_);
group_.add( v ); group_.add(v);
} }
virtual void remove( verifiable& v ) virtual void remove(verifiable& v)
{ {
scoped_lock _( mutex_ ); scoped_lock _(mutex_);
group_.remove( v ); group_.remove(v);
children_.erase( &v ); children_.erase(&v);
} }
bool verify() const bool verify() const
{ {
scoped_lock _( mutex_ ); scoped_lock _(mutex_);
return group_.verify(); return group_.verify();
} }
void reset() void reset()
{ {
scoped_lock _( mutex_ ); scoped_lock _(mutex_);
group_.reset(); group_.reset();
} }
virtual void serialize( std::ostream& s, const verifiable& v ) const virtual void serialize(std::ostream& s, const verifiable& v) const
{ {
scoped_lock _( mutex_ ); scoped_lock _(mutex_);
children_cit it = children_.find( &v ); children_cit it = children_.find(&v);
if( it != children_.end() ) if (it != children_.end())
s << it->second; s << it->second;
else else
s << "?"; s << "?";
@ -81,39 +81,39 @@ namespace detail
class counter_child class counter_child
{ {
public: public:
counter_child( parents_t& parents, const void* p ) counter_child(parents_t& parents, const void* p)
: parents_( &parents ) : parents_(&parents)
, it_( parents.insert( , it_(parents.insert(
std::make_pair( p, parents_t::mapped_type() ) ).first ) std::make_pair(p, parents_t::mapped_type())).first)
{ {
++it_->second.second; ++it_->second.second;
} }
counter_child( const counter_child& rhs ) counter_child(const counter_child& rhs)
: parents_( rhs.parents_ ) : parents_(rhs.parents_)
, it_( rhs.it_ ) , it_(rhs.it_)
, child_( rhs.child_ ) , child_(rhs.child_)
{ {
++it_->second.second; ++it_->second.second;
} }
~counter_child() ~counter_child()
{ {
if( --it_->second.second == 0 ) if (--it_->second.second == 0)
parents_->erase( it_ ); parents_->erase(it_);
} }
void update( boost::unit_test::const_string instance, void update(boost::unit_test::const_string instance,
boost::optional< type_name > type, boost::optional< type_name > type,
boost::unit_test::const_string name ) boost::unit_test::const_string name)
{ {
child_.update( it_->second.first, instance, type, name ); child_.update(it_->second.first, instance, type, name);
} }
friend std::ostream& operator<<( std::ostream& s, friend std::ostream& operator<<(std::ostream& s,
const counter_child& c ) const counter_child& c)
{ {
return s << c.child_; return s << c.child_;
} }
private: private:
counter_child& operator=( const counter_child& ); counter_child& operator=(const counter_child&);
parents_t* parents_; parents_t* parents_;
parents_t::iterator it_; parents_t::iterator it_;
@ -129,10 +129,10 @@ namespace detail
mutable mutex mutex_; mutable mutex mutex_;
private: private:
BOOST_TEST_SINGLETON_CONS( root_t ); MOCK_SINGLETON_CONS(root_t);
}; };
BOOST_TEST_SINGLETON_INST( root ) MOCK_SINGLETON_INST(root)
} }
} // mock } // mock
#endif // MOCK_ROOT_HPP_INCLUDED #endif // MOCK_ROOT_HPP_INCLUDED

View file

@ -0,0 +1,80 @@
// (C) Copyright Gennadiy Rozental 2005-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)
// See http://www.boost.org/libs/test for the library home page.
//
// File : $RCSfile$
//
// Version : $Revision$
//
// Description : simple helpers for creating cusom output manipulators
// ***************************************************************************
#ifndef MOCK_UTILS_TRIVIAL_SIGNLETON_HPP
#define MOCK_UTILS_TRIVIAL_SIGNLETON_HPP
// Boost.Test
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
// Boost
#include <boost/test/detail/suppress_warnings.hpp>
//____________________________________________________________________________//
namespace boost {
namespace unit_test {
// ************************************************************************** //
// ************** singleton ************** //
// ************************************************************************** //
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(), {})
};
//____________________________________________________________________________//
#define MOCK_SINGLETON_CONS( type ) \
friend class boost::unit_test::singleton<type>; \
type() {} \
/**/
#if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
#define MOCK_SINGLETON_INST( inst ) \
template class unit_test::singleton< BOOST_JOIN( inst, _t ) > ; \
namespace { BOOST_JOIN( inst, _t)& inst = BOOST_JOIN( inst, _t)::instance(); }
#elif defined(__APPLE_CC__) && defined(__GNUC__) && __GNUC__ < 4
#define MOCK_SINGLETON_INST( inst ) \
static BOOST_JOIN( inst, _t)& inst = BOOST_JOIN (inst, _t)::instance();
#else
#define MOCK_SINGLETON_INST( inst ) \
namespace { BOOST_JOIN( inst, _t)& inst = BOOST_JOIN( inst, _t)::instance(); }
#endif
//____________________________________________________________________________//
} // namespace unit_test
} // namespace boost
#include <boost/test/detail/enable_warnings.hpp>
#endif // MOCK_UTILS_TRIVIAL_SIGNLETON_HPP