|
|
#include <Objection.h> namespace SCO_SC {// Handlers and default actions typedef int Objection_action(const char* message); class Objection{ public: // Constructors, destructor Objection(); Objection(Objection_action* default_action); ~Objection(); // Copy and assign Objection(const Objection& o); Objection& operator=(const Objection& o); // Raise the Objection int raise(const char* message = "); // Designate a handler Objection_action* appoint(Objection_action* hdlr=0); Objection_action* ignore(); }; }
Objections provide a simple error-handling facility similar to UNIX® signals (see signal(2)). Several library components (e.g., Fsm(3C++)) use Objections to report errors to clients.
An Objection is normally associated with a single error or class of errors. Consider the sequence of events that occurs when an error associated with Objection x is detected. To signal that the error has occurred, x.raise() is called. raise(), in turn, calls the handler associated with Objection x. A handler is associated with Objection x by calling x.appoint() with the address of the handler. If x.appoint() is not called, a default handler will be used (see below). A handler is expected to perform some appropriate action and then return an integer value; by convention, 1 indicates success and 0 indicates failure in handling the error. A default handler does nothing except return 0. Next, assuming the handler returns 0 AND a default action was specified when the Objection was created, the default action will be performed. Finally, control will be returned to the caller (the one who called raise()). The value returned to the caller is precisely the value returned by raise().
typedef int Objection_action(const char* message); Serves as the type for both handlers and default actions. Handlers return an integer; by convention, 0 indicates failure and 1 indicates success. A default action returns an integer, but the integer has no meaning.
Objection(); An Objection whose default handler does nothing except return 0. No default action is defined for this Objection.
Objection(Objection_action* default_action); An Objection whose default handler does nothing except return 0 and whose default action is default_action. If default_action is 0, the Objection is equivalent to one created using the parameterless constructor.
~Objection(); Destructor.
Objection(const Objection& o);
Objection& operator=(const Objection& o); Copying or assigning an Objection creates a copy of its value.
int raise(const char* message = %%); Calls the handler associated with this Objection, passing the string message. If the handler returns zero and a default action has been defined, the default action will be performed. Returns the value returned by the handler.
Objection_action* appoint(Objection_action* hdlr=0); Changes the handler associated with this Objection to hdlr. If the argument is omitted, a handler that does nothing except return 0 will be used. Returns a pointer to the previous handler.
Objection_action* ignore(); Changes the handler associated with this Objection to one that does nothing except return 1. Returns a pointer to the previous handler.
Ideally, exception-handling facilities should be built into a programming language. Exceptions have quite complex semantics that cannot be simulated by function calls. Objections are not a substitute for exceptions, nor are they upward-compatible with exceptions.
There should probably be two typedefs, one for handlers and one for default actions.