48#define G4AnyMethod_hh 1
57 virtual const char*
what()
const throw()
59 return "G4BadArgument: failed operator()";
64using std::remove_const;
65using std::remove_reference;
76 template <
class S,
class T>
79 fContent =
new FuncRef<S, T>(f);
82 template <
class S,
class T,
class A0>
86 fContent =
new FuncRef1<S, T, A0>(f);
89 template <
class S,
class T,
class A0,
class A1>
93 fContent =
new FuncRef2<S, T, A0, A1>(f);
97 : fContent(other.fContent ? other.fContent->Clone() : nullptr)
107 std::swap(fContent, rhs.fContent);
108 std::swap(narg, rhs.narg);
114 template <
class S,
class T>
122 template <
class S,
class T,
class A0>
129 template <
class S,
class T,
class A0,
class A1>
153 fContent->operator()(obj,
a0);
158 std::size_t
NArg()
const {
return narg; }
160 const std::type_info&
ArgType(
size_t n = 0)
const
162 return fContent ? fContent->ArgType(n) :
typeid(void);
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;
179 template <
class S,
class T>
180 struct FuncRef :
public Placeholder
186 virtual void operator()(
void* obj) { ((T*) obj->*fRef)(); }
187 virtual void operator()(
void*,
const std::string&)
191 virtual Placeholder* Clone()
const {
return new FuncRef(fRef); }
192 virtual const std::type_info& ArgType(std::size_t)
const
199 template <
class S,
class T,
class A0>
200 struct FuncRef1 :
public Placeholder
203 typename remove_const<typename remove_reference<A0>::type>::type nakedA0;
205 FuncRef1(
S (T::*f)(A0))
210 virtual void operator()(
void* obj,
const std::string& s0)
213 std::stringstream strs(s0);
215 ((T*) obj->*fRef)(
a0);
217 virtual Placeholder* Clone()
const {
return new FuncRef1(fRef); }
218 virtual const std::type_info& ArgType(
size_t)
const {
return typeid(A0); }
222 template <
class S,
class T,
class A0,
class A1>
223 struct FuncRef2 :
public Placeholder
226 typename remove_const<typename remove_reference<A0>::type>::type nakedA0;
228 typename remove_const<typename remove_reference<A1>::type>::type nakedA1;
230 FuncRef2(
S (T::*f)(A0, A1))
235 virtual void operator()(
void* obj,
const std::string& s0)
239 std::stringstream strs(s0);
241 ((T*) obj->*fRef)(
a0, a1);
243 virtual Placeholder* Clone()
const {
return new FuncRef2(fRef); }
244 virtual const std::type_info& ArgType(
size_t i)
const
246 return i == 0 ?
typeid(A0) :
typeid(A1);
248 S (T::*fRef)(A0, A1);
251 Placeholder* fContent =
nullptr;
252 std::size_t narg = 0;
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)())
virtual const char * what() const