0

I want to use memcpy within class template. So my template will be limited to any link to C POD (structure) and char* (and ofcourse structures can be declared in other independent classes). I want any class to be able to subscribe its function (if it has respectful input args) to cast event. So my class now looks 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 ;
};

generally I want 2 things that are really one (I try to explain in pseudocode):

template < typename GraphElementDatataStructurePtrType>
class IGraphElement{
    typedef void FuncCharPtr(GraphElementDatataStructurePtrType, int) ;  // here I want FuncCharPtr to be  of type (AnyClassThatWantsToConnectToThisGraphElement::*)(GraphElementDatataStructurePtrType, int) 

public:
    void Add(FuncCharPtr* f)
    {
        FuncVec.push_back(f);
    }

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

private:
    vector<FuncCharPtr*> FuncVec ; 
  };

Is what I desire any how possible and how to implement it into my class? (sorry - I am a c++ nube=()

Rella
  • 65,003
  • 109
  • 363
  • 636
  • Why do you want to use `memcpy`? And why are you using `new char[]` instead of `std::vector` or `std::string`, and ditto for the other type? – GManNickG Jan 30 '11 at 07:54
  • @GMan - If you can provide any possible code that would make my class better I would be happy to see it. But my point here is to copy pointer contents and to keep and share pointers for/to subscribers. BTW if boost can help I could use it... – Rella Jan 30 '11 at 08:44
  • Hi, please clarify a bit more. As I understand you want to accomplish two tasks: 1) Build a container for any callable entity 2) Call each entity from this container with provided arguments. Am I correct? – Marcin Jan 30 '11 at 09:19

1 Answers1

1

Your problem has been addressed by the boost::signals library.
If you are interested in the inner workings you can try to implement something similar using the boost::function and boost::bind libraries.
You can research the Modern C++ Design for details about the inner workings of functor templates, or just google&ask this forum.

Here's a solution code sketch using boost:

void DataCastHelper (boost::funtion funcCharPtr, char * data, int length) {
   char* dataCopy = new char[length];
   memcpy(dataCopy, data, length);

   funcCharPtr(dataCopy, length);
}

class IGraphElement {
public:
    void Add (FuncCharPt* f) {
        funcVec.connect(boost::bind(&DataCastHelper, f, _1, _2));
    }
    void CastData(char * data, int length){
        funcVec(data. length);
    }

private:
    boost::signal<FuncCharPtr> funcVec;
}

The FuncCharPt* f argument passed to the IGraphElement::Add method is stacked with DataCastHelper to make a data coping for you. The signal handles functors iteration and invocation and also passing arguments to functors.

Regards

Marcin
  • 897
  • 1
  • 7
  • 19
  • Please note that you can plug almost anything into the signal using boost::bind. Example: http://stackoverflow.com/questions/768351/complete-example-using-boostsignals-for-c-eventing – Marcin Jan 30 '11 at 10:54
  • nope - signal didn't work for me (see http://stackoverflow.com/questions/4844842/boostsignals-for-c-data-copying ) so can you provide any practical code on how to do what I need or any specific page on some specific book on how to do such thing? – Rella Jan 30 '11 at 20:02
  • I've added a short code sketch describing solution based on signals ad functors. If you have problems with "taking a grip of it" You probably should investigate functor usage futher. The Alexandrescu's book I've mentioned should be good for a start. – Marcin Jan 30 '11 at 21:46
  • and how would funcCharPtr typedef look like? – Rella Jan 30 '11 at 22:34
  • funcCharPtr should correspond to your signal template specialization type. So it should be boost::function or something similar. – Marcin Jan 30 '11 at 23:47