是否可以使用参数保存函数指针以供以后使用?

问题描述 投票:3回答:2

昨天,我试图编写一个基本渲染器,渲染器在数据加载到着色器时控制渲染器,而渲染对象不知道所使用的着色器。作为一个顽固的人(而不是睡眠不足),我花了几个小时试图将函数指针发送到渲染器,保存,然后在适当的时间运行。直到后来我意识到我想要构建的是一个消息系统。它让我想知道,是否可以直接保存带参数的函数指针,以便稍后在c ++中运行。

我最初的想法看起来像这样:

//set up libraries and variables
Renderer renderer();
renderable obj();
mat4 viewMatrix();
// renderer returns and object id
int objID = renderer.loadObj(obj)

int main()
{
  //do stuff
  while(running)
  {
    //do stuff
    renderer.pushInstruction(//some instruction);
    renderer.render();
  }
}

// functionPtr.h
#include <functional>

class storableFunction
{
  public:
  virtual ~storableFunction = 0;
  virtual void call() = 0;
};

template<class type>
class functionPtr : public storableFunction
{
  std::function<type> func;
public:
  functionPtr(std::function<type> func)
    : func(func) {}
  void call() { func(); }
};

//renderer.h
struct  modelObj
{
  // model data and attached shader obj
  std::queue<storableFunction> instruction;
}

class renderer
{
  std::map<int, modelObj> models;
public:
    // renderer functions
    void pushInputDataInstruction(int id, //function, arg1, arg2);
    // this was overloaded because I did not know what type the second argument  would be
    // pushInputDataInstruction implementation in .cpp
    {
      models[id].instruction.push(functionPtr(std::bind(//method with args)))
    }
  void render();
};

//implantation in .cpp
{
  for(// all models)
  //bind all data
  applyInstructions(id);
  // this would call all the instructrions using functionptr.call() in the queue and clear the queue
  draw();
  // unbind all data
}

我意识到boost可能支持某种类似的功能,但我想避免使用boost。

这样的事情是可能的,一般的设计会是什么样的,甚至用什么来看待消息总线是一个更加成熟的设计模式呢?

c++ function class function-pointers
2个回答
2
投票

是否可以直接保存带参数的函数指针,以便稍后在c ++中运行。

是的。

首先,如果您已经使用C ++ 11或者后者,则不需要使用boost来处理当前的问题。

最简单直观的方法是,将所有函数作为lambda函数(即返回lambdas)并存储到您的函数中

std::queue<storableFunction> instruction;

你会在这里找到关于lambdas的详细解释:What is a lambda expression in C++11?


提供storableFunction的想法很好,因为你可以为你存储到modelObj的每个成员函数明确地告诉函数指针类型。

但是,如果考虑存储到某些STL容器,则需要使用std::function,其中有一些type erasure overhead,它可以处理the different lambda functions,能够捕获范围内的变量)。

这是an example codestd::vector

#include <iostream>
#include <vector>
#include <functional>

int main()
{
  int arg1 = 4;
  std::string arg2 = "String";

  std::vector<std::function<void()>> vecFunPtr
  {
    [](int arg1 = 1){ std::cout << arg1 << std::endl; },
    [](float arg1 = 2.0f){ std::cout << arg1 << std::endl; },
    [](double arg1 = 3.0){ std::cout << arg1 << std::endl; },
    [&arg1, &arg2](){ std::cout << arg1 << " " << arg2 << std::endl; }
  };

  for(const auto& funs: vecFunPtr) funs(); // call the stored lambdas
  return 0;
}

输出:

1
2
3
4 String

在您的情况下,Renderer可以写成如下。有一点需要注意,你需要做一些解决方法,将不同的参数传递给成员函数(或者肯定是lambda捕获)。

附注:Here你会发现一些提示,以避免由于std::function导致的性能问题,这可能会有所帮助。

class Renderer
{
  typedef std::queue<std::function<void()>> modelObj; // you might need modelObj for only this class
  typedef std::function<void()> fFunPtr;              // typedef for void(*)() using std::function
  std::map<int, modelObj> models;
public:
    // renderer functions can be written like returning a lambda
    fFunPtr rFun1(int arg1)    { return [](int arg1 = 1){ std::cout << arg1 << std::endl; }; }
    fFunPtr rFun2(double arg1) { return [](float arg1 = 2.0f){ std::cout << arg1 << std::endl; }; }
    // function to store them for latter use
    void pushInputDataInstruction(const int id, const fFunPtr& funPtr)
    {
      models[id].push(funPtr); 
    }
};

int main()
{
  Renderer objRender;
  //do stuff
  while(/*condition*/)
  {
    //do stuff
    objRender.pushInstruction(/* id number, function pointer*/)
    renderer.render();
  }
  return 0;
}

7
投票

std::bind是一种方法,但如果您可以访问C ++ 11及更高版本,则可能需要考虑使用lambdas。 Scott Meyer建议他们在Effective Modern C ++中使用std :: bind(在大多数情况下)。

lambda有三个部分:

  • []部分,用于标识捕获的值或引用,
  • ()部分,用于标识稍后将在调用lambda时提供的参数。
  • {}部分,用于标识如何处理捕获的值和参数

简单的例子:

#include <iostream>

void printValue(int x) {
    std::cout << x << std::endl;
}

int main(int argc, char * argv[]) {
    int x = 23;

    // [x] means 'capture x's value, keep it for later'
    // (int y) means 'I'll provide y when I invoke the lambda'
    auto storedFunction = [x](int y){return printValue(x + y);};

    x = 15;

    // Now we invoke the lamda, with y = 2
    // Result: 25 (23 + 2), even if x was changed after the lambda was created
    storedFunction(2); 
    return 0;
}

如果要捕获对x的引用,请使用[&x]。在上面的例子中,结果将是17(即15 + 2)。如果您确实使用了引用,请注意不要让xstoredFunction之前超出范围,因为它将成为垃圾数据的悬空引用。

大多数编译器现在支持C ++ 11,但您可能需要在项目设置中明确添加支持:

  • Visual Studio:项目属性/ C ++ /语言/ C ++语言标准
  • gcc:--std=c++11(或14或17 ......)
  • CMake还允许您设置标准:set (CMAKE_CXX_STANDARD 11)
© www.soinside.com 2019 - 2024. All rights reserved.