45 #define G4AnyMethod_h 1
54 virtual const char*
what()
const throw() {
55 return "G4BadArgument: failed operator()";
59 #if defined(G4USE_STD11) || defined(G4USE_STD14)
60 #include <type_traits>
61 using std::remove_reference;
85 fContent =
new FuncRef<S,T>(f);
87 template <
class S,
class T,
class A0>
G4AnyMethod(
S (T::*f)(A0)) : narg(1) {
88 fContent =
new FuncRef1<S,T,A0>(f);
90 template <
class S,
class T,
class A0,
class A1>
G4AnyMethod(
S (T::*f)(A0,A1)) : narg(2) {
91 fContent =
new FuncRef2<S,T,A0,A1>(f);
94 fContent(other.fContent ? other.fContent->Clone() : 0),narg(other.narg) {}
101 std::swap(fContent, rhs.fContent);
102 std::swap(narg, rhs.narg);
133 fContent->operator()(obj);
136 fContent->operator()(obj,
a0);
139 size_t NArg()
const {
return narg; }
141 const std::type_info&
ArgType(
size_t n = 0)
const {
142 return fContent ? fContent->ArgType(
n) :
typeid(
void);
149 virtual ~Placeholder() {}
150 virtual Placeholder* Clone()
const = 0;
152 virtual void operator()(
void*,
const std::string&) = 0;
153 virtual const std::type_info&
ArgType(
size_t)
const = 0;
156 template <
class S,
class T>
struct FuncRef:
public Placeholder {
157 FuncRef(
S (T::*f)()) : fRef(f) {}
162 virtual void operator()(
void*,
const std::string&) {
165 virtual Placeholder* Clone()
const {
166 return new FuncRef(fRef);
168 virtual const std::type_info&
ArgType(
size_t)
const {
174 template <
class S,
class T,
class A0>
struct FuncRef1:
public Placeholder {
177 FuncRef1(
S (T::*f)(A0)) : fRef(f) {}
182 virtual void operator()(
void* obj,
const std::string&
s0) {
184 std::stringstream strs(s0);
186 ((T*)obj->*fRef)(
a0);
188 virtual Placeholder* Clone()
const {
189 return new FuncRef1(fRef);
191 virtual const std::type_info&
ArgType(
size_t)
const {
197 template <
class S,
class T,
class A0,
class A1>
struct FuncRef2:
public Placeholder {
201 FuncRef2(
S (T::*f)(A0, A1)) : fRef(f) {}
206 virtual void operator()(
void* obj,
const std::string&
s0) {
209 std::stringstream strs(s0);
211 ((T*)obj->*fRef)(
a0, a1);
213 virtual Placeholder* Clone()
const {
214 return new FuncRef2(fRef);
216 virtual const std::type_info&
ArgType(
size_t i)
const {
217 return i == 0 ?
typeid(A0) :
typeid(A1);
219 S (T::*fRef)(A0, A1);
222 Placeholder* fContent;
G4AnyMethod(S(T::*f)(A0))
G4AnyMethod(const G4AnyMethod &other)
G4AnyMethod(S(T::*f)(A0, A1))
G4AnyMethod & operator=(S(T::*f)())
const G4ThreeVector const G4double const
G4AnyMethod & Swap(G4AnyMethod &rhs)
void operator()(void *obj)
const std::type_info & ArgType(size_t n=0) const
virtual const char * what() const
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
void operator()(void *obj, const std::string &a0)