In C++ if I have object of some class Class obj;
I want to be able to intercept and process calls of methods of any names and arguments like obj.add(123)
, also to process any properties usage like auto x = obj.value;
.
The thing is that I don't know what method or property names will be used, e.g. one may use obj.abc(1, 2, true)
or auto x = obj.xyz;
.
So if somebody called obj.abc(1, 2, true)
I want next method to be called with args ("abc", 1, 2, true)
:
template <typename ... Args>
std::any ProcessMethod(std::string const & method_name, Args && ... args) {/*...*/}
or if property was used auto x = obj.xyz;
I want next method to be called with ("xyz")
argument:
std::any ProcessProperty(std::string const & property_name) {/*...*/}
Those two methods above use runtime approach. Even better would be for me to pass called method or property name at compile time (as compile time fixed string). So that I can find out method return type or property type at compile time, i.e. do (for some special class FixedString holding string at compile time):
template <FixedString method_name, typename ... Args>
typename ReturnTypeOf<method_name>::type ProcessMethod(Args && ... args) {/*...*/}
template <FixedString property_name>
typename PropertyTypeOf<property_name>::type & ProcessProperty() {/*...*/}
Is that possible anyhow to intercept all calls to unknown methods or properties and forward them to last two methods above?
Of cause I can use next syntax: obj.call<"abc">(1, 2, true)
and auto x = obj.prop<"xyz">();
to solve my task, but if it is possible to solve anyhow in C++ I would like to stay with syntax obj.abc(1, 2, true)
and auto x = obj.xyz;
.
Is very last syntax above achievable anyhow through C++ reflection or templates magic?
Aucun commentaire:
Enregistrer un commentaire