BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Kinetic/Listener.h
Go to the documentation of this file.
00001 // Copyright (c) 2005  Stanford University (USA).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Kinetic_data_structures/include/CGAL/Kinetic/Listener.h $
00016 // $Id: Listener.h 42004 2008-02-06 18:13:32Z drussel $
00017 // 
00018 //
00019 // Author(s)     : Daniel Russel <drussel@alumni.princeton.edu>
00020 
00021 #ifndef CGAL_TOOLS_LISTENER_BASE_H
00022 #define CGAL_TOOLS_LISTENER_BASE_H
00023 #include <CGAL/Kinetic/basic.h>
00024 #include <boost/utility.hpp>
00025 CGAL_KINETIC_BEGIN_NAMESPACE
00026 
00028 
00065 template <class Interface>
00066 class Listener_base: public Interface, public boost::noncopyable
00067 {
00068   typedef Listener_base<Interface> LB_this;
00069 public:
00070   typedef typename Interface::Notifier Notifier;  
00071 
00072   Listener_base(){}
00073 
00074   virtual ~Listener_base() {
00075     if (h_ != NULL) {
00076       CGAL_precondition(h_->listener()==this);
00077       h_->set_listener(NULL);
00078     }
00079   }
00081 
00085   Notifier* notifier() {
00086     return h_.get();
00087   }
00089 
00092   const Notifier* notifier() const
00093   {
00094     return h_.get();
00095   }
00096 
00097   template <class Ptr>
00098   void set_notifier(Ptr t) {
00099     if (h_!= NULL) h_->set_listener(NULL);
00100     h_=t;
00101     CGAL_precondition(h_->listener() == NULL);
00102     h_->set_listener(this);
00103   }
00104 
00105  
00107 
00111   virtual void new_notification(typename Interface::Notification_type nt)=0;
00112 
00113   class Handle: public boost::noncopyable {
00114   public:
00115     Handle(): p_(NULL){}
00116     Handle(LB_this *p):p_(p){}
00117     LB_this &operator*(){return *p_;}
00118     const LB_this &operator*() const {return *p_;}
00119     LB_this *operator->(){return p_;}
00120     const LB_this *operator->() const {return p_;}
00121     bool operator==(void* o) const {
00122       return p_== o;
00123     }
00124     bool operator!=(void* o) const {
00125       return p_!= o;
00126     }
00127     void operator=(LB_this *o) {
00128       p_=o;
00129     }
00130     LB_this* get() {return p_;}
00131   private:
00132     
00133     LB_this *p_;
00134   };
00135   
00136 protected:
00137   typename Notifier::Handle h_;
00138 };
00139 
00140 /*
00141 #define CGAL_KINETIC_LISTENER(...) private:                     \
00142   struct Listener_core{                                         \
00143     typedef This Notifier;                                      \
00144     typedef enum {__VA_ARGS__} Notification_type;               \
00145   };                                                            \
00146   public:                                                       \
00147   typedef CGAL::Kinetic::Listener_base<Listener_core> Listener; \
00148   friend class CGAL::Kinetic::Listener_base<Listener_core>;     \
00149 private:                                                        \
00150   void set_listener(Listener *sk) {                             \
00151     listener_=sk;                                               \
00152   }                                                             \
00153   Listener* listener() {return listener_.get();}                \
00154   typename Listener::Handle listener_;
00155 
00156 
00157 #define CGAL_KINETIC_LISTENERNT(...) private:                   \
00158   struct Listener_core{                                         \
00159     typedef This Notifier;                                      \
00160     typedef enum {__VA_ARGS__} Notification_type;               \
00161   };                                                            \
00162 public:                                                         \
00163  typedef CGAL::Kinetic::Listener_base<Listener_core> Listener;  \
00164  friend class CGAL::Kinetic::Listener_base<Listener_core>;      \
00165 private:                                                        \
00166  void set_listener(Listener *sk) {                              \
00167    listener_=sk;                                                \
00168  }                                                              \
00169  Listener* listener() {return listener_.get();}                 \
00170  Listener::Handle listener_;
00171 */
00172 #define CGAL_KINETIC_LISTENER1(A) private:                      \
00173   struct Listener_core{                                         \
00174     typedef This Notifier;                                      \
00175     typedef enum {A} Notification_type;                         \
00176   };                                                            \
00177   public:                                                       \
00178   typedef CGAL::Kinetic::Listener_base<Listener_core> Listener; \
00179   friend class CGAL::Kinetic::Listener_base<Listener_core>;     \
00180 private:                                                        \
00181   void set_listener(Listener *sk) {                             \
00182     listener_=sk;                                               \
00183   }                                                             \
00184   Listener* listener() {return listener_.get();}                \
00185   typename Listener::Handle listener_;
00186 
00187 
00188 #define CGAL_KINETIC_LISTENERNT1(A) private:                    \
00189   struct Listener_core{                                         \
00190     typedef This Notifier;                                      \
00191     typedef enum {A} Notification_type;                         \
00192   };                                                            \
00193 public:                                                         \
00194  typedef CGAL::Kinetic::Listener_base<Listener_core> Listener;  \
00195  friend class CGAL::Kinetic::Listener_base<Listener_core>;      \
00196 private:                                                        \
00197  void set_listener(Listener *sk) {                              \
00198    listener_=sk;                                                \
00199  }                                                              \
00200  Listener* listener() {return listener_.get();}                 \
00201  Listener::Handle listener_;
00202 
00203 #define CGAL_KINETIC_LISTENER2(A,B) private:                    \
00204   struct Listener_core{                                         \
00205     typedef This Notifier;                                      \
00206     typedef enum {A,B} Notification_type;                       \
00207   };                                                            \
00208   public:                                                       \
00209   typedef CGAL::Kinetic::Listener_base<Listener_core> Listener; \
00210   friend class CGAL::Kinetic::Listener_base<Listener_core>;     \
00211 private:                                                        \
00212   void set_listener(Listener *sk) {                             \
00213     listener_=sk;                                               \
00214   }                                                             \
00215   Listener* listener() {return listener_.get();}                \
00216   typename Listener::Handle listener_;
00217 
00218 
00219 #define CGAL_KINETIC_LISTENERNT2(A,B) private:                  \
00220   struct Listener_core{                                         \
00221     typedef This Notifier;                                      \
00222     typedef enum {A,B} Notification_type;                       \
00223   };                                                            \
00224 public:                                                         \
00225  typedef CGAL::Kinetic::Listener_base<Listener_core> Listener;  \
00226  friend class CGAL::Kinetic::Listener_base<Listener_core>;      \
00227 private:                                                        \
00228  void set_listener(Listener *sk) {                              \
00229    listener_=sk;                                                \
00230  }                                                              \
00231  Listener* listener() {return listener_.get();}                 \
00232  Listener::Handle listener_;
00233 
00234 #define CGAL_KINETIC_NOTIFY(field) if (listener_!= NULL) listener_->new_notification(Listener::field)
00235 
00236 #define CGAL_KINETIC_LISTENER_DESTRUCTOR CGAL_assertion(listener_==NULL);
00237 
00238 
00239 #define CGAL_KINETIC_LISTENER_BASICS(Name, KDS)         \
00240   public:                                               \
00241   Name(): recipient_(NULL){}                            \
00242   typedef KDS Recipient;                                \
00243   Recipient* recipient() const {return recipient_;}     \
00244   void set_recipient(Recipient *r){recipient_=r;}       \
00245 private:                                                \
00246  Recipient* recipient_;
00247 
00248 
00249 
00250 
00251 #define CGAL_KINETIC_LISTEN1(Notifier, NOTIF, function)\
00252   private:                                                              \
00253   class Notifier##_listener: public Notifier::Listener {                \
00254     CGAL_KINETIC_LISTENER_BASICS(Notifier##_listener, This);            \
00255   public:                                                               \
00256     virtual void new_notification(typename Notifier::Listener::Notification_type t) { \
00257       if (recipient() != NULL && t== Notifier::Listener::NOTIF) recipient()->function; \
00258       else {                                                            \
00259       }                                                                 \
00260     }                                                                   \
00261   };                                                            \
00262   friend class Notifier##_listener;                                     \
00263   Notifier##_listener listener_##Notifier##_;
00264 
00265 #define CGAL_KINETIC_LISTEN2(Notifier, NOTIF, function, NOTIF2, function2) \
00266   private:                                                              \
00267   class Notifier##_listener: public Notifier::Listener {                \
00268     CGAL_KINETIC_LISTENER_BASICS(Notifier##_listener, This);            \
00269   public:                                                               \
00270     virtual void new_notification(typename Notifier::Listener::Notification_type t) { \
00271       if (recipient()== NULL) return;                                   \
00272       if (t== Notifier::Listener::NOTIF)        recipient()->function();        \
00273       else if (t== Notifier::Listener::NOTIF2)  recipient()->function2; \
00274       else {                                                            \
00275       }                                                                 \
00276     }                                                                   \
00277   };                                                            \
00278   friend class Notifier##_listener;                                     \
00279   Notifier##_listener listener_##Notifier##_;
00280 
00281 #define CGAL_KINETIC_NOTIFIER(Notifier) listener_##Notifier##_.notifier()
00282 
00283 #define CGAL_KINETIC_INIT_LISTEN(Notifier, ptr) \
00284   listener_##Notifier##_.set_recipient(this);   \
00285   listener_##Notifier##_.set_notifier(ptr);
00286   
00287 
00288   
00289 CGAL_KINETIC_END_NAMESPACE
00290 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines