-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.hpp
217 lines (183 loc) · 5.33 KB
/
action.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Copyright Mathieu Champlon 2008
//
// 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_ACTION_HPP_INCLUDED
#define MOCK_ACTION_HPP_INCLUDED
#include "lambda.hpp"
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/ref.hpp>
namespace mock
{
namespace detail
{
template< typename Result, typename Signature >
class action
{
typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type;
typedef BOOST_DEDUCED_TYPENAME
boost::remove_reference<
BOOST_DEDUCED_TYPENAME boost::remove_const< Result >::type
>::type result_type;
typedef lambda< Result, Signature > lambda_type;
public:
template< typename Value >
void returns( Value v )
{
r_.reset( new result_type( v ) );
f_ = lambda_type::make_val( boost::ref( *r_ ) );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
{
f_ = lambda_type::make_val( 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:
boost::shared_ptr< result_type > r_;
functor_type f_;
};
template< typename Result, typename Signature >
class action< Result*, Signature >
{
typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type;
typedef lambda< Result*, Signature > lambda_type;
public:
void returns( Result* r )
{
f_ = lambda_type::make_val( r );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
{
f_ = lambda_type::make_val( 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 >
{
typedef BOOST_DEDUCED_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;
}
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 >
{
typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type;
typedef lambda< std::auto_ptr< Result >, Signature > lambda_type;
public:
action()
: r_()
, f_()
{}
action( const action& rhs )
: r_( const_cast< action& >( rhs ).r_.release() )
, f_( r_.get() ? lambda_type::make_val( boost::ref( r_ ) ) : rhs.f_ )
{}
template< typename Value >
void returns( Value v )
{
set( 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 );
r_.reset();
}
const functor_type& functor() const
{
return f_;
}
private:
template< typename Y >
void set( std::auto_ptr< Y > r )
{
r_ = r;
f_ = lambda_type::make_val( boost::ref( r_ ) );
}
template< typename Y >
void set( const boost::reference_wrapper< Y >& r )
{
f_ = lambda_type::make_val( r );
r_.reset();
}
template< typename Y >
void set( Y* r )
{
r_.reset( r );
f_ = lambda_type::make_val( boost::ref( r_ ) );
}
mutable std::auto_ptr< Result > r_;
functor_type f_;
};
}
}
#endif // MOCK_ACTION_HPP_INCLUDED