home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / base / binding.h < prev    next >
C/C++ Source or Header  |  1995-04-08  |  5KB  |  176 lines

  1.  
  2.  
  3. #ifndef _binding_h_ /* Mon Sep 20 08:44:42 1993 */
  4. #define _binding_h_
  5.  
  6.  
  7.  
  8.  
  9.  
  10. /*
  11.  *
  12.  *          Copyright (C) 1994, M. A. Sridhar
  13.  *  
  14.  *
  15.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  16.  *     to copy, modify or distribute this software  as you see fit,
  17.  *     and to use  it  for  any  purpose, provided   this copyright
  18.  *     notice and the following   disclaimer are included  with all
  19.  *     copies.
  20.  *
  21.  *                        DISCLAIMER
  22.  *
  23.  *     The author makes no warranties, either expressed or implied,
  24.  *     with respect  to  this  software, its  quality, performance,
  25.  *     merchantability, or fitness for any particular purpose. This
  26.  *     software is distributed  AS IS.  The  user of this  software
  27.  *     assumes all risks  as to its quality  and performance. In no
  28.  *     event shall the author be liable for any direct, indirect or
  29.  *     consequential damages, even if the  author has been  advised
  30.  *     as to the possibility of such damages.
  31.  *
  32.  */
  33.  
  34.  
  35.  
  36. // A "binding" is simply an object-method pair. The method denoted by a
  37. // binding must have signature
  38. // \par\begin{verbatim}
  39. //        bool Method (CL_Object&, long)
  40. // \end{verbatim}
  41. //
  42. // \noindent
  43. // Bindings are used for notification and inter-object communication. An
  44. // abstract binding (class CL_AbstractBinding) describes the protocol for
  45. // a binding; the derived template class CL_Binding is used to build
  46. // bindings for each class that needs one.
  47. //
  48.  
  49. #ifdef __GNUC__
  50. #pragma implementation
  51. #endif
  52.  
  53. #include "base/object.h"
  54.  
  55.  
  56. class CL_EXPORT CL_AbstractBinding: public CL_Object {
  57.  
  58. public:
  59.     ~CL_AbstractBinding () {};
  60.     
  61.     virtual bool Execute (CL_Object& o, long l) const;
  62.     // Execute the binding; that is, invoke our contained method on the
  63.     // object we point to, with parameters o and l. Return the return
  64.     // value of the invoked method.
  65.  
  66.     virtual bool Valid () const;
  67.     // Return whether this is a valid binding, i.e., whether both the
  68.     // contained object pointer and method pointer are non-null.
  69.  
  70.     virtual const  char* ClassName() const;
  71.  
  72. };
  73.  
  74.  
  75. // This is a template-based class that describes an object-method pair,
  76. // for an object of type {\tt Base}, the type parameter.
  77.  
  78. template <class Base>
  79. class CL_EXPORT CL_Binding: public CL_AbstractBinding {
  80.  
  81. public:
  82.     typedef bool (Base::*MethodPtr) (CL_Object&, long);
  83.  
  84.     //  ---------------------- Construction ----------------------
  85.     
  86.     CL_Binding (Base* obj, MethodPtr method)
  87.         {_obj = obj;  _method = method;};
  88.     // Construct a binding for an object of type {\tt Base}, that has a
  89.     // method {\tt method}.
  90.  
  91.     CL_Binding ()
  92.         { _obj = 0; _method = 0;};
  93.     // Construct a null binding, one whose object and method pointers are
  94.     // both NULL, for an object of type Base.
  95.  
  96.     CL_Binding (const CL_Binding<Base>& b)
  97.         {_obj = b._obj; _method = b._method;};
  98.     // Copy constructor.
  99.  
  100.     ~CL_Binding () {};
  101.     // Destructor.
  102.     
  103.     bool Valid () const { return _obj != 0 && _method != 0; };
  104.     // Return TRUE if both object and method pointers of this binding are
  105.     // non-NULL.
  106.     
  107.     bool Execute (CL_Object& o, long v) const
  108.         { return (_obj != 0 && _method != 0)
  109.                 ? (_obj->*_method) (o, v) : FALSE; };
  110.     // Override the virtual method inherited from {\tt AbstractBinding}.
  111.  
  112.     void operator= (const CL_Object& o)
  113.         {*this = ((const CL_Binding<Base>&) o);};
  114.     
  115.     CL_Binding<Base>& operator= (const CL_Binding<Base>& o)
  116.         {_obj = o._obj; _method = o._method; return *this;};
  117.     
  118.     bool operator== (const CL_Object& o) const
  119.         {return *this == ((const CL_Binding<Base>&) o);};
  120.     // Cast the given object {\tt o} down to a Binding, and return TRUE if the
  121.     // pointers in it are the same as the pointers in this object. 
  122.     
  123.     bool operator== (const CL_Binding<Base>& o) const
  124.         { return _obj == o._obj && _method == o._method; };
  125.  
  126.     CL_Object* Clone () const { return new CL_Binding<Base> (_obj, _method);};
  127.     // Override the method inherited from {\tt CL_Object}.
  128.  
  129.     const  char* ClassName () const {return "CL_Binding";};
  130.     
  131. protected:
  132.     Base*     _obj;
  133.     MethodPtr _method;
  134. };
  135.  
  136.  
  137.  
  138.  
  139.  
  140. // #ifndef __GNUC__
  141. // // template <class Base>
  142. // // typedef bool (Base::*MethodPtr) (CL_Object&, long);
  143. // 
  144. // template <class Base>
  145. // #ifdef __GNUC__
  146. // static
  147. // #else
  148. // inline
  149. // #endif
  150. // CL_Binding<Base> MakeBinding (Base* o, CL_Binding<Base>::MethodPtr m)
  151. // {
  152. //     return CL_Binding<Base> (o, m);
  153. // }
  154. // #endif /* __GNUC__ */
  155.  
  156. inline const char* CL_AbstractBinding::ClassName() const
  157. {
  158.     return "CL_AbstractBinding";
  159. }
  160.  
  161. inline bool CL_AbstractBinding::Execute (CL_Object&, long) const
  162. {
  163.     NotImplemented ("Execute"); return FALSE;
  164. }
  165.  
  166. inline bool CL_AbstractBinding::Valid () const
  167. {
  168.     NotImplemented ("Valid"); return FALSE;
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175. #endif
  176.