mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Refactoring
git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@723 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
parent
0e586516c8
commit
cf22b638e2
1 changed files with 142 additions and 199 deletions
|
|
@ -23,6 +23,79 @@ namespace mock
|
|||
{
|
||||
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
|
||||
{
|
||||
virtual ~holder()
|
||||
|
|
@ -57,203 +130,73 @@ namespace detail
|
|||
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_;
|
||||
functor_type f_;
|
||||
};
|
||||
|
||||
template< typename Result, typename Signature >
|
||||
class action< Result*, Signature >
|
||||
: public action_base< Result*, Signature >
|
||||
{
|
||||
typedef typename
|
||||
boost::function< Signature > functor_type;
|
||||
|
||||
typedef lambda< Result*, Signature > lambda_type;
|
||||
|
||||
public:
|
||||
void returns( Result* r )
|
||||
{
|
||||
f_ = lambda_type::make_val( r );
|
||||
set( lambda_type::make_val( r ) );
|
||||
}
|
||||
template< typename Y >
|
||||
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 >
|
||||
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;
|
||||
|
||||
public:
|
||||
action()
|
||||
: f_( lambda_type::make_nothing() )
|
||||
{}
|
||||
|
||||
void calls( const functor_type& f )
|
||||
{
|
||||
if( ! f )
|
||||
throw std::invalid_argument( "null functor" );
|
||||
f_ = f;
|
||||
set( lambda_type::make_nothing() );
|
||||
}
|
||||
|
||||
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 >
|
||||
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:
|
||||
action()
|
||||
{}
|
||||
action( const action& rhs )
|
||||
: v_( const_cast< action& >( rhs ).v_.release() )
|
||||
, f_( v_.get()
|
||||
? lambda_type::make_ref( boost::ref( v_ ) )
|
||||
: rhs.f_ )
|
||||
{}
|
||||
|
||||
template< typename Value >
|
||||
void returns( Value v )
|
||||
: v_( rhs.v_.release() )
|
||||
{
|
||||
set( v );
|
||||
if( v_.get() )
|
||||
returns( boost::ref( v_ ) );
|
||||
}
|
||||
|
||||
void calls( const functor_type& f )
|
||||
template< typename Y >
|
||||
void returns( Y* r )
|
||||
{
|
||||
if( ! f )
|
||||
throw std::invalid_argument( "null functor" );
|
||||
f_ = f;
|
||||
v_.reset( r );
|
||||
returns( boost::ref( v_ ) );
|
||||
}
|
||||
|
||||
template< typename Exception >
|
||||
void throws( Exception e )
|
||||
template< typename Y >
|
||||
void returns( std::auto_ptr< Y > r )
|
||||
{
|
||||
f_ = lambda_type::make_throw( e );
|
||||
v_ = r;
|
||||
returns( boost::ref( v_ ) );
|
||||
}
|
||||
|
||||
const functor_type& functor() const
|
||||
template< typename Y >
|
||||
void returns( const boost::reference_wrapper< Y >& r )
|
||||
{
|
||||
return f_;
|
||||
set( r );
|
||||
}
|
||||
|
||||
private:
|
||||
template< typename Y >
|
||||
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_;
|
||||
mutable std::auto_ptr< Result > v_;
|
||||
};
|
||||
}
|
||||
} // mock
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue