1 |
gbeauche |
1.1 |
/* |
2 |
|
|
* nvmemfun.hpp - Non-virtual member function wrappers |
3 |
|
|
* |
4 |
|
|
* Kheperix (C) 2003 Gwenole Beauchesne |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifndef NVMEMFUN_H |
22 |
|
|
#define NVMEMFUN_H |
23 |
|
|
|
24 |
|
|
#include <functional> |
25 |
|
|
|
26 |
|
|
#if (defined(__GNUC__) || defined(__ICC)) |
27 |
|
|
|
28 |
|
|
template< class PMF, class PF > |
29 |
|
|
inline PF nv_mem_fun_of(PMF pmf) { |
30 |
|
|
union { PMF pmf; uintptr p[2]; } x; |
31 |
|
|
x.pmf = pmf; |
32 |
|
|
#if defined(__GXX_ABI_VERSION) /* GCC >= 3.0 */ |
33 |
|
|
const int N = 0; |
34 |
|
|
#else |
35 |
|
|
const int N = 1; |
36 |
|
|
#endif |
37 |
|
|
return (PF)x.p[N]; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
template< class R, class T > |
41 |
|
|
class nv_mem_fun_t : public std::unary_function<T, R> { |
42 |
|
|
typedef R (T::*pmf_t)(); |
43 |
|
|
typedef R (*pf_t)(T *); |
44 |
|
|
pf_t pf; |
45 |
|
|
public: |
46 |
|
|
nv_mem_fun_t(pmf_t pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {} |
47 |
|
|
R operator()(T *p) const { return (*pf)(p); } |
48 |
|
|
operator bool () const { return pf; } |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
template< class R, class T > |
52 |
|
|
class const_nv_mem_fun_t : public std::unary_function<T, R> { |
53 |
|
|
typedef R (T::*pmf_t)(); |
54 |
|
|
typedef R (*pf_t)(T *); |
55 |
|
|
pf_t const pf; |
56 |
|
|
public: |
57 |
|
|
const_nv_mem_fun_t(pmf_t const pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {} |
58 |
|
|
R operator()(const T *p) const { return (*pf)(p); } |
59 |
|
|
operator bool () const { return pf; } |
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
template< class R, class T, class A > |
63 |
|
|
class nv_mem_fun1_t : public std::binary_function<T, A, R> { |
64 |
|
|
typedef R (T::*pmf_t)(A); |
65 |
|
|
typedef R (*pf_t)(T *, A x); |
66 |
|
|
pf_t pf; |
67 |
|
|
public: |
68 |
|
|
nv_mem_fun1_t(pmf_t pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {} |
69 |
|
|
R operator()(T *p, A x) const { return (*pf)(p, x); } |
70 |
|
|
operator bool () const { return pf; } |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
template< class R, class T, class A > |
74 |
|
|
class const_nv_mem_fun1_t : public std::binary_function<T, A, R> { |
75 |
|
|
typedef R (T::*pmf_t)(A); |
76 |
|
|
typedef R (*pf_t)(T *, A x); |
77 |
|
|
pf_t const pf; |
78 |
|
|
public: |
79 |
|
|
const_nv_mem_fun1_t(pmf_t const pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {} |
80 |
|
|
R operator()(const T *p, A x) const { return (*pf)(p, x); } |
81 |
|
|
operator bool () const { return pf; } |
82 |
|
|
}; |
83 |
|
|
|
84 |
|
|
#else |
85 |
|
|
|
86 |
|
|
template< class R, class T > |
87 |
|
|
class nv_mem_fun_t : public std::unary_function<T, R> { |
88 |
|
|
R (T::*pf)(); |
89 |
|
|
public: |
90 |
|
|
nv_mem_fun_t(R (T::*pmf)()) : pf(pmf) {} |
91 |
|
|
R operator()(T *p) const { return (p->*pf)(); } |
92 |
|
|
operator bool () const { return pf; } |
93 |
|
|
}; |
94 |
|
|
|
95 |
|
|
template< class R, class T > |
96 |
|
|
class const_nv_mem_fun_t : public std::unary_function<T, R> { |
97 |
|
|
R (T::*pf)() const; |
98 |
|
|
public: |
99 |
|
|
const_nv_mem_fun_t(R (T::*pmf)() const) : pf(pmf) {} |
100 |
|
|
R operator()(const T *p) const { return (p->*pf)(); } |
101 |
|
|
operator bool () const { return pf; } |
102 |
|
|
}; |
103 |
|
|
|
104 |
|
|
template< class R, class T, class A > |
105 |
|
|
class nv_mem_fun1_t : public std::binary_function<T, A, R> { |
106 |
|
|
R (T::*pf)(A); |
107 |
|
|
public: |
108 |
|
|
nv_mem_fun1_t(R (T::*pmf)(A)) : pf(pmf) {} |
109 |
|
|
R operator()(T *p, A x) const { return (p->*pf)(x); } |
110 |
|
|
operator bool () const { return pf; } |
111 |
|
|
}; |
112 |
|
|
|
113 |
|
|
template< class R, class T, class A > |
114 |
|
|
class const_nv_mem_fun1_t : public std::binary_function<T, A, R> { |
115 |
|
|
R (T::*pf)(A) const; |
116 |
|
|
public: |
117 |
|
|
const_nv_mem_fun1_t(R (T::*pmf)(A) const) : pf(pmf) {} |
118 |
|
|
R operator()(const T *p, A x) const { return (p->*pf)(x); } |
119 |
|
|
operator bool () const { return pf; } |
120 |
|
|
}; |
121 |
|
|
|
122 |
|
|
#endif |
123 |
|
|
|
124 |
|
|
template< class R, class T > |
125 |
|
|
inline nv_mem_fun_t<R, T> nv_mem_fun(R (T::*pmf)()) { |
126 |
|
|
return nv_mem_fun_t<R, T>(pmf); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
template< class R, class T > |
130 |
|
|
inline const_nv_mem_fun_t<R, T> nv_mem_fun(R (T::*pmf)() const) { |
131 |
|
|
return const_nv_mem_fun_t<R, T>(pmf); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
template< class R, class T, class A > |
135 |
|
|
inline nv_mem_fun1_t<R, T, A> nv_mem_fun(R (T::*pmf)(A)) { |
136 |
|
|
return nv_mem_fun1_t<R, T, A>(pmf); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
template< class R, class T, class A > |
140 |
|
|
inline const_nv_mem_fun1_t<R, T, A> nv_mem_fun(R (T::*pmf)(A) const) { |
141 |
|
|
return const_nv_mem_fun1_t<R, T, A>(pmf); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
#endif /* NVMEMFUN_H */ |