Geant4-11
G4AnyMethod.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4AnyMethod
27//
28// Class description:
29//
30// The G4AnyMethod class represents any object method.
31// The class only holds a member pointer.
32
33// See http://www.boost.org/libs/any for Documentation.
34// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
35//
36// Permission to use, copy, modify, and distribute this software for any
37// purpose is hereby granted without fee, provided that this copyright and
38// permissions notice appear in all copies and derivatives.
39//
40// This software is provided "as is" without express or implied warranty.
41// What: variant At boost::any
42// who: contributed by Kevlin Henney,
43// with features contributed and bugs found by
44// Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
45// when: July 2001
46// --------------------------------------------------------------------
47#ifndef G4AnyMethod_hh
48#define G4AnyMethod_hh 1
49
50#include <functional>
51
53class G4BadArgument : public std::bad_cast
54{
55 public:
57 virtual const char* what() const throw()
58 {
59 return "G4BadArgument: failed operator()";
60 }
61};
62
63#include <type_traits>
64using std::remove_const;
65using std::remove_reference;
66
68{
69 public:
70
74 {}
75
76 template <class S, class T>
77 G4AnyMethod(S (T::*f)())
78 {
79 fContent = new FuncRef<S, T>(f);
80 }
81
82 template <class S, class T, class A0>
83 G4AnyMethod(S (T::*f)(A0))
84 : narg(1)
85 {
87 }
88
89 template <class S, class T, class A0, class A1>
90 G4AnyMethod(S (T::*f)(A0, A1))
91 : narg(2)
92 {
94 }
95
97 : fContent(other.fContent ? other.fContent->Clone() : nullptr)
98 , narg(other.narg)
99 {}
100
103 ~G4AnyMethod() { delete fContent; }
104
106 {
107 std::swap(fContent, rhs.fContent);
108 std::swap(narg, rhs.narg);
109 return *this;
110 }
111
114 template <class S, class T>
116 {
117 G4AnyMethod(f).Swap(*this);
118 narg = 0;
119 return *this;
120 }
121
122 template <class S, class T, class A0>
123 G4AnyMethod& operator=(S (T::*f)(A0))
124 {
125 G4AnyMethod(f).Swap(*this);
126 narg = 1;
127 return *this;
128 }
129 template <class S, class T, class A0, class A1>
130 G4AnyMethod& operator=(S (T::*f)(A0, A1))
131 {
132 G4AnyMethod(f).Swap(*this);
133 narg = 1;
134 return *this;
135 }
136
138 {
139 G4AnyMethod(rhs).Swap(*this);
140 narg = rhs.narg;
141 return *this;
142 }
143
146 G4bool Empty() const { return !fContent; }
147
150 void operator()(void* obj) { fContent->operator()(obj); }
151 void operator()(void* obj, const std::string& a0)
152 {
153 fContent->operator()(obj, a0);
154 }
155
158 std::size_t NArg() const { return narg; }
159
160 const std::type_info& ArgType(size_t n = 0) const
161 {
162 return fContent ? fContent->ArgType(n) : typeid(void);
163 }
164
165 private:
166
168 {
169 public:
170
172 virtual ~Placeholder() {}
173 virtual Placeholder* Clone() const = 0;
174 virtual void operator()(void*) = 0;
175 virtual void operator()(void*, const std::string&) = 0;
176 virtual const std::type_info& ArgType(size_t) const = 0;
177 };
178
179 template <class S, class T>
180 struct FuncRef : public Placeholder
181 {
182 FuncRef(S (T::*f)())
183 : fRef(f)
184 {}
185
186 virtual void operator()(void* obj) { ((T*) obj->*fRef)(); }
187 virtual void operator()(void*, const std::string&)
188 {
189 throw G4BadArgument();
190 }
191 virtual Placeholder* Clone() const { return new FuncRef(fRef); }
192 virtual const std::type_info& ArgType(std::size_t) const
193 {
194 return typeid(void);
195 }
196 S (T::*fRef)();
197 };
198
199 template <class S, class T, class A0>
200 struct FuncRef1 : public Placeholder
201 {
202 typedef
203 typename remove_const<typename remove_reference<A0>::type>::type nakedA0;
204
205 FuncRef1(S (T::*f)(A0))
206 : fRef(f)
207 {}
208
209 virtual void operator()(void*) { throw G4BadArgument(); }
210 virtual void operator()(void* obj, const std::string& s0)
211 {
212 nakedA0 a0;
213 std::stringstream strs(s0);
214 strs >> a0;
215 ((T*) obj->*fRef)(a0);
216 }
217 virtual Placeholder* Clone() const { return new FuncRef1(fRef); }
218 virtual const std::type_info& ArgType(size_t) const { return typeid(A0); }
219 S (T::*fRef)(A0);
220 };
221
222 template <class S, class T, class A0, class A1>
223 struct FuncRef2 : public Placeholder
224 {
225 typedef
226 typename remove_const<typename remove_reference<A0>::type>::type nakedA0;
227 typedef
228 typename remove_const<typename remove_reference<A1>::type>::type nakedA1;
229
230 FuncRef2(S (T::*f)(A0, A1))
231 : fRef(f)
232 {}
233
234 virtual void operator()(void*) { throw G4BadArgument(); }
235 virtual void operator()(void* obj, const std::string& s0)
236 {
237 nakedA0 a0;
238 nakedA1 a1;
239 std::stringstream strs(s0);
240 strs >> a0 >> a1;
241 ((T*) obj->*fRef)(a0, a1);
242 }
243 virtual Placeholder* Clone() const { return new FuncRef2(fRef); }
244 virtual const std::type_info& ArgType(size_t i) const
245 {
246 return i == 0 ? typeid(A0) : typeid(A1);
247 }
248 S (T::*fRef)(A0, A1);
249 };
250
252 std::size_t narg = 0;
253};
254
255#endif
G4double S(G4double temp)
const G4double a0
bool G4bool
Definition: G4Types.hh:86
virtual void operator()(void *)=0
virtual Placeholder * Clone() const =0
virtual const std::type_info & ArgType(size_t) const =0
virtual void operator()(void *, const std::string &)=0
G4AnyMethod(S(T::*f)(A0, A1))
Definition: G4AnyMethod.hh:90
std::size_t NArg() const
Definition: G4AnyMethod.hh:158
Placeholder * fContent
Definition: G4AnyMethod.hh:251
G4AnyMethod & Swap(G4AnyMethod &rhs)
Definition: G4AnyMethod.hh:105
void operator()(void *obj)
Definition: G4AnyMethod.hh:150
const std::type_info & ArgType(size_t n=0) const
Definition: G4AnyMethod.hh:160
G4AnyMethod(S(T::*f)())
Definition: G4AnyMethod.hh:77
std::size_t narg
Definition: G4AnyMethod.hh:252
G4AnyMethod & operator=(S(T::*f)(A0, A1))
Definition: G4AnyMethod.hh:130
G4AnyMethod(S(T::*f)(A0))
Definition: G4AnyMethod.hh:83
G4AnyMethod(const G4AnyMethod &other)
Definition: G4AnyMethod.hh:96
G4AnyMethod & operator=(const G4AnyMethod &rhs)
Definition: G4AnyMethod.hh:137
G4AnyMethod & operator=(S(T::*f)(A0))
Definition: G4AnyMethod.hh:123
G4bool Empty() const
Definition: G4AnyMethod.hh:146
void operator()(void *obj, const std::string &a0)
Definition: G4AnyMethod.hh:151
G4AnyMethod & operator=(S(T::*f)())
Definition: G4AnyMethod.hh:115
virtual const char * what() const
Definition: G4AnyMethod.hh:57
virtual void operator()(void *obj, const std::string &s0)
Definition: G4AnyMethod.hh:210
remove_const< typenameremove_reference< A0 >::type >::type nakedA0
Definition: G4AnyMethod.hh:203
virtual const std::type_info & ArgType(size_t) const
Definition: G4AnyMethod.hh:218
virtual void operator()(void *)
Definition: G4AnyMethod.hh:209
virtual Placeholder * Clone() const
Definition: G4AnyMethod.hh:217
FuncRef1(S(T::*f)(A0))
Definition: G4AnyMethod.hh:205
virtual const std::type_info & ArgType(size_t i) const
Definition: G4AnyMethod.hh:244
virtual void operator()(void *obj, const std::string &s0)
Definition: G4AnyMethod.hh:235
remove_const< typenameremove_reference< A0 >::type >::type nakedA0
Definition: G4AnyMethod.hh:226
remove_const< typenameremove_reference< A1 >::type >::type nakedA1
Definition: G4AnyMethod.hh:228
virtual Placeholder * Clone() const
Definition: G4AnyMethod.hh:243
FuncRef2(S(T::*f)(A0, A1))
Definition: G4AnyMethod.hh:230
virtual void operator()(void *)
Definition: G4AnyMethod.hh:234
S(T::* fRef)(A0, A1)
Definition: G4AnyMethod.hh:248
virtual Placeholder * Clone() const
Definition: G4AnyMethod.hh:191
virtual void operator()(void *obj)
Definition: G4AnyMethod.hh:186
virtual const std::type_info & ArgType(std::size_t) const
Definition: G4AnyMethod.hh:192
virtual void operator()(void *, const std::string &)
Definition: G4AnyMethod.hh:187