UPLOAD - DOSUIT07.ZIP - COMMAND.HPP

 
Output of file : COMMAND.HPP contained in archive : DOSUIT07.ZIP

#ifndef COMMAND_HPP
#define COMMAND_HPP
#ifndef COLLECT_HPP
#include "collect.hpp"
#endif
#ifndef PROTECT_HPP
#include "protect.hpp"
#endif

/*
This is the base class for a whole collection of classes (see
GCommand below). It enables pointers to methods in classes to be
passed around the system and through the EventQueue in a type-safe
manner.
*/
class Command : public Protected {
virtual int v_doit(const char *sender); // Default implementation does nothing
public:
virtual ~Command();
// Force virtual destructor.
int doit(const char *sender) { VALIDATE(this); return v_doit(sender); }
// Do the command. Actually calls the private virtual method
// v_doit, which is overridden in subclasses. The default
// implementation does nothing.
virtual Command *clone() const;
// Make an exact copy of this command.
// You MUST override this in derived classes.
};

declare(GCollection,Command);

class Window;

// A Command which closes a specific Window.
class CloseCommand : public Command {
Window *w;
virtual int v_doit(const char *sender); // Close the Window
public:
CloseCommand(Window *window);
virtual Command *clone() const;
// Overridden, as always. Make an exact copy of this command.
};

// A Command that calls a static/external function.
// The function is passed the name of the object which is sending the command.
class FunctionCommand : public Command {
int (*f)(const char *);
virtual int v_doit(const char *sender); // Call the funstion
public:
FunctionCommand(int (*function)(const char *));
virtual Command *clone() const;
// Overridden, as always. Make an exact copy of this command.
};

#define GCommand(type) name2(GCommand,type)

/*
A Generic Command which calls a method for objects of type type. The
method is passed a pointer to the sending VObject passed to
GCommand(type)::doit(). GCommands may be associated with Options,
Buttons etc. To make an ActionButton which calls the
select(const char *) method of your class MyList, you first need a
GCommand class which can send messages to a MyList. All you need to
do is

declare(GCommand,MyList);

in your header (mylist.hpp) file. This will provide the definition
of a GCommand class, called GCommand(MyList), including all the
inline functions. Then, in your mylist.cpp file, do

implement(GCommand,MyList)

(Note lack of semicolon), which will implement the non- inline
functions.

Now, assuming you have created a MyList object called aMyList, you
can create the ActionButton ...

ActionButton *button = new ActionButton(
new GCommand(MyList)(MyList::select,aMyList), "Select");
*/
#define GCommanddeclare(type)\
class GCommand(type) : public Command {\
int (type::*f)(const char *);\
type *to;\
virtual int v_doit(const char *);\
public:\
GCommand(type)(int (type::*function)(const char *), type *destination);\
virtual Command *clone() const;\
}

#define GCommandimplement(type)\
GCommand(type)::GCommand(type)(int (type::*function)(const char *), type *destination)\
: f(function), to(destination)\
{}\
int GCommand(type)::v_doit(const char *sender) { return (to && f) ? (to->*f)(sender) : 0; }\
Command *GCommand(type)::clone() const { return new GCommand(type)(*this); }

#define PCommand(type,param) name3(PCommand,type,param)

/*
A Generic Command which calls a method for objects of type type,
passing a parameter of type param. The method is also passed a
pointer to the sending VObject passed to GCommand(type)::doit().
PCommands may be associated with Options, Buttons etc. To make an
ActionButton which calls the select(VObject *,int) method of your
class MyList, you first need a PCommand class which can send
messages to a MyList. All you need to do is

declare(PCommand,MyList,int);

in your header (mylist.hpp) file. This will provide the definition
of a GCommand class, called PCommand(MyList,int), including all the
inline functions. Then, in your mylist.cpp file, do

implement(PCommand,MyList,int)

(Note lack of semicolon), which will implement the non- inline
functions.
*/
#define PCommanddeclare2(type,param)\
class PCommand(type, param) : public Command {\
int (type::*f)(param);\
type *to;\
param p;\
virtual int v_doit(const char *);\
public:\
PCommand(type,param)(int (type::*function)(const char *,param), type *destination, param parm);\
virtual Command *clone() const;\
}

#define PCommandimplement(type,param)\
PCommand(type,param)::PCommand(type,param)(int (type::*function)(const char *,param), \
type *destination, param parm)\
: f(function), to(destination), p(parm)\
{}\
int PCommand(type,param)::v_doit(const char *sender) { return (to->*f)(sender,p); }\
Command *PCommand(type,param)::clone() const { return new PCommand(type,param)(*this); }

#endif