• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0
4 // (see accompanying file LICENSE_1_0.txt or a copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // Home at http://www.boost.org/libs/local_function
7 
8 //[impl_tparam_tricks
9 #include <boost/detail/lightweight_test.hpp>
10 #include <vector>
11 #include <algorithm>
12 
13 // Casting functor trick.
14 struct casting_func {
casting_funccasting_func15     explicit casting_func(void* obj, void (*call)(void*, const int&))
16             : obj_(obj), call_(call) {}
17     // Unfortunately, function pointer call is not inlined.
operator ()casting_func18     inline void operator()(const int& num) { call_(obj_, num); }
19 private:
20     void* obj_;
21     void (*call_)(void*, const int&);
22 };
23 
24 // Virtual functor trick.
25 struct virtual_func {
26     struct interface {
27         // Unfortunately, virtual function call is not inlined.
operator ()virtual_func::interface28         inline virtual void operator()(const int&) {}
29     };
virtual_funcvirtual_func30     explicit virtual_func(interface& func): func_(&func) {}
operator ()virtual_func31     inline void operator()(const int& num) { (*func_)(num); }
32 private:
33     interface* func_;
34 };
35 
main(void)36 int main(void) {
37     int sum = 0, factor = 10;
38 
39     // Local class for local function.
40     struct local_add : virtual_func::interface {
41         explicit local_add(int& _sum, const int& _factor)
42                 : sum_(_sum), factor_(_factor) {}
43         inline void operator()(const int& num) {
44             body(sum_, factor_, num);
45         }
46         inline static void call(void* obj, const int& num) {
47             local_add* self = static_cast<local_add*>(obj);
48             self->body(self->sum_, self->factor_, num);
49         }
50     private:
51         int& sum_;
52         const int& factor_;
53         inline void body(int& sum, const int& factor, const int& num) {
54             sum += factor * num;
55         }
56     } add_local(sum, factor);
57     casting_func add_casting(&add_local, &local_add::call);
58     virtual_func add_virtual(add_local);
59 
60     std::vector<int> v(10);
61     std::fill(v.begin(), v.end(), 1);
62 
63     // std::for_each(v.begin(), v.end(), add_local); // Error but OK on C++11.
64     std::for_each(v.begin(), v.end(), add_casting); // OK.
65     std::for_each(v.begin(), v.end(), add_virtual); // OK.
66 
67     BOOST_TEST(sum == 200);
68     return boost::report_errors();
69 }
70 //]
71 
72