自动化方法调用 C++
Automatize method calling c++
我正在开发 C++11 应用程序。我有一个将被多次调用的方法(1000)。该方法接受字符串和函数指针作为参数。
问题是指向函数的指针将接受不同类型的参数,有时是 int,有时是 std:string 等等。
为了避免对我的方法进行 1000 次不同的调用,我打算构建一个 std::pairs 列表,其中包含字符串和函数指针,并遍历它以调用我的方法。
但我还没有找到一个干净的方法来实现它。
示例:
// This is the method I want to call,
// T is function pointer, I have several versions for
// different data types, e.g here bool version
template <class T>
void myMethod(const std::string& strArg,
void (T::*setter) (const bool)) const
上一个方法将使用不同的参数调用 1000 次:每个字符串对应唯一的函数指针。
现在我调用它 1000 次,例如:
myMethod(str1, pFunction1);
myMethod(str2, pFunction2);
myMethod(str3, pFunction3);
...
myMethod(str1000, pFunction1000);
我想,先创建一个由我手动填写的列表:
std::list<std::pair<str, functionPointer>>
然后我会为列表中的每个成员迭代调用我的方法
有什么想法吗?
感谢和问候
是的,你可以收集这样的对。
std::vector<std::pair<std::string, void (SomeClass::*) (const bool)>> myMethodArgs
{
{ str1, pFunction1 },
{ str2, pFunction2 },
...
{ str1000, pFunction1000 },
};
然后全部称呼它们
for (auto & pair : myMethodArgs)
{
myMethod(pair.first, pair.second); // deduces myMethod<SomeClass>
}
但听起来您对这些调用中的许多都有不同的类型。您可以取而代之的是由 lambda 构成的 std::function<void()>
的集合,每个 myMethod
的不同实例化
std::vector<std::function<void()>> myMethodCalls
{
[]{ myMethod(str1, &Class1::method); },
[]{ myMethod(str2, &Class2::other_method); },
...
[]{ myMethod(str1000, &Class1000::final_method); },
};
for (auto & call : myMethodCalls)
{
call();
}
我希望你能在稍后调用你的函数时做到这一点!
假设我们可以编写您想要的代码(伪代码):
std::list<std::pair<str, PointerToFunctionWithAnySignature >> ptrList;
现在你填写列表
ptrList.add( "bla", Do1 );
ptrList.add( "blub", Do2 );
函数的签名如下:
void Do1(int);
void Do2(float, std::string& );
现在您想遍历列表:
for ( auto& el: ptrList )
{
// here we want to call the function:
el.second( ???????? );
}
就是这个问题!如果您有不同的签名:您想如何知道您必须在该调用的哪个位置填写哪个参数。
因此,即使您能够存储指向具有不同签名的函数的指针,也没有任何意义,因为您无法调用它们!
如果您已经知道稍后调用的参数,您可以使用lambda functions
将函数和参数存储为单个对象,并使其可以使用唯一签名调用。您也可以为此使用 std::bind,但首选 lambda,它更易于处理且更易于阅读。
我正在开发 C++11 应用程序。我有一个将被多次调用的方法(1000)。该方法接受字符串和函数指针作为参数。
问题是指向函数的指针将接受不同类型的参数,有时是 int,有时是 std:string 等等。
为了避免对我的方法进行 1000 次不同的调用,我打算构建一个 std::pairs 列表,其中包含字符串和函数指针,并遍历它以调用我的方法。
但我还没有找到一个干净的方法来实现它。
示例:
// This is the method I want to call,
// T is function pointer, I have several versions for
// different data types, e.g here bool version
template <class T>
void myMethod(const std::string& strArg,
void (T::*setter) (const bool)) const
上一个方法将使用不同的参数调用 1000 次:每个字符串对应唯一的函数指针。
现在我调用它 1000 次,例如:
myMethod(str1, pFunction1);
myMethod(str2, pFunction2);
myMethod(str3, pFunction3);
...
myMethod(str1000, pFunction1000);
我想,先创建一个由我手动填写的列表:
std::list<std::pair<str, functionPointer>>
然后我会为列表中的每个成员迭代调用我的方法
有什么想法吗?
感谢和问候
是的,你可以收集这样的对。
std::vector<std::pair<std::string, void (SomeClass::*) (const bool)>> myMethodArgs
{
{ str1, pFunction1 },
{ str2, pFunction2 },
...
{ str1000, pFunction1000 },
};
然后全部称呼它们
for (auto & pair : myMethodArgs)
{
myMethod(pair.first, pair.second); // deduces myMethod<SomeClass>
}
但听起来您对这些调用中的许多都有不同的类型。您可以取而代之的是由 lambda 构成的 std::function<void()>
的集合,每个 myMethod
std::vector<std::function<void()>> myMethodCalls
{
[]{ myMethod(str1, &Class1::method); },
[]{ myMethod(str2, &Class2::other_method); },
...
[]{ myMethod(str1000, &Class1000::final_method); },
};
for (auto & call : myMethodCalls)
{
call();
}
我希望你能在稍后调用你的函数时做到这一点!
假设我们可以编写您想要的代码(伪代码):
std::list<std::pair<str, PointerToFunctionWithAnySignature >> ptrList;
现在你填写列表
ptrList.add( "bla", Do1 );
ptrList.add( "blub", Do2 );
函数的签名如下:
void Do1(int);
void Do2(float, std::string& );
现在您想遍历列表:
for ( auto& el: ptrList )
{
// here we want to call the function:
el.second( ???????? );
}
就是这个问题!如果您有不同的签名:您想如何知道您必须在该调用的哪个位置填写哪个参数。
因此,即使您能够存储指向具有不同签名的函数的指针,也没有任何意义,因为您无法调用它们!
如果您已经知道稍后调用的参数,您可以使用lambda functions
将函数和参数存储为单个对象,并使其可以使用唯一签名调用。您也可以为此使用 std::bind,但首选 lambda,它更易于处理且更易于阅读。