Move libcds 1.6.0 from SVN
[libcds.git] / cds / details / functor_wrapper.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_DETAILS_FUNCTOR_WRAPPER_H
4 #define __CDS_DETAILS_FUNCTOR_WRAPPER_H
5
6 #include <cds/ref.h>
7
8 //@cond
9 namespace cds { namespace details {
10
11     template <typename Functor>
12     struct functor_wrapper
13     {
14     public:
15         functor_wrapper()
16         {}
17
18         functor_wrapper( Functor /*f*/)
19         {}
20
21         Functor get()
22         {
23             return Functor();
24         }
25     };
26
27     template <typename Functor>
28     struct functor_wrapper<Functor&>
29     {
30         Functor&    m_func;
31     public:
32         functor_wrapper( Functor& f)
33         : m_func(f)
34         {}
35
36         Functor& get()
37         {
38             return m_func;
39         }
40     };
41
42     template <typename Functor>
43     struct functor_wrapper< boost::reference_wrapper<Functor> >
44     {
45         boost::reference_wrapper<Functor>    m_func;
46     public:
47         functor_wrapper( boost::reference_wrapper<Functor> f)
48         : m_func(f)
49         {}
50
51         Functor& get()
52         {
53             return m_func.get();
54         }
55     };
56
57 #ifdef CDS_CXX11_VARIADIC_TEMPLATE_SUPPORT
58     template <typename Result, typename... Args>
59     struct functor_wrapper<Result (*)(Args...)>
60     {
61         typedef Result (* func_ptr)(Args...);
62         typedef Result (& func_ref)(Args...);
63         func_ptr m_func;
64     public:
65         functor_wrapper( func_ptr f )
66             : m_func(f)
67         {}
68
69         func_ref get()
70         {
71             assert( m_func != NULL );
72             return *m_func;
73         }
74     };
75 #else
76     template <typename Result>
77     struct functor_wrapper<Result (*)()>
78     {
79         typedef Result (* func_ptr)();
80         typedef Result (& func_ref)();
81         func_ptr m_func;
82     public:
83         functor_wrapper( func_ptr f )
84             : m_func(f)
85         {}
86
87         func_ref get()
88         {
89             assert( m_func != NULL );
90             return *m_func;
91         }
92     };
93
94     template <typename Result, typename Arg1>
95     struct functor_wrapper<Result (*)(Arg1)>
96     {
97         typedef Result (* func_ptr)(Arg1);
98         typedef Result (& func_ref)(Arg1);
99         func_ptr m_func;
100     public:
101         functor_wrapper( func_ptr f )
102             : m_func(f)
103         {}
104
105         func_ref get()
106         {
107             assert( m_func != NULL );
108             return *m_func;
109         }
110     };
111
112     template <typename Result, typename Arg1, typename Arg2>
113     struct functor_wrapper<Result (*)(Arg1, Arg2)>
114     {
115         typedef Result (* func_ptr)(Arg1, Arg2);
116         typedef Result (& func_ref)(Arg1, Arg2);
117         func_ptr m_func;
118     public:
119         functor_wrapper( func_ptr f )
120             : m_func(f)
121         {}
122
123         func_ref get()
124         {
125             assert( m_func != NULL );
126             return *m_func;
127         }
128     };
129
130     template <typename Result, typename Arg1, typename Arg2, typename Arg3>
131     struct functor_wrapper<Result (*)(Arg1, Arg2, Arg3)>
132     {
133         typedef Result (* func_ptr)(Arg1, Arg2, Arg3);
134         typedef Result (& func_ref)(Arg1, Arg2, Arg3);
135         func_ptr m_func;
136     public:
137         functor_wrapper( func_ptr f )
138             : m_func(f)
139         {}
140
141         func_ref get()
142         {
143             assert( m_func != NULL );
144             return *m_func;
145         }
146     };
147
148 #endif
149 }}  // namespace cds::details
150 //@endcond
151
152 #endif // #ifndef __CDS_DETAILS_FUNCTOR_WRAPPER_H