Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

arc.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 1997, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef GLUE_ARC_H
00024 #define GLUE_ARC_H
00025 
00026 #include "std/ref.H"
00027 #include "std/strings.H"
00028 #include "fsa/guard.H"
00029 
00030 typedef const class STATE cSTATE;
00031 class STATE;
00032 #define STATEstart ((STATE *)-1)
00033 
00034 
00035 //
00036 //  TRIGGER - abstract trigger function for ARC transitions
00037 //
00038 //
00039 MAKE_PTR_BASEC(TRIGGER);
00040 class TRIGGER : public REFcounter {
00041    public:
00042       virtual     ~TRIGGER() { }
00043       virtual void exec(STATE *&, cEVENTptr &) = 0;
00044 };
00045 
00046 //
00047 //  TRIGGERfunc - function pointer ARC transition
00048 //
00049 //
00050 class TRIGGERfunc : public TRIGGER {
00051   public    : 
00052     typedef void (*_functor)(cEVENTptr &, STATE *&);
00053 
00054   protected : 
00055     _functor  _func;
00056     
00057   public: 
00058  virtual ~TRIGGERfunc() { }
00059           TRIGGERfunc(_functor f): _func(f) { }
00060 
00061      void exec(STATE *&s, cEVENTptr &e) { _func(e,s); }
00062 };
00063 
00064 //
00065 //  TRIGGERmeth - provides a method for ARC transition
00066 //
00067 //
00068 template <class T, class EVT>
00069 class TRIGGERmeth : public  TRIGGER {
00070   public:
00071    typedef void (T::*_meth)(EVT, STATE *&);
00072 
00073   protected:
00074     T       *_obj;
00075     _meth    _method;
00076 
00077   public:
00078  virtual ~TRIGGERmeth() { }
00079           TRIGGERmeth(T *obj, _meth meth): _obj(obj), _method(meth) { }
00080 
00081    void   exec(STATE *&s, cEVENTptr &e) { EVT x((EVT) e); 
00082                                           if (!x->cast(e)) {
00083                                             cerr<< "ERROR: TRIGGERmeth: event type ";
00084                                                 e->debug(cerr);
00085                                             cerr<<" doesn't match FSA Arc method " <<  x->static_name() << endl;
00086                                           } else
00087                                             (_obj->*_method)(x->cast(e),s); }
00088 };
00089 
00090 
00091 //----------------------------------------------------------
00092 //
00093 //  Arc - 
00094 //     represents an Arc of an FSA.  An Arc is traversed when
00095 //  its specific Event is generated.  Traversing an Arc 
00096 //  results in the Callback associated with the Arc being
00097 //  executed.  The next state of the FSA is returned by the
00098 //  callback.
00099 //
00100 //------------------------------------------------------
00101 typedef const class ARC cARC;
00102 class ARC {
00103  protected :
00104 
00105   str_ptr    _name;
00106   STATE     *_next;  // next state transition for ARC
00107   GUARDptr   _guard; // must be a pointer since we allow subclasses
00108   TRIGGERptr _trigger;
00109 
00110  public : 
00111  
00112              ARC(Cstr_ptr &n, cEVENTptr &e, cTRIGGERptr &t,STATE *s=0):_name(n),
00113                       _next(s),_guard(new GUARDevent(e)),_trigger(t) {}
00114              ARC(Cstr_ptr &n, cGUARDptr &g, cTRIGGERptr &t,STATE *s=0):_name(n),
00115                       _next(s),_guard(g),_trigger(t)  { }
00116   virtual   ~ARC() {}
00117 
00118   int        match(cEVENTptr &e)    const { return _guard->exec(e); }
00119   bool       guarded()              const { return _guard->guarded(); }
00120   cGUARDptr &guard()                const { return _guard; }
00121   int        operator==(cARC &a)    const { return *_guard == *a._guard; }
00122   void       debug(ostream &o)      const { if (_name) o << _name << ":";
00123                                             if (_guard)_guard->debug(o); }
00124   void       exec(STATE *&s, cEVENTptr &e, STATE *start) { 
00125                                             if (_next) 
00126                                                s = _next==STATEstart ? start:_next; 
00127                                             _trigger->exec(s,e);
00128                                             if (s == STATEstart) s = start; }
00129 };
00130 
00131 #endif

Generated on Mon Sep 15 16:25:56 2003 for gluebase by doxygen1.2.18