summaryrefslogtreecommitdiff
path: root/boost/test/utils/callback.hpp
blob: 6aa0a157730b25834cda690722fb757108a56ce2 (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//  (C) Copyright Gennadiy Rozental 2005-2008.
//  Use, modification, and distribution are subject to 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 : 
// ***************************************************************************

#ifndef BOOST_TEST_CALLBACK_020505GER
#define BOOST_TEST_CALLBACK_020505GER

// Boost
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/shared_ptr.hpp>

#include <boost/test/detail/suppress_warnings.hpp>

#if BOOST_WORKAROUND(BOOST_MSVC, < 1300) || BOOST_WORKAROUND(BOOST_INTEL, <= 700)
#  define BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
#endif

//____________________________________________________________________________//

namespace boost {

namespace unit_test {

namespace ut_detail {

struct unused {};

template<typename R>
struct invoker {
    template<typename Functor>
    R       invoke( Functor& f )                        { return f(); }
    template<typename Functor, typename T1>
    R       invoke( Functor& f, T1 t1 )                 { return f( t1 ); }
    template<typename Functor, typename T1, typename T2>
    R       invoke( Functor& f, T1 t1, T2 t2 )          { return f( t1, t2 ); }
    template<typename Functor, typename T1, typename T2, typename T3>
    R       invoke( Functor& f, T1 t1, T2 t2, T3 t3 )   { return f( t1, t2, t3 ); }
};

//____________________________________________________________________________//

template<>
struct invoker<unused> {
    template<typename Functor>
    unused  invoke( Functor& f )                        { f(); return unused(); }
    template<typename Functor, typename T1>
    unused  invoke( Functor& f, T1 t1 )                 { f( t1 ); return unused(); }
    template<typename Functor, typename T1, typename T2>
    unused  invoke( Functor& f, T1 t1, T2 t2 )          { f( t1, t2 ); return unused(); }
    template<typename Functor, typename T1, typename T2, typename T3>
    unused  invoke( Functor& f, T1 t1, T2 t2, T3 t3 )   { f( t1, t2, t3 ); return unused(); }
};

//____________________________________________________________________________//

} // namespace ut_detail

// ************************************************************************** //
// **************             unit_test::callback0             ************** //
// ************************************************************************** //

namespace ut_detail {

template<typename R>
struct callback0_impl {
    virtual ~callback0_impl() {}

    virtual R invoke() = 0;
};

//____________________________________________________________________________//

template<typename R, typename Functor>
struct callback0_impl_t : callback0_impl<R> {
    // Constructor
    explicit callback0_impl_t( Functor f ) : m_f( f ) {}

    virtual R invoke() { return invoker<R>().invoke( m_f ); }

private:
    // Data members
    Functor m_f;
};

//____________________________________________________________________________//

} // namespace ut_detail

template<typename R = ut_detail::unused>
class callback0 {
public:
    // Constructors
    callback0() {}
#ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
    callback0( callback0 const& rhs ) : m_impl( rhs.m_impl ) {}
#endif

    template<typename Functor>
    callback0( Functor f )
    : m_impl( new ut_detail::callback0_impl_t<R,Functor>( f ) ) {}
    
    void        operator=( callback0 const& rhs ) { m_impl = rhs.m_impl; }

    template<typename Functor>
    void        operator=( Functor f ) { m_impl.reset( new ut_detail::callback0_impl_t<R,Functor>( f ) );  }

    R           operator()() const { return m_impl->invoke(); }

    bool        operator!() const { return !m_impl; }

private:
    // Data members
    boost::shared_ptr<ut_detail::callback0_impl<R> > m_impl;
};

// ************************************************************************** //
// **************             unit_test::callback1             ************** //
// ************************************************************************** //

namespace ut_detail {

template<typename R, typename T1>
struct callback1_impl {
    virtual ~callback1_impl() {}

    virtual R invoke( T1 t1 ) = 0;
};

//____________________________________________________________________________//

template<typename R, typename T1,typename Functor>
struct callback1_impl_t : callback1_impl<R,T1> {
    // Constructor
    explicit callback1_impl_t( Functor f ) : m_f( f ) {}

    virtual R invoke( T1 t1 ) { return invoker<R>().invoke( m_f, t1 ); }

private:
    // Data members
    Functor m_f;
};

//____________________________________________________________________________//

} // namespace ut_detail

template<typename T1,typename R = ut_detail::unused>
class callback1 {
public:
    // Constructors
    callback1() {}
#ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
    callback1( callback1 const& rhs ) : m_impl( rhs.m_impl ) {}
#endif

    template<typename Functor>
    callback1( Functor f )
    : m_impl( new ut_detail::callback1_impl_t<R,T1,Functor>( f ) ) {}

    void        operator=( callback1 const& rhs ) { m_impl = rhs.m_impl; }

    template<typename Functor>
    void        operator=( Functor f ) { m_impl.reset( new ut_detail::callback1_impl_t<R,T1,Functor>( f ) );  }

    R           operator()( T1 t1 ) const { return m_impl->invoke( t1 ); }

    bool        operator!() const { return !m_impl; }

private:
    // Data members
    boost::shared_ptr<ut_detail::callback1_impl<R,T1> > m_impl;
};

// ************************************************************************** //
// **************             unit_test::callback2             ************** //
// ************************************************************************** //

namespace ut_detail {

template<typename R, typename T1,typename T2>
struct callback2_impl {
    virtual ~callback2_impl() {}

    virtual R invoke( T1 t1, T2 t2 ) = 0;
};

//____________________________________________________________________________//

template<typename R, typename T1, typename T2, typename Functor>
struct callback2_impl_t : callback2_impl<R,T1,T2> {
    // Constructor
    explicit callback2_impl_t( Functor f ) : m_f( f ) {}

    virtual R invoke( T1 t1, T2 t2 ) { return invoker<R>().template invoke<Functor,T1,T2>( m_f, t1, t2 ); }

private:
    // Data members
    Functor m_f;
};

//____________________________________________________________________________//

} // namespace ut_detail

template<typename T1,typename T2, typename R = ut_detail::unused>
class callback2 {
public:
    // Constructors
    callback2() {}
#ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
    callback2( callback2 const& rhs ) : m_impl( rhs.m_impl ) {}
#endif

    template<typename Functor>
                callback2( Functor f ) : m_impl( new ut_detail::callback2_impl_t<R,T1,T2,Functor>( f ) ) {}

    void        operator=( callback2 const& rhs ) { m_impl = rhs.m_impl; }

    template<typename Functor>
    void        operator=( Functor f ) { m_impl.reset( new ut_detail::callback2_impl_t<R,T1,T2,Functor>( f ) );  }

    R           operator()( T1 t1, T2 t2 ) const { return m_impl->invoke( t1, t2 ); }

    bool        operator!() const { return !m_impl; }

private:
    // Data members
    boost::shared_ptr<ut_detail::callback2_impl<R,T1,T2> > m_impl;
};

// ************************************************************************** //
// **************             unit_test::callback3             ************** //
// ************************************************************************** //

namespace ut_detail {

template<typename R, typename T1, typename T2, typename T3>
struct callback3_impl {
    virtual ~callback3_impl() {}

    virtual R invoke( T1 t1, T2 t2, T3 t3 ) = 0;
};

//____________________________________________________________________________//

template<typename R, typename T1, typename T2, typename T3, typename Functor>
struct callback3_impl_t : callback3_impl<R,T1,T2,T3> {
    // Constructor
    explicit callback3_impl_t( Functor f ) : m_f( f ) {}

    virtual R invoke( T1 t1, T2 t2, T3 t3 ) { return invoker<R>().invoke( m_f, t1, t2, t3 ); }

private:
    // Data members
    Functor m_f;
};

//____________________________________________________________________________//

} // namespace ut_detail

template<typename T1,typename T2, typename T3, typename R = ut_detail::unused>
class callback3 {
public:
    // Constructors
    callback3() {}
#ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
    callback3( callback3 const& rhs ) : m_impl( rhs.m_impl ) {}
#endif

    template<typename Functor>
    callback3( Functor f )
    : m_impl( new ut_detail::callback3_impl_t<R,T1,T2,T3,Functor>( f ) ) {}

    void        operator=( callback3 const& rhs ) { m_impl = rhs.m_impl; }

    template<typename Functor>
    void        operator=( Functor f ) { m_impl.reset( new ut_detail::callback3_impl_t<R,T1,T2,T3,Functor>( f ) );  }

    R           operator()( T1 t1, T2 t2, T3 t3 ) const { return m_impl->invoke( t1, t2, t3 ); }

    bool        operator!() const { return !m_impl; }

private:
    // Data members
    boost::shared_ptr<ut_detail::callback3_impl<R,T1,T2,T3> > m_impl;
};

} // namespace unit_test

} // namespace boost

#undef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR

//____________________________________________________________________________//

#include <boost/test/detail/enable_warnings.hpp>

#endif // BOOST_TEST_CALLBACK_020505GER