1 |
/* support.hh |
2 |
* This file is part of ndk library |
3 |
* Copyright (c) 2003,2004 by Stanislav Ievlev |
4 |
* |
5 |
* This file is covered by the GNU Library General Public License, |
6 |
* which should be included with libndk as the file COPYING. |
7 |
*/ |
8 |
#pragma once |
9 |
|
10 |
#include <ndk/support.hh> |
11 |
|
12 |
#include <memory> |
13 |
#include <stdexcept> |
14 |
#include <string> |
15 |
|
16 |
namespace ndk |
17 |
{ |
18 |
template<class T> |
19 |
class singleton_holder |
20 |
{ |
21 |
/**< simple Meyers' style singleton */ |
22 |
public: |
23 |
typedef T obj_type; |
24 |
static T & |
25 |
instance () /**< @return instance of the object */ |
26 |
{ |
27 |
static T obj; |
28 |
|
29 |
return obj; |
30 |
} |
31 |
|
32 |
private: |
33 |
/* all this operations are unavailable to ordinal users */ |
34 |
singleton_holder (); |
35 |
singleton_holder (const singleton_holder &s); |
36 |
singleton_holder &operator = (const singleton_holder &s); |
37 |
~singleton_holder (); |
38 |
}; |
39 |
|
40 |
|
41 |
struct NoArg { }; |
42 |
|
43 |
template<typename RetT, typename ArgT> |
44 |
struct functor_impl |
45 |
{ |
46 |
// general polymorphic functor interface |
47 |
virtual ~functor_impl () |
48 |
{ |
49 |
} |
50 |
|
51 |
virtual RetT operator () (ArgT) = 0; |
52 |
}; |
53 |
|
54 |
template<typename RetT> |
55 |
struct functor_impl<RetT, NoArg> |
56 |
{ |
57 |
// general polymorphic functor interface |
58 |
virtual ~functor_impl () |
59 |
{ |
60 |
} |
61 |
|
62 |
virtual RetT operator () () = 0; |
63 |
}; |
64 |
|
65 |
template<typename FunctorT, typename RetT, typename ArgT> |
66 |
struct functor_impl_arb |
67 |
: public functor_impl<RetT, ArgT> |
68 |
{ |
69 |
// special interface version for arbitrary functor |
70 |
functor_impl_arb (const FunctorT &fun) |
71 |
: fun_ (fun) |
72 |
{ |
73 |
} |
74 |
|
75 |
RetT operator () (ArgT arg) |
76 |
{ |
77 |
return fun_ (arg); |
78 |
} |
79 |
|
80 |
FunctorT fun_; // internal copy of the functor |
81 |
}; |
82 |
|
83 |
template<typename FunctorT, typename RetT> |
84 |
struct functor_impl_arb<FunctorT, RetT, NoArg> |
85 |
: public functor_impl<RetT, NoArg> |
86 |
{ |
87 |
// special interface version for arbitrary functor |
88 |
functor_impl_arb (const FunctorT &fun) |
89 |
: fun_ (fun) |
90 |
{ |
91 |
} |
92 |
|
93 |
RetT operator () () |
94 |
{ |
95 |
return fun_ (); |
96 |
} |
97 |
|
98 |
FunctorT fun_; // internal copy of the functor |
99 |
}; |
100 |
|
101 |
template<typename RetT, typename ArgT = NoArg> |
102 |
struct functor |
103 |
{ |
104 |
// generalized functor for ndk callbacks |
105 |
typedef functor_impl<RetT, ArgT> impl_type; |
106 |
typedef std::shared_ptr<impl_type> invoker_type; |
107 |
|
108 |
template<typename FunctorT> |
109 |
functor (const FunctorT &fun) |
110 |
// initialize with functor type |
111 |
: invoker_ (new functor_impl_arb<FunctorT, RetT, ArgT> (fun)) |
112 |
{ |
113 |
} |
114 |
|
115 |
RetT operator () (ArgT arg) |
116 |
{ |
117 |
// run action |
118 |
if (invoker_.get ()) |
119 |
return (*invoker_.get ()) (arg); |
120 |
throw std::runtime_error ("calling empty functor"); |
121 |
} |
122 |
|
123 |
template<typename FunctorT> |
124 |
functor<RetT, ArgT> & |
125 |
operator = (const FunctorT fun) |
126 |
{ |
127 |
invoker_ = new functor_impl_arb<FunctorT, RetT, ArgT> (fun); |
128 |
return *this; |
129 |
} |
130 |
|
131 |
invoker_type invoker_; |
132 |
}; |
133 |
|
134 |
template<typename RetT> |
135 |
struct functor<RetT, NoArg> |
136 |
{ |
137 |
// generalized functor for ndk callbacks |
138 |
typedef functor_impl<RetT, NoArg> impl_type; |
139 |
typedef std::shared_ptr<impl_type> invoker_type; |
140 |
|
141 |
template<typename FunctorT> |
142 |
functor (const FunctorT &fun) |
143 |
// initialize with functor type |
144 |
: invoker_ (new functor_impl_arb<FunctorT, RetT, NoArg> (fun)) |
145 |
{ |
146 |
} |
147 |
|
148 |
RetT operator () () |
149 |
{ |
150 |
// run action |
151 |
if (invoker_.get ()) |
152 |
return (*invoker_.get ())(); throw std::runtime_error ("calling empty functor"); |
153 |
} |
154 |
|
155 |
template<typename FunctorT> |
156 |
functor<RetT, NoArg> & |
157 |
operator = (const FunctorT fun) |
158 |
{ |
159 |
invoker_ = new functor_impl_arb<FunctorT, RetT, NoArg> (fun); |
160 |
return *this; |
161 |
} |
162 |
|
163 |
invoker_type invoker_; |
164 |
}; |
165 |
} |