48#define G4AnyMethod_hh 1
61 const char*
what()
const throw()
override {
return "G4BadArgument: failed operator()"; }
69 template<
class S,
class T>
72 fContent =
new FuncRef<S, T>(f);
75 template<
class S,
class T,
class A0>
78 fContent =
new FuncRef1<S, T, A0>(f);
81 template<
class S,
class T,
class A0,
class A1>
84 fContent =
new FuncRef2<S, T, A0, A1>(f);
88 : fContent(other.fContent != nullptr ? other.fContent->Clone() : nullptr), narg(other.narg)
95 std::swap(fContent, rhs.fContent);
96 std::swap(narg, rhs.narg);
100 template<
class S,
class T>
108 template<
class S,
class T,
class A0>
115 template<
class S,
class T,
class A0,
class A1>
137 void operator()(
void* obj,
const std::string&
a0) { fContent->operator()(obj,
a0); }
141 std::size_t
NArg()
const {
return narg; }
143 const std::type_info&
ArgType(
size_t n = 0)
const
145 return fContent !=
nullptr ? fContent->ArgType(n) :
typeid(void);
152 Placeholder() =
default;
153 virtual ~Placeholder() =
default;
154 virtual Placeholder* Clone()
const = 0;
155 virtual void operator()(
void*) = 0;
156 virtual void operator()(
void*,
const std::string&) = 0;
157 virtual const std::type_info& ArgType(
size_t)
const = 0;
160 template<
class S,
class T>
161 struct FuncRef :
public Placeholder
163 FuncRef(
S (T::*f)()) : fRef(f) {}
165 void operator()(
void* obj)
override { ((T*)obj->*fRef)(); }
166 void operator()(
void*,
const std::string&)
override {
throw G4BadArgument(); }
167 Placeholder* Clone()
const override {
return new FuncRef(fRef); }
168 const std::type_info& ArgType(std::size_t)
const override {
return typeid(void); }
172 template<
class S,
class T,
class A0>
173 struct FuncRef1 :
public Placeholder
175 using nakedA0 = std::remove_const_t<std::remove_reference_t<A0>>;
177 FuncRef1(
S (T::*f)(A0)) : fRef(f) {}
180 void operator()(
void* obj,
const std::string& s0)
override
183 std::stringstream strs(s0);
185 ((T*)obj->*fRef)(
a0);
187 Placeholder* Clone()
const override {
return new FuncRef1(fRef); }
188 const std::type_info& ArgType(
size_t)
const override {
return typeid(A0); }
192 template<
class S,
class T,
class A0,
class A1>
193 struct FuncRef2 :
public Placeholder
195 using nakedA0 = std::remove_const_t<std::remove_reference_t<A0>>;
196 using nakedA1 = std::remove_const_t<std::remove_reference_t<A1>>;
198 FuncRef2(
S (T::*f)(A0, A1)) : fRef(f) {}
201 void operator()(
void* obj,
const std::string& s0)
override
205 std::stringstream strs(s0);
207 ((T*)obj->*fRef)(
a0, a1);
209 Placeholder* Clone()
const override {
return new FuncRef2(fRef); }
210 const std::type_info& ArgType(
size_t i)
const override
212 return i == 0 ?
typeid(A0) : typeid(A1);
214 S (T::*fRef)(A0, A1);
217 Placeholder* fContent =
nullptr;
218 std::size_t narg = 0;
G4double S(G4double temp)
G4AnyMethod(S(T::*f)(A0, A1))
G4AnyMethod & Swap(G4AnyMethod &rhs)
void operator()(void *obj)
const std::type_info & ArgType(size_t n=0) const
G4AnyMethod & operator=(S(T::*f)(A0, A1))
G4AnyMethod(S(T::*f)(A0))
G4AnyMethod(const G4AnyMethod &other)
G4AnyMethod & operator=(const G4AnyMethod &rhs)
G4AnyMethod & operator=(S(T::*f)(A0))
void operator()(void *obj, const std::string &a0)
G4AnyMethod & operator=(S(T::*f)())
const char * what() const override