|
optional< MethodArgument > | apply (MetaObject &instance, const MethodArguments &arguments) const |
| Applies the arguments on the method of an instance of the class holding the member function.
|
|
int | getArgumentCount () const |
| Gets the number of method arguments.
|
|
AbstractPropertyType | getArgumentType (size_t index) const |
| Gets the argument type from a given index.
|
|
const char * | getName () const |
| Gets the name of the method.
|
|
AbstractPropertyType | getReturnType () const |
| Gets the return-type of the method.
|
|
bool | isValid () const |
| Checks whether the metamethod is valid.
|
|
| MetaMethod () |
| Constructor.
|
|
template<class TClass , typename TRet , typename TRetProp > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)()) |
| Template constructor for a method with return type and no argument specialization.
|
|
template<class TClass , typename TRet , typename TRetProp , typename TArg1 , typename TArg1Prop > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)(TArg1), const PropertyType< TArg1Prop > &property1) |
| Template constructor for a method with return type and one argument specialization.
|
|
template<class TClass , typename TRet , typename TRetProp , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)(TArg1, TArg2), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2) |
| Template constructor for a method with return type and two arguments specialization.
|
|
template<class TClass , typename TRet , typename TRetProp , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)(TArg1, TArg2, TArg3), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3) |
| Template constructor for a method with return type and three arguments specialization.
|
|
template<class TClass , typename TRet , typename TRetProp , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop , typename TArg4 , typename TArg4Prop > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)(TArg1, TArg2, TArg3, TArg4), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3, const PropertyType< TArg4Prop > &property4) |
| Template constructor for a method with return type and four arguments specialization.
|
|
template<class TClass , typename TRet , typename TRetProp , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop , typename TArg4 , typename TArg4Prop , typename TArg5 , typename TArg5Prop > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)(TArg1, TArg2, TArg3, TArg4, TArg5), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3, const PropertyType< TArg4Prop > &property4, const PropertyType< TArg5Prop > &property5) |
| Template constructor for a method with return type and five arguments specialization.
|
|
template<class TClass , typename TRet , typename TRetProp , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop , typename TArg4 , typename TArg4Prop , typename TArg5 , typename TArg5Prop , typename TArg6 , typename TArg6Prop > |
| MetaMethod (FixedString name, const PropertyType< TRetProp > &ret, TRet(TClass::*method)(TArg1, TArg2, TArg3, TArg4, TArg5, TArg6), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3, const PropertyType< TArg4Prop > &property4, const PropertyType< TArg5Prop > &property5, const PropertyType< TArg6Prop > &property6) |
| Template constructor for a method with return type and six arguments specialization.
|
|
template<class TClass > |
| MetaMethod (FixedString name, void(TClass::*method)()) |
| Template constructor for a void method with no argument specialization.
|
|
template<class TClass , typename TArg1 , typename TArg1Prop > |
| MetaMethod (FixedString name, void(TClass::*method)(TArg1), const PropertyType< TArg1Prop > &property1) |
| Template constructor for a void method with one argument specialization.
|
|
template<class TClass , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop > |
| MetaMethod (FixedString name, void(TClass::*method)(TArg1, TArg2), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2) |
| Template constructor for a void method with two arguments specialization.
|
|
template<class TClass , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop > |
| MetaMethod (FixedString name, void(TClass::*method)(TArg1, TArg2, TArg3), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3) |
| Template constructor for a void method with three arguments specialization.
|
|
template<class TClass , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop , typename TArg4 , typename TArg4Prop > |
| MetaMethod (FixedString name, void(TClass::*method)(TArg1, TArg2, TArg3, TArg4), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3, const PropertyType< TArg4Prop > &property4) |
| Template constructor for a void method with four arguments specialization.
|
|
template<class TClass , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop , typename TArg4 , typename TArg4Prop , typename TArg5 , typename TArg5Prop > |
| MetaMethod (FixedString name, void(TClass::*method)(TArg1, TArg2, TArg3, TArg4, TArg5), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3, const PropertyType< TArg4Prop > &property4, const PropertyType< TArg5Prop > &property5) |
| Template constructor for a void method with five arguments specialization.
|
|
template<class TClass , typename TArg1 , typename TArg1Prop , typename TArg2 , typename TArg2Prop , typename TArg3 , typename TArg3Prop , typename TArg4 , typename TArg4Prop , typename TArg5 , typename TArg5Prop , typename TArg6 , typename TArg6Prop > |
| MetaMethod (FixedString name, void(TClass::*method)(TArg1, TArg2, TArg3, TArg4, TArg5, TArg6), const PropertyType< TArg1Prop > &property1, const PropertyType< TArg2Prop > &property2, const PropertyType< TArg3Prop > &property3, const PropertyType< TArg4Prop > &property4, const PropertyType< TArg5Prop > &property5, const PropertyType< TArg6Prop > &property6) |
| Template constructor for a void method with six arguments specialization.
|
|
MetaMethod provides method invocation mechanism for Kanzi classes.
You can expose class methods to scripting and in general to its metadata subsystem using the MetaMethod class. To do so you must register the methods to the metadata of the class using the KZ_METACLASS_METHOD macro.
class ExposeMethods : public MetaObject
{
public:
static PropertyType<int> IntPropertyType;
static PropertyType<
kanzi::
string> StringPropertyType;
static MetaMethod VoidMethod;
static MetaMethod StringRet;
static MetaMethod VirtualMethod;
static MetaMethod OneArgument;
static MetaMethod TwoArguments;
static MetaMethod VoidMethodOverload;
static shared_ptr<ExposeMethods> create(Domain*, string_view)
{
return shared_ptr<ExposeMethods>(new ExposeMethods);
}
explicit ExposeMethods();
bool m_voidMethodCalled;
bool m_virtualMethodCalled;
int m_intDataReceived;
kanzi::string m_stringDataReceived;
void voidMethod();
string stringRet();
virtual void virtualMethod();
void oneArgument(int i);
void twoArguments(int i, const kanzi::string& s);
void voidMethod(int i);
};
PropertyType<int> ExposeMethods::IntPropertyType(
kzMakeFixedString(
"Test.ExposeMethods.IntPropertyType"), -30, 0,
true);
PropertyType<kanzi::string> ExposeMethods::StringPropertyType(
kzMakeFixedString(
"Test.ExposeMethods.StringPropertyType"),
"lazy dog", 0,
true);
MetaMethod ExposeMethods::VoidMethod(
kzMakeFixedString(
"voidMethod"), &ExposeMethods::voidMethod);
MetaMethod ExposeMethods::StringRet(
kzMakeFixedString(
"stringRet"), ExposeMethods::StringPropertyType, &ExposeMethods::stringRet);
MetaMethod ExposeMethods::VirtualMethod(
kzMakeFixedString(
"virtualMethod"), &ExposeMethods::virtualMethod);
MetaMethod ExposeMethods::OneArgument(
kzMakeFixedString(
"oneArgument"), &ExposeMethods::oneArgument, ExposeMethods::IntPropertyType);
MetaMethod ExposeMethods::TwoArguments(
kzMakeFixedString(
"twoArguments"), &ExposeMethods::twoArguments, ExposeMethods::IntPropertyType, ExposeMethods::StringPropertyType);
MetaMethod ExposeMethods::VoidMethodOverload(
kzMakeFixedString(
"voidMethod2"), &ExposeMethods::voidMethod, ExposeMethods::IntPropertyType);
You can invoke a metamethod on an object using one of the invoke() static methods. Use the string representation of the method you want to invoke.
shared_ptr<ExposeMethods> object = ExposeMethods::create(nullptr, "");
MetaMethod::invoke(*object, "voidMethod");
The arguments are passed to the invocation using the MethodArguments class, where you must specify the argument types with property types which are the same as the targeted method arguments.
MethodArguments args;
args.setArgument(ExposeMethods::IntPropertyType, 100);
optional<MethodArgument> result = MetaMethod::invoke(*object, "oneArgument", args);
For an invocation to succeed, you must set all argument types in the MethodArguments which are present in the signature of the invoked method. Omitting any argument required by the signature results in failure.
optional<MethodArgument> result = MetaMethod::invoke(*object, "oneArgument");
if (result)
{
}
When you need to make overloaded methods available to the metadata of the class, to invoke the correct overload, you must create a dedicated MetaMethod instance with a different name.
MethodArguments args;
args.setArgument(ExposeMethods::IntPropertyType, 42);
optional<MethodArgument> result = MetaMethod::invoke(*object, "voidMethod2", args);
invoke() stores the return values of the invoked methods in a MethodArgument object. The function returns nullopt if the invocation cannot find the method in the metadata of the object, or in either of its ascendants metadata.
A MethodArgument instance holding the result is returned if the invocation found the method and was able to apply the specified arguments. When the method is of void type, the returned result object contains an invalid MethodArgument instance.
optional<MethodArgument> result = MetaMethod::invoke(*object, "stringRet");
if (result != nullopt && result.value().isValid())
{
}
In cases when the argument types are unknown at build time and you need a dynamic argument invocation, use the MetaMethod name to query the MetaMethod from the metaclass of the object, set the arguments based on the method arguments and apply those on the metamethod.
typedef map<PropertyDataType, Variant> ValueMap;
shared_ptr<ExposeMethods> instance = ExposeMethods::create(nullptr, "");
ValueMap valueMap;
valueMap.insert(make_pair(PropertyDataTypeInt, Variant(321)));
valueMap.insert(make_pair(PropertyDataTypeString, Variant(string("Archaeopteryx"))));
MethodArguments arguments;
const MetaMethod* method = instance->getDynamicMetaclass()->findMethod("twoArguments");
if (method)
{
for (int i = 0; i < method->getArgumentCount(); i++)
{
AbstractPropertyType type = method->getArgumentType(i);
switch (type.getDataType())
{
arguments.setAbstractArgument(type, get<int>(valueMap.at(PropertyDataTypeInt)));
break;
arguments.setAbstractArgument(type, get<string>(valueMap.at(PropertyDataTypeString)));
break;
default:
break;
}
}
method->apply(*instance, arguments);
}