ODIN
tjhandler.h
1 /***************************************************************************
2  tjhandler.h - description
3  -------------------
4  begin : Mon Aug 19 2002
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJHANDLER_H
19 #define TJHANDLER_H
20 
21 #include <tjutils/tjutils.h>
22 #include <tjutils/tjthread.h>
23 
30 class HandlerComponent {
31  public:
32  static const char* get_compName();
33 };
34 
35 
37 
38 template<class I> class Handled; // forward declaration
39 
40 
41 
43 
44 template<class I>
45 class Handler {
46 
47 
48  public:
49 
50  Handler();
51 
52  Handler(const Handler& handler);
53 
54  Handler& operator = (const Handler& handler);
55 
56  ~Handler();
57 
58  const Handler& clear_handledobj() const;
59 
60  const Handler& set_handled(I handled) const;
61 
62  I get_handled() const;
63 
64 
65  private:
66  friend class Handled<I>;
67 
68  const Handler& handled_remove(Handled<I>* handled) const;
69 
70  mutable I handledobj;
71 
72 };
73 
74 
76 
77 
78 template<class I>
79 class Handled {
80 
81  public:
82  Handled();
83  ~Handled();
84 
85  bool is_handled() const {return bool(handlers.size());}
86 
87  private:
88  friend class Handler<I>;
89 
90  const Handled& set_handler(const Handler<I>& handler) const;
91  const Handled& erase_handler(const Handler<I>& handler) const;
92 
93  mutable STD_list< const Handler<I>* > handlers;
94 };
95 
96 
100 
101 class SingletonBase; // forward declaration
102 
103 // mapping between singletons and labels
104 typedef STD_map<STD_string, SingletonBase*> SingletonMap;
105 
106 
107 class SingletonBase {
108 
109  public:
110  virtual void* get_ptr() const = 0;
111 
112  static SingletonMap* get_singleton_map();
113  static void set_singleton_map_external(SingletonMap* extmap);
114 
115  protected:
116  SingletonBase();
117  virtual ~SingletonBase() {}
118 
119  static STD_string get_singleton_label(SingletonBase* sing_ptr);
120  static void* get_external_map_ptr(const STD_string& sing_label);
121 
122  static SingletonMap* singleton_map;
123  static SingletonMap* singleton_map_external;
124 };
125 
126 
128 
129 
133 template<class T>
134 class LockProxy {
135 
136  public:
137  LockProxy(volatile T* r, Mutex* m) : presource(const_cast<T*>(r)), pmutex(m) {if(pmutex) pmutex->lock();}
138  ~LockProxy() {if(pmutex) pmutex->unlock();}
139  T* operator -> () {return presource;}
140 
141  private:
142  T* presource;
143  Mutex* pmutex;
144 };
145 
146 
148 
149 
150 
165 template<class T, bool thread_safe> // VxWorks does not support default-template args
166 class SingletonHandler : public SingletonBase {
167 
168  public:
169 
170  SingletonHandler() {
171  // do nothing because members are already initialized by init()
172  }
173 
174 
180  void init (const char* unique_label);
181 
185  void destroy ();
186 
187 
191  void copy(T& destination) const;
192 
196  T* unlocked_ptr() {return get_map_ptr();}
197 
198 
199  // emulate pointer syntax:
200 
204  LockProxy<T> operator -> () {return LockProxy<T>(get_map_ptr(),mutex);}
205 
209  const T* operator -> () const {return get_map_ptr();}
210 
214  operator bool () const {return (bool)get_map_ptr();}
215 
216 
217 
218  private:
219 
220  // implement virtual function of SingletonBase
221  void* get_ptr() const {return ptr;}
222 
223  T* get_map_ptr() const;
224 
225  // Use pointers to avoid the need for static initialization
226  mutable T* ptr;
227  STD_string* singleton_label;
228  Mutex* mutex;
229 
230 };
231 
232 
233 
236 #endif
Definition: tjthread.h:46
void unlock()
void lock()
LockProxy< T > operator->()
Definition: tjhandler.h:204
T * unlocked_ptr()
Definition: tjhandler.h:196
void init(const char *unique_label)
void copy(T &destination) const