ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/include/nvmemfun.hpp
Revision: 1.4
Committed: 2005-06-22T12:25:43Z (19 years, 3 months ago) by gbeauche
Branch: MAIN
Changes since 1.3: +2 -2 lines
Log Message:
pointer-to-member-functions fixlets, NULL may be (void *)0 on ancient
compilers which is not suitable here.

File Contents

# User Rev Content
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 gbeauche 1.4 if (pmf == 0)
31     return 0;
32 gbeauche 1.1 union { PMF pmf; uintptr p[2]; } x;
33     x.pmf = pmf;
34     #if defined(__GXX_ABI_VERSION) /* GCC >= 3.0 */
35     const int N = 0;
36     #else
37     const int N = 1;
38     #endif
39     return (PF)x.p[N];
40     }
41    
42     template< class R, class T >
43     class nv_mem_fun_t : public std::unary_function<T, R> {
44     typedef R (T::*pmf_t)();
45     typedef R (*pf_t)(T *);
46     pf_t pf;
47     public:
48     nv_mem_fun_t(pmf_t pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {}
49     R operator()(T *p) const { return (*pf)(p); }
50 gbeauche 1.2 pf_t ptr() const { return pf; }
51 gbeauche 1.1 };
52    
53     template< class R, class T >
54     class const_nv_mem_fun_t : public std::unary_function<T, R> {
55     typedef R (T::*pmf_t)();
56     typedef R (*pf_t)(T *);
57     pf_t const pf;
58     public:
59     const_nv_mem_fun_t(pmf_t const pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {}
60     R operator()(const T *p) const { return (*pf)(p); }
61 gbeauche 1.2 pf_t ptr() const { return pf; }
62 gbeauche 1.1 };
63    
64     template< class R, class T, class A >
65     class nv_mem_fun1_t : public std::binary_function<T, A, R> {
66     typedef R (T::*pmf_t)(A);
67     typedef R (*pf_t)(T *, A x);
68     pf_t pf;
69     public:
70     nv_mem_fun1_t(pmf_t pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {}
71     R operator()(T *p, A x) const { return (*pf)(p, x); }
72 gbeauche 1.2 pf_t ptr() const { return pf; }
73 gbeauche 1.1 };
74    
75     template< class R, class T, class A >
76     class const_nv_mem_fun1_t : public std::binary_function<T, A, R> {
77     typedef R (T::*pmf_t)(A);
78     typedef R (*pf_t)(T *, A x);
79     pf_t const pf;
80     public:
81     const_nv_mem_fun1_t(pmf_t const pmf) : pf(nv_mem_fun_of<pmf_t, pf_t>(pmf)) {}
82     R operator()(const T *p, A x) const { return (*pf)(p, x); }
83 gbeauche 1.2 pf_t ptr() const { return pf; }
84 gbeauche 1.1 };
85    
86     #else
87    
88     template< class R, class T >
89     class nv_mem_fun_t : public std::unary_function<T, R> {
90 gbeauche 1.2 typedef R (T::*pmf_t)();
91     pmf_t pf;
92 gbeauche 1.1 public:
93     nv_mem_fun_t(R (T::*pmf)()) : pf(pmf) {}
94     R operator()(T *p) const { return (p->*pf)(); }
95 gbeauche 1.2 pmf_t ptr() const { return pf; }
96 gbeauche 1.1 };
97    
98     template< class R, class T >
99     class const_nv_mem_fun_t : public std::unary_function<T, R> {
100 gbeauche 1.2 typedef R (T::*pmf_t)() const;
101     pmf_t pf;
102 gbeauche 1.1 public:
103     const_nv_mem_fun_t(R (T::*pmf)() const) : pf(pmf) {}
104     R operator()(const T *p) const { return (p->*pf)(); }
105 gbeauche 1.2 pmf_t ptr() const { return pf; }
106 gbeauche 1.1 };
107    
108     template< class R, class T, class A >
109     class nv_mem_fun1_t : public std::binary_function<T, A, R> {
110 gbeauche 1.2 typedef R (T::*pmf_t)(A);
111     pmf_t pf;
112 gbeauche 1.1 public:
113     nv_mem_fun1_t(R (T::*pmf)(A)) : pf(pmf) {}
114     R operator()(T *p, A x) const { return (p->*pf)(x); }
115 gbeauche 1.2 pmf_t ptr() const { return pf; }
116 gbeauche 1.1 };
117    
118     template< class R, class T, class A >
119     class const_nv_mem_fun1_t : public std::binary_function<T, A, R> {
120 gbeauche 1.2 typedef R (T::*pmf_t)(A) const;
121     pmf_t pf;
122 gbeauche 1.1 public:
123     const_nv_mem_fun1_t(R (T::*pmf)(A) const) : pf(pmf) {}
124     R operator()(const T *p, A x) const { return (p->*pf)(x); }
125 gbeauche 1.2 pmf_t ptr() const { return pf; }
126 gbeauche 1.1 };
127    
128     #endif
129    
130     template< class R, class T >
131     inline nv_mem_fun_t<R, T> nv_mem_fun(R (T::*pmf)()) {
132     return nv_mem_fun_t<R, T>(pmf);
133     }
134    
135     template< class R, class T >
136     inline const_nv_mem_fun_t<R, T> nv_mem_fun(R (T::*pmf)() const) {
137     return const_nv_mem_fun_t<R, T>(pmf);
138     }
139    
140     template< class R, class T, class A >
141     inline nv_mem_fun1_t<R, T, A> nv_mem_fun(R (T::*pmf)(A)) {
142     return nv_mem_fun1_t<R, T, A>(pmf);
143     }
144    
145     template< class R, class T, class A >
146     inline const_nv_mem_fun1_t<R, T, A> nv_mem_fun(R (T::*pmf)(A) const) {
147     return const_nv_mem_fun1_t<R, T, A>(pmf);
148     }
149    
150     #endif /* NVMEMFUN_H */