Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
52/** Bad Argument exception */
53class G4BadArgument : public std::bad_cast
54{
55 public:
56 G4BadArgument() = default;
57 const char* what() const throw() override
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
71 /** contructors */
72
73 G4AnyMethod() = default;
74
75 template <class S, class T>
76 G4AnyMethod(S (T::*f)())
77 {
78 fContent = new FuncRef<S, T>(f);
79 }
80
81 template <class S, class T, class A0>
82 G4AnyMethod(S (T::*f)(A0))
83 : narg(1)
84 {
85 fContent = new FuncRef1<S, T, A0>(f);
86 }
87
88 template <class S, class T, class A0, class A1>
89 G4AnyMethod(S (T::*f)(A0, A1))
90 : narg(2)
91 {
92 fContent = new FuncRef2<S, T, A0, A1>(f);
93 }
94
96 : fContent(other.fContent != nullptr ? other.fContent->Clone() : nullptr)
97 , narg(other.narg)
98 {}
99
100 /** destructor */
101
102 ~G4AnyMethod() { delete fContent; }
103
105 {
106 std::swap(fContent, rhs.fContent);
107 std::swap(narg, rhs.narg);
108 return *this;
109 }
110
111 /** Assignment operators */
112
113 template <class S, class T>
115 {
116 G4AnyMethod(f).Swap(*this);
117 narg = 0;
118 return *this;
119 }
120
121 template <class S, class T, class A0>
122 G4AnyMethod& operator=(S (T::*f)(A0))
123 {
124 G4AnyMethod(f).Swap(*this);
125 narg = 1;
126 return *this;
127 }
128 template <class S, class T, class A0, class A1>
129 G4AnyMethod& operator=(S (T::*f)(A0, A1))
130 {
131 G4AnyMethod(f).Swap(*this);
132 narg = 1;
133 return *this;
134 }
135
137 {
138 G4AnyMethod(rhs).Swap(*this);
139 narg = rhs.narg;
140 return *this;
141 }
142
143 /** Query */
144
145 G4bool Empty() const { return fContent == nullptr; }
146
147 /** call operators */
148
149 void operator()(void* obj) { fContent->operator()(obj); }
150 void operator()(void* obj, const std::string& a0)
151 {
152 fContent->operator()(obj, a0);
153 }
154
155 /** Number of arguments */
156
157 std::size_t NArg() const { return narg; }
158
159 const std::type_info& ArgType(size_t n = 0) const
160 {
161 return fContent != nullptr ? fContent->ArgType(n) : typeid(void);
162 }
163
164 private:
165
166 class Placeholder
167 {
168 public:
169 Placeholder() = default;
170 virtual ~Placeholder() = default;
171 virtual Placeholder* Clone() const = 0;
172 virtual void operator()(void*) = 0;
173 virtual void operator()(void*, const std::string&) = 0;
174 virtual const std::type_info& ArgType(size_t) const = 0;
175 };
176
177 template <class S, class T>
178 struct FuncRef : public Placeholder
179 {
180 FuncRef(S (T::*f)())
181 : fRef(f)
182 {}
183
184 void operator()(void* obj) override { ((T*) obj->*fRef)(); }
185 void operator()(void*, const std::string&) override
186 {
187 throw G4BadArgument();
188 }
189 Placeholder* Clone() const override { return new FuncRef(fRef); }
190 const std::type_info& ArgType(std::size_t) const override
191 {
192 return typeid(void);
193 }
194 S (T::*fRef)();
195 };
196
197 template <class S, class T, class A0>
198 struct FuncRef1 : public Placeholder
199 {
200 using nakedA0 =
201 typename remove_const<typename remove_reference<A0>::type>::type;
202
203 FuncRef1(S (T::*f)(A0))
204 : fRef(f)
205 {}
206
207 void operator()(void*) override { throw G4BadArgument(); }
208 void operator()(void* obj, const std::string& s0) override
209 {
210 nakedA0 a0;
211 std::stringstream strs(s0);
212 strs >> a0;
213 ((T*) obj->*fRef)(a0);
214 }
215 Placeholder* Clone() const override { return new FuncRef1(fRef); }
216 const std::type_info& ArgType(size_t) const override
217 {
218 return typeid(A0);
219 }
220 S (T::*fRef)(A0);
221 };
222
223 template <class S, class T, class A0, class A1>
224 struct FuncRef2 : public Placeholder
225 {
226 using nakedA0 =
227 typename remove_const<typename remove_reference<A0>::type>::type;
228 using nakedA1 =
229 typename remove_const<typename remove_reference<A1>::type>::type;
230
231 FuncRef2(S (T::*f)(A0, A1))
232 : fRef(f)
233 {}
234
235 void operator()(void*) override { throw G4BadArgument(); }
236 void operator()(void* obj, const std::string& s0) override
237 {
238 nakedA0 a0;
239 nakedA1 a1;
240 std::stringstream strs(s0);
241 strs >> a0 >> a1;
242 ((T*) obj->*fRef)(a0, a1);
243 }
244 Placeholder* Clone() const override { return new FuncRef2(fRef); }
245 const std::type_info& ArgType(size_t i) const override
246 {
247 return i == 0 ? typeid(A0) : typeid(A1);
248 }
249 S (T::*fRef)(A0, A1);
250 };
251
252 Placeholder* fContent = nullptr;
253 std::size_t narg = 0;
254};
255
256#endif
G4double S(G4double temp)
const G4double a0
bool G4bool
Definition: G4Types.hh:86
G4AnyMethod(S(T::*f)(A0, A1))
Definition: G4AnyMethod.hh:89
std::size_t NArg() const
Definition: G4AnyMethod.hh:157
G4AnyMethod & Swap(G4AnyMethod &rhs)
Definition: G4AnyMethod.hh:104
G4AnyMethod()=default
void operator()(void *obj)
Definition: G4AnyMethod.hh:149
const std::type_info & ArgType(size_t n=0) const
Definition: G4AnyMethod.hh:159
G4AnyMethod(S(T::*f)())
Definition: G4AnyMethod.hh:76
G4AnyMethod & operator=(S(T::*f)(A0, A1))
Definition: G4AnyMethod.hh:129
G4AnyMethod(S(T::*f)(A0))
Definition: G4AnyMethod.hh:82
G4AnyMethod(const G4AnyMethod &other)
Definition: G4AnyMethod.hh:95
G4AnyMethod & operator=(const G4AnyMethod &rhs)
Definition: G4AnyMethod.hh:136
G4AnyMethod & operator=(S(T::*f)(A0))
Definition: G4AnyMethod.hh:122
G4bool Empty() const
Definition: G4AnyMethod.hh:145
void operator()(void *obj, const std::string &a0)
Definition: G4AnyMethod.hh:150
G4AnyMethod & operator=(S(T::*f)())
Definition: G4AnyMethod.hh:114
const char * what() const override
Definition: G4AnyMethod.hh:57
G4BadArgument()=default