• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List

tjhandler.h

00001 /***************************************************************************
00002                           tjhandler.h  -  description
00003                              -------------------
00004     begin                : Mon Aug 19 2002
00005     copyright            : (C) 2001 by Thies H. Jochimsen
00006     email                : jochimse@cns.mpg.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef TJHANDLER_H
00019 #define TJHANDLER_H
00020 
00021 #include <tjutils/tjutils.h>
00022 #include <tjutils/tjthread.h>
00023 
00030 class HandlerComponent {
00031  public:
00032   static const char* get_compName();
00033 };
00034 
00035 
00037 
00038 template<class I> class Handled; // forawrd declaration
00039 
00040 
00041 
00043 
00044 template<class I>
00045 class Handler {
00046 
00047 
00048  public:
00049 
00050   Handler();
00051 
00052   Handler(const Handler& handler);
00053 
00054   Handler& operator = (const Handler& handler);
00055 
00056   ~Handler();
00057 
00058   const Handler& clear_handledobj() const;
00059 
00060   const Handler& set_handled(I handled) const;
00061 
00062   I get_handled() const;
00063 
00064 
00065  private:
00066   friend class Handled<I>;
00067 
00068   const Handler& handled_remove(Handled<I>* handled) const;
00069 
00070   mutable I handledobj;
00071 
00072 };
00073 
00074 
00076 
00077 
00078 template<class I>
00079 class Handled {
00080 
00081  public:
00082   Handled();
00083   ~Handled();
00084 
00085   bool is_handled() const {return bool(handlers.size());}
00086   
00087  private:
00088   friend class Handler<I>;
00089   
00090   const Handled& set_handler(const Handler<I>& handler) const;
00091   const Handled& erase_handler(const Handler<I>& handler) const;
00092 
00093   mutable STD_list< const Handler<I>* > handlers;
00094 };
00095 
00096 
00100 
00101 class SingletonBase; // forward declaration
00102 
00103 // mapping between singletons and labels
00104 typedef STD_map<STD_string, SingletonBase*> SingletonMap;
00105 
00106 
00107 class SingletonBase {
00108 
00109  public:
00110   virtual void* get_ptr() const = 0;
00111 
00112   static SingletonMap* get_singleton_map();
00113   static void set_singleton_map_external(SingletonMap* extmap);
00114 
00115  protected:
00116   SingletonBase();
00117   virtual ~SingletonBase() {}
00118 
00119   static STD_string get_singleton_label(SingletonBase* sing_ptr);
00120   static void* get_external_map_ptr(const STD_string& sing_label);
00121 
00122   static SingletonMap* singleton_map;
00123   static SingletonMap* singleton_map_external;
00124 };
00125 
00126 
00128 
00129 
00133 template<class T>
00134 class LockProxy {
00135 
00136  public:
00137   LockProxy(volatile T* r, Mutex* m) : presource(const_cast<T*>(r)), pmutex(m) {if(pmutex) pmutex->lock();}
00138   ~LockProxy() {if(pmutex) pmutex->unlock();}
00139   T* operator -> () {return presource;}
00140 
00141  private:
00142   T* presource;
00143   Mutex* pmutex;
00144 };
00145 
00146 
00148 
00149 
00150 
00165 template<class T, bool thread_safe> // VxWorks does not support default-template args
00166 class SingletonHandler : public SingletonBase {
00167 
00168  public:
00169 
00170   SingletonHandler() {
00171     // do nothing because members are already initialized by init()
00172   }
00173 
00174 
00180   void init (const char* unique_label);
00181 
00185   void destroy ();
00186 
00187 
00191   void copy(T& destination) const;
00192 
00196   T* unlocked_ptr() {return get_map_ptr();}
00197 
00198 
00199   // emulate pointer syntax:
00200 
00204   LockProxy<T> operator -> () {return LockProxy<T>(get_map_ptr(),mutex);}
00205 
00209   const T* operator -> () const {return get_map_ptr();}
00210 
00214   operator bool () const {return (bool)get_map_ptr();}
00215 
00216 
00217 
00218  private:
00219 
00220   // implement virtual function of SingletonBase
00221   void* get_ptr() const {return ptr;}
00222 
00223   T* get_map_ptr() const;
00224 
00225   // Use pointers to avoid the need for static initialization
00226   mutable T* ptr;
00227   STD_string* singleton_label;
00228   Mutex* mutex;
00229 
00230 };
00231 
00232 
00233 
00236 #endif

Generated on Tue Dec 18 2012 15:11:15 by  doxygen 1.7.1