callback.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  2. // Use, modification, and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 49312 $
  10. //
  11. // Description :
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_CALLBACK_020505GER
  14. #define BOOST_TEST_CALLBACK_020505GER
  15. // Boost
  16. #include <boost/config.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <boost/test/detail/suppress_warnings.hpp>
  20. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) || BOOST_WORKAROUND(BOOST_INTEL, <= 700)
  21. # define BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
  22. #endif
  23. //____________________________________________________________________________//
  24. namespace boost {
  25. namespace unit_test {
  26. namespace ut_detail {
  27. struct unused {};
  28. template<typename R>
  29. struct invoker {
  30. template<typename Functor>
  31. R invoke( Functor& f ) { return f(); }
  32. template<typename Functor, typename T1>
  33. R invoke( Functor& f, T1 t1 ) { return f( t1 ); }
  34. template<typename Functor, typename T1, typename T2>
  35. R invoke( Functor& f, T1 t1, T2 t2 ) { return f( t1, t2 ); }
  36. template<typename Functor, typename T1, typename T2, typename T3>
  37. R invoke( Functor& f, T1 t1, T2 t2, T3 t3 ) { return f( t1, t2, t3 ); }
  38. };
  39. //____________________________________________________________________________//
  40. template<>
  41. struct invoker<unused> {
  42. template<typename Functor>
  43. unused invoke( Functor& f ) { f(); return unused(); }
  44. template<typename Functor, typename T1>
  45. unused invoke( Functor& f, T1 t1 ) { f( t1 ); return unused(); }
  46. template<typename Functor, typename T1, typename T2>
  47. unused invoke( Functor& f, T1 t1, T2 t2 ) { f( t1, t2 ); return unused(); }
  48. template<typename Functor, typename T1, typename T2, typename T3>
  49. unused invoke( Functor& f, T1 t1, T2 t2, T3 t3 ) { f( t1, t2, t3 ); return unused(); }
  50. };
  51. //____________________________________________________________________________//
  52. } // namespace ut_detail
  53. // ************************************************************************** //
  54. // ************** unit_test::callback0 ************** //
  55. // ************************************************************************** //
  56. namespace ut_detail {
  57. template<typename R>
  58. struct callback0_impl {
  59. virtual ~callback0_impl() {}
  60. virtual R invoke() = 0;
  61. };
  62. //____________________________________________________________________________//
  63. template<typename R, typename Functor>
  64. struct callback0_impl_t : callback0_impl<R> {
  65. // Constructor
  66. explicit callback0_impl_t( Functor f ) : m_f( f ) {}
  67. virtual R invoke() { return invoker<R>().invoke( m_f ); }
  68. private:
  69. // Data members
  70. Functor m_f;
  71. };
  72. //____________________________________________________________________________//
  73. } // namespace ut_detail
  74. template<typename R = ut_detail::unused>
  75. class callback0 {
  76. public:
  77. // Constructors
  78. callback0() {}
  79. #ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
  80. callback0( callback0 const& rhs ) : m_impl( rhs.m_impl ) {}
  81. #endif
  82. template<typename Functor>
  83. callback0( Functor f )
  84. : m_impl( new ut_detail::callback0_impl_t<R,Functor>( f ) ) {}
  85. void operator=( callback0 const& rhs ) { m_impl = rhs.m_impl; }
  86. template<typename Functor>
  87. void operator=( Functor f ) { m_impl.reset( new ut_detail::callback0_impl_t<R,Functor>( f ) ); }
  88. R operator()() const { return m_impl->invoke(); }
  89. bool operator!() const { return !m_impl; }
  90. private:
  91. // Data members
  92. boost::shared_ptr<ut_detail::callback0_impl<R> > m_impl;
  93. };
  94. // ************************************************************************** //
  95. // ************** unit_test::callback1 ************** //
  96. // ************************************************************************** //
  97. namespace ut_detail {
  98. template<typename R, typename T1>
  99. struct callback1_impl {
  100. virtual ~callback1_impl() {}
  101. virtual R invoke( T1 t1 ) = 0;
  102. };
  103. //____________________________________________________________________________//
  104. template<typename R, typename T1,typename Functor>
  105. struct callback1_impl_t : callback1_impl<R,T1> {
  106. // Constructor
  107. explicit callback1_impl_t( Functor f ) : m_f( f ) {}
  108. virtual R invoke( T1 t1 ) { return invoker<R>().invoke( m_f, t1 ); }
  109. private:
  110. // Data members
  111. Functor m_f;
  112. };
  113. //____________________________________________________________________________//
  114. } // namespace ut_detail
  115. template<typename T1,typename R = ut_detail::unused>
  116. class callback1 {
  117. public:
  118. // Constructors
  119. callback1() {}
  120. #ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
  121. callback1( callback1 const& rhs ) : m_impl( rhs.m_impl ) {}
  122. #endif
  123. template<typename Functor>
  124. callback1( Functor f )
  125. : m_impl( new ut_detail::callback1_impl_t<R,T1,Functor>( f ) ) {}
  126. void operator=( callback1 const& rhs ) { m_impl = rhs.m_impl; }
  127. template<typename Functor>
  128. void operator=( Functor f ) { m_impl.reset( new ut_detail::callback1_impl_t<R,T1,Functor>( f ) ); }
  129. R operator()( T1 t1 ) const { return m_impl->invoke( t1 ); }
  130. bool operator!() const { return !m_impl; }
  131. private:
  132. // Data members
  133. boost::shared_ptr<ut_detail::callback1_impl<R,T1> > m_impl;
  134. };
  135. // ************************************************************************** //
  136. // ************** unit_test::callback2 ************** //
  137. // ************************************************************************** //
  138. namespace ut_detail {
  139. template<typename R, typename T1,typename T2>
  140. struct callback2_impl {
  141. virtual ~callback2_impl() {}
  142. virtual R invoke( T1 t1, T2 t2 ) = 0;
  143. };
  144. //____________________________________________________________________________//
  145. template<typename R, typename T1, typename T2, typename Functor>
  146. struct callback2_impl_t : callback2_impl<R,T1,T2> {
  147. // Constructor
  148. explicit callback2_impl_t( Functor f ) : m_f( f ) {}
  149. virtual R invoke( T1 t1, T2 t2 ) { return invoker<R>().template invoke<Functor,T1,T2>( m_f, t1, t2 ); }
  150. private:
  151. // Data members
  152. Functor m_f;
  153. };
  154. //____________________________________________________________________________//
  155. } // namespace ut_detail
  156. template<typename T1,typename T2, typename R = ut_detail::unused>
  157. class callback2 {
  158. public:
  159. // Constructors
  160. callback2() {}
  161. #ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
  162. callback2( callback2 const& rhs ) : m_impl( rhs.m_impl ) {}
  163. #endif
  164. template<typename Functor>
  165. callback2( Functor f ) : m_impl( new ut_detail::callback2_impl_t<R,T1,T2,Functor>( f ) ) {}
  166. void operator=( callback2 const& rhs ) { m_impl = rhs.m_impl; }
  167. template<typename Functor>
  168. void operator=( Functor f ) { m_impl.reset( new ut_detail::callback2_impl_t<R,T1,T2,Functor>( f ) ); }
  169. R operator()( T1 t1, T2 t2 ) const { return m_impl->invoke( t1, t2 ); }
  170. bool operator!() const { return !m_impl; }
  171. private:
  172. // Data members
  173. boost::shared_ptr<ut_detail::callback2_impl<R,T1,T2> > m_impl;
  174. };
  175. // ************************************************************************** //
  176. // ************** unit_test::callback3 ************** //
  177. // ************************************************************************** //
  178. namespace ut_detail {
  179. template<typename R, typename T1, typename T2, typename T3>
  180. struct callback3_impl {
  181. virtual ~callback3_impl() {}
  182. virtual R invoke( T1 t1, T2 t2, T3 t3 ) = 0;
  183. };
  184. //____________________________________________________________________________//
  185. template<typename R, typename T1, typename T2, typename T3, typename Functor>
  186. struct callback3_impl_t : callback3_impl<R,T1,T2,T3> {
  187. // Constructor
  188. explicit callback3_impl_t( Functor f ) : m_f( f ) {}
  189. virtual R invoke( T1 t1, T2 t2, T3 t3 ) { return invoker<R>().invoke( m_f, t1, t2, t3 ); }
  190. private:
  191. // Data members
  192. Functor m_f;
  193. };
  194. //____________________________________________________________________________//
  195. } // namespace ut_detail
  196. template<typename T1,typename T2, typename T3, typename R = ut_detail::unused>
  197. class callback3 {
  198. public:
  199. // Constructors
  200. callback3() {}
  201. #ifdef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
  202. callback3( callback3 const& rhs ) : m_impl( rhs.m_impl ) {}
  203. #endif
  204. template<typename Functor>
  205. callback3( Functor f )
  206. : m_impl( new ut_detail::callback3_impl_t<R,T1,T2,T3,Functor>( f ) ) {}
  207. void operator=( callback3 const& rhs ) { m_impl = rhs.m_impl; }
  208. template<typename Functor>
  209. void operator=( Functor f ) { m_impl.reset( new ut_detail::callback3_impl_t<R,T1,T2,T3,Functor>( f ) ); }
  210. R operator()( T1 t1, T2 t2, T3 t3 ) const { return m_impl->invoke( t1, t2, t3 ); }
  211. bool operator!() const { return !m_impl; }
  212. private:
  213. // Data members
  214. boost::shared_ptr<ut_detail::callback3_impl<R,T1,T2,T3> > m_impl;
  215. };
  216. } // namespace unit_test
  217. } // namespace boost
  218. #undef BOOST_CALLBACK_EXPLICIT_COPY_CONSTRUCTOR
  219. //____________________________________________________________________________//
  220. #include <boost/test/detail/enable_warnings.hpp>
  221. #endif // BOOST_TEST_CALLBACK_020505GER