Boost::Signals for C++ Data copying

2019-05-27 12:22发布

Recently I had a class that looked like

class IGraphElement{
    typedef void FuncCharPtr(char*, int) ;
public:
    void Add(FuncCharPtr* f)
    {
        FuncVec.push_back(f);
    }
    void CastData(char * data, int length){
        for(size_t i = 0 ; i < FuncVec.size(); i++){
            char* dataCopy = new char[length];
            memcpy(dataCopy, data, length);
            FuncVec[i](dataCopy, length);
        }
    }
private:
    vector<FuncCharPtr*> FuncVec ;
};

There I was giving to all subscribers a pointer to there copy of data. Now I want to make my class to use boost. I understand that with boost I will be free from typedef and vector instead I would have something like

class IGraphElement{

public:
  signal<void (char*, int) > SigB;

but how shall be CastData rewritten for me to keep controll over data which will be sent/casted to subscribers?

1条回答
倾城 Initia
2楼-- · 2019-05-27 12:41

You are doing one mistake: you assume that the function you're calling will free the resources (the char* pointer) you give it. You should do something like:

void CastData(char * data, int length){
    for(size_t i = 0 ; i < FuncVec.size(); i++){
        char* dataCopy = new char[length];
        memcpy(dataCopy, data, length);
        FuncVec[i](dataCopy, length);
        delete[] dataCopy;
    }
}

Now, about the boost::signals. The signal simply holds a list of function pointers. If the signal is raised, it just calls each function in that list with the specified parameters. Something like:

class signal { //non-templated version
public:
    void operator() (Params) 
    {
        for (FuncList::iterator i = f_.begin(); i != f_.end(); ++i) {
            (**i)(Params);
        }
    }
private:
    typedef ... Function;
    typedef std::vector<Function> FuncList;
    FuncList f_;
}

Since the parameters are passed directly, you will have to encapsulate your data structure into a helper class. You will do the memory management in the copy constructor.

查看更多
登录 后发表回答