Frage zur Umsetzung [Parser]
-
Ich programmiere gerade eine Konsole und bin auf folgendes Problem beim Parser gestoßen:
Ich habe eine Klasse Command erstellt, die den Befehl und die zugehörige Funktion enthält:
class Command { private: basic_string<wchar_t>& name_; void (*functionPointer)(void); public: Command(void); ~Command(void); Command& operator =(const basic_string<wchar_t>& name); Command& operator =(void (*functionPointer)(void)); // == --> string und command!!! friend inline bool operator==(const Command& cmdA, const Command& cmdB); friend inline bool operator==(const basic_string<wchar_t>& cmdA, const Command& cmdB); bool execute(void) const; }; inline bool operator==(const Command& cmdA, const Command& cmdB); inline bool operator==(const basic_string<wchar_t>& cmdA, const Command& cmdB);Das Problem ist nun folgendes:
void (*functionPointer)(void);Die Parameter der Funktion sind nur Void, ich könnte zwar jetzt die Command Klasse mit Templates überladen, dass hilft mir aber ncith grade weiter. Was ist, wenn ich eine Funktion haben will, die n-Parameter, eines x-beliebigen Datentypes haben will?
wie: void
move(uint objID, float x, float y, float z);
-
dann benutz doch nen functor :).
btw: komisches klassendesign
-
functor?
-
Tc++H schrieb:
functor?
generische Module
-
Tc++H schrieb:
functor?
Wikipedia
-
Hallo,
ein Functor ist ein Objekt einer Klasse die den Funktionsoperator operator() überlädt. Der entscheidene Vorteil eines Funktors gegenüber einer Funktion (bzw. eines Funktionszeigers) ist der, dass ein Funktor, da es sich ja um ein Objekt handelt, beliebigen Zustand mit sich rumschleppen kann.
-
ich finde die folgende Methode sehr nutzbar und hab sie schon mehr mals in unterschidlichen Projecten verwendet.
class ICommand { public: virtual void Do() = 0; virtual void Undo() = 0; virtual void Redo() = 0; }; class ICommandProcessor { public: virtual execute(ICommand *cmd) = 0; virtual process(ICommand *cmd) = 0; }; class InsertText : public ICommand { public: InsertText(ITextStory *story, TextIndex position, std::wstring &text); }; class DeleteText : public ICommand { DeleteText(ITextStory *story, const TextRange &range); }; ICommand *insertTextCmd(ITextStory *story, TextIndex position, std::wstring &text) { return new InsertText(story, position, text); } ICommand *deleteTextCmd(ITextStory *story, const TextRange &range) { return new DeleteText(story, range); } int main() { smatr_ptr<ICommandProcessor> cmdProc(...); smatr_ptr<ICommandSequence> cmdSeq(...); try { cmdProc->process(insertTextCmd( textModel, 11, "aaaaaaaaabbbbbbccccc"))); cmdProc->process(deleteTextCmd( textModel, TextRange(0, 23))); cmdSec->Commit(); } catch(std::exception &e) { cmdSec->Abort(); //... } return 0; }