Archived

This topic is now archived and is closed to further replies.

chbfiv

Cmd Pipeline function pointer

Recommended Posts

Im working on a class that takes input commands and runs a function based off that command. for now, Classes can AddCmd''s to the global ref of Cmd_pipeline, but I can''t figure out a way to pass function pointers for what the cmd is going todo. For example:
-main.cpp-
//include Cmd_PipeLine

//Cmd_PipeLine gCmdPipe; 

//main
     //scan for Cmds

-WindowClass.h-
//Class CreateWindow
     //void CreateWindow();
     //gCmdPipe.AddCmd("CreateWindow",CreateWindow());

 
AddCmd(string& cmdlabel,void *Function???); I''m a bit lost on how to store or even pass a function pointer to that class function, or even call it in the AddCmd body....?

Share this post


Link to post
Share on other sites
This is kind a bump, and Update to the Question.

-I figured out how to pass funtions as arguments.

class Cmd_Pipeline {
public:
Cmd_Pipeline() {}
~Cmd_Pipeline() {}
bool IsCmd(string& text);
int Run(string text) {
int size = Labels.size();
for(int i=0;i < size; i++) {
if(text == Labels)
cout << "nice!" << endl;
}
return 0;
}

void AddCmd(string label,int (*Function)()) {
Labels.push_back(label);
pFunctions.push_back(Function);
}
private:
typedef int (*pFunctionMember)();
vector Labels;
vector pFunctions;
};

Cmd_Pipeline cmdpipe;

int Function1() {
cout << "function1 was Run!" << endl;
return 0;
}

int main (int argc,char** argv) {
bool bRunProgram = true; //keep scanning scanf
string scanBuffer;//buffer for scanf

cmdpipe.AddCmd("function1",&Function1);
cmdpipe.Run("function1");

while(bRunProgram) {
cin >> scanBuffer;
transform(scanBuffer.begin(),scanBuffer.end(),scanBuffer.begin(),toupper); //makes commands NOT case dependent
if(scanBuffer == "EXIT")
bRunProgram = false;
else
bRunProgram = true;
scanBuffer.clear();
}

return(0);
}

This works great! however, If I were to add a add Under Funtion1''s Body

class Tmp {
public:
Tmp() { cmdpipe.AddCmd("TmpFunc1",&Tmp::Func1); } //this??
~Tmp() { }
int Func1() { cout << "Tmp Class Func1 called!" << endl; }
};


main.cpp(14): error C2664: ''Cmd_Pipeline::AddCmd'' : cannot convert parameter 2 from ''int (__thiscall Tmp::* )(void)'' to ''int (__cdecl *)(void)''

I understand this is a class funtion, so I added this to the Cmd_Pipeline class

private:
//Add this line,
typedef int (Cmd_Pipeline::*pClassFunctionMember)() pClassFunction;
//Now I can define a vector of ClassFunctions for CmdPipeline

However, I cant overload AddCmd() because I don''t know how to pass class funtion as arguments.

I guessed this
void AddCmd(string label,int (Cmd_Pipeline::*Function)()) { }

but it looks for a member of Cmd_Pipeline called Function*...

So right now I have two objectives.
1-figure out how to pass in a class function into a function, or how to define a function argument of a function.
2-make it so the function can return any type, or have any number of args

Share this post


Link to post
Share on other sites
quote:
figure out how to pass in a class function into a function

Pointer to member syntax is gross. Try a space between the * and the name?

I use Boost.Function, it will let you use member functions or regular functions, and you can call them like a normal function.

quote:
how to define a function argument of a function.

int (*func)(int, string&)

defines a function pointer that returns an integer and takes a string reference.

quote:
make it so the function can return any type, or have any number of args

You''ll have to go with some sort of generic type like a string for everything to pull this off. C++ sucks like that.

Share this post


Link to post
Share on other sites
I got this from www.function-pointer.org


class TFunctor {
public:
virtual void operator()(const char* string)=0; // call using operator

virtual int operator()()=0; // call using operator

};
template <class TClass> class Functor : public TFunctor {
private:
void (TClass::*fpt0)(const char*); // pointer to member function

int (TClass::*fpt1)();
TClass* pt2Object; // pointer to object

public:
Functor(TClass* _pt2Object, void(TClass::*_fpt)(const char*)) { pt2Object = _pt2Object; fpt0=_fpt; };
Functor(TClass* _pt2Object, int(TClass::*_fpt)()) { pt2Object = _pt2Object; fpt1=_fpt; };
virtual void operator()(const char* string) { (*pt2Object.*fpt0)(string);}; // execute member function

virtual int operator()() { (*pt2Object.*fpt1)();return(0);};
};


Only problem is you need to define each function type, but i only needed it for one but with multi classes, so this worked great for my Cmd pipeline.
Also note, that I just moved around some stuff, and made my own changes.


Share this post


Link to post
Share on other sites