Refactoring

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@723 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2014-04-26 07:06:44 +00:00
parent 0e586516c8
commit cf22b638e2

View file

@ -23,6 +23,79 @@ namespace mock
{ {
namespace detail namespace detail
{ {
template< typename Result, typename Signature >
class action_base
{
private:
typedef boost::function< Signature > functor_type;
typedef lambda< Result, Signature > lambda_type;
public:
const functor_type& functor() const
{
return f_;
}
void calls( const functor_type& f )
{
if( ! f )
throw std::invalid_argument( "null functor" );
f_ = f;
}
template< typename Exception >
void throws( Exception e )
{
f_ = lambda_type::make_throw( e );
}
protected:
void set( const functor_type& f )
{
f_ = f;
}
template< typename Y >
void set( const boost::reference_wrapper< Y >& r )
{
f_ = lambda_type::make_ref( r );
}
functor_type f_;
};
template< typename Result, typename Signature >
class action : public action_base< Result, Signature >
{
typedef lambda< Result, Signature > lambda_type;
public:
template< typename Value >
void returns( const Value& v )
{
returns( boost::ref( v_.store( v ) ) );
}
template< typename Value >
void returns( Value* v )
{
typedef typename
boost::remove_reference<
typename boost::remove_const< Result >::type
>::type result_type;
returns( result_type( v ) );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
{
set( r );
}
template< typename Value >
void moves( BOOST_RV_REF( Value ) v )
{
set( lambda_type::make_move( v_.store( boost::move( v ) ) ) );
}
private:
struct holder : boost::noncopyable struct holder : boost::noncopyable
{ {
virtual ~holder() virtual ~holder()
@ -57,203 +130,73 @@ namespace detail
boost::shared_ptr< holder > h_; boost::shared_ptr< holder > h_;
}; };
template< typename Result, typename Signature >
class action
{
typedef typename
boost::function< Signature > functor_type;
typedef typename
boost::remove_reference<
typename boost::remove_const< Result >::type
>::type result_type;
typedef lambda< Result, Signature > lambda_type;
public:
template< typename Value >
void returns( const Value& v )
{
f_ = lambda_type::make_ref( boost::ref( v_.store( v ) ) );
}
template< typename Value >
void returns( Value* v )
{
returns( result_type( v ) );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
{
f_ = lambda_type::make_ref( r );
}
template< typename Value >
void moves( BOOST_RV_REF( Value ) v )
{
f_ = lambda_type::make_move( v_.store( boost::move( v ) ) );
}
void calls( const functor_type& f )
{
if( ! f )
throw std::invalid_argument( "null functor" );
f_ = f;
}
template< typename Exception >
void throws( Exception e )
{
f_ = lambda_type::make_throw( e );
}
const functor_type& functor() const
{
return f_;
}
private:
value v_; value v_;
functor_type f_;
}; };
template< typename Result, typename Signature > template< typename Result, typename Signature >
class action< Result*, Signature > class action< Result*, Signature >
: public action_base< Result*, Signature >
{ {
typedef typename
boost::function< Signature > functor_type;
typedef lambda< Result*, Signature > lambda_type; typedef lambda< Result*, Signature > lambda_type;
public: public:
void returns( Result* r ) void returns( Result* r )
{ {
f_ = lambda_type::make_val( r ); set( lambda_type::make_val( r ) );
} }
template< typename Y > template< typename Y >
void returns( const boost::reference_wrapper< Y >& r ) void returns( const boost::reference_wrapper< Y >& r )
{ {
f_ = lambda_type::make_ref( r ); set( r );
} }
void calls( const functor_type& f )
{
if( ! f )
throw std::invalid_argument( "null functor" );
f_ = f;
}
template< typename Exception >
void throws( Exception e )
{
f_ = lambda_type::make_throw( e );
}
const functor_type& functor() const
{
return f_;
}
private:
functor_type f_;
}; };
template< typename Signature > template< typename Signature >
class action< void, Signature > class action< void, Signature > : public action_base< void, Signature >
{ {
typedef typename
boost::function< Signature > functor_type;
typedef lambda< void, Signature > lambda_type; typedef lambda< void, Signature > lambda_type;
public: public:
action() action()
: f_( lambda_type::make_nothing() )
{}
void calls( const functor_type& f )
{ {
if( ! f ) set( lambda_type::make_nothing() );
throw std::invalid_argument( "null functor" );
f_ = f;
} }
template< typename Exception >
void throws( Exception e )
{
f_ = lambda_type::make_throw( e );
}
const functor_type& functor() const
{
return f_;
}
private:
functor_type f_;
}; };
template< typename Result, typename Signature > template< typename Result, typename Signature >
class action< std::auto_ptr< Result >, Signature > class action< std::auto_ptr< Result >, Signature >
: public action_base< std::auto_ptr< Result >, Signature >
{ {
typedef typename
boost::function< Signature > functor_type;
typedef lambda< std::auto_ptr< Result >, Signature > lambda_type;
public: public:
action() action()
{} {}
action( const action& rhs ) action( const action& rhs )
: v_( const_cast< action& >( rhs ).v_.release() ) : v_( rhs.v_.release() )
, f_( v_.get()
? lambda_type::make_ref( boost::ref( v_ ) )
: rhs.f_ )
{}
template< typename Value >
void returns( Value v )
{ {
set( v ); if( v_.get() )
returns( boost::ref( v_ ) );
} }
void calls( const functor_type& f ) template< typename Y >
void returns( Y* r )
{ {
if( ! f ) v_.reset( r );
throw std::invalid_argument( "null functor" ); returns( boost::ref( v_ ) );
f_ = f;
} }
template< typename Y >
template< typename Exception > void returns( std::auto_ptr< Y > r )
void throws( Exception e )
{ {
f_ = lambda_type::make_throw( e ); v_ = r;
returns( boost::ref( v_ ) );
} }
template< typename Y >
const functor_type& functor() const void returns( const boost::reference_wrapper< Y >& r )
{ {
return f_; set( r );
} }
private: private:
template< typename Y > mutable std::auto_ptr< Result > v_;
void set( std::auto_ptr< Y > r )
{
v_ = r;
f_ = lambda_type::make_ref( boost::ref( v_ ) );
}
template< typename Y >
void set( const boost::reference_wrapper< Y >& r )
{
f_ = lambda_type::make_ref( r );
}
template< typename Y >
void set( Y* r )
{
v_.reset( r );
f_ = lambda_type::make_ref( boost::ref( v_ ) );
}
std::auto_ptr< Result > v_;
functor_type f_;
}; };
} }
} // mock } // mock