ODIN
tjhandler_code.h
1 #include <tjutils/tjhandler.h>
2 #include <tjutils/tjlog.h>
3 
4 
6 
7 template<class I>
8 Handled<I>::Handled() {}
9 
10 template<class I>
11  Handled<I>::~Handled() {
12  Log<HandlerComponent> odinlog("Handled","~Handled");
13  for(typename STD_list< const Handler<I>* >::const_iterator it=handlers.begin(); it!=handlers.end(); ++it) {
14  (*it)->handled_remove(this); // do not call L::remove here because it will modify handlers list
15  }
16  }
17 
18 
19 template<class I>
20  const Handled<I>& Handled<I>::set_handler(const Handler<I>& handler) const {
21  handlers.push_back(&handler);
22  return *this;
23  }
24 
25 template<class I>
26  const Handled<I>& Handled<I>::erase_handler(const Handler<I>& handler) const {
27  handlers.remove(&handler);
28  return *this;
29  }
30 
31 
33 
34 
35 template<class I>
36  Handler<I>::Handler() {
37  handledobj=0;
38  }
39 
40 template<class I>
41  Handler<I>::Handler(const Handler& handler) {
42  Handler<I>::operator = (handler);
43  }
44 
45 template<class I>
46  Handler<I>& Handler<I>::operator = (const Handler& handler) {
47  clear_handledobj();
48  I hd=handler.get_handled();
49  if(hd) set_handled(hd);
50  return *this;
51  }
52 
53 
54 template<class I>
55  Handler<I>::~Handler() {
56  Log<HandlerComponent> odinlog("Handler","~Handler");
57  clear_handledobj();
58  }
59 
60 template<class I>
61  const Handler<I>& Handler<I>::clear_handledobj() const {
62  Log<HandlerComponent> odinlog("Handler","clear_handledobj");
63  ODINLOG(odinlog,normalDebug) << "handledobj=" << (void*)handledobj << STD_endl;
64  if(handledobj) handledobj->Handled<I>::erase_handler(*this);
65  handledobj=0;
66  return *this;
67  }
68 
69 template<class I>
70  const Handler<I>& Handler<I>::set_handled(I handled) const {
71  Log<HandlerComponent> odinlog("Handler","set_handled");
72  ODINLOG(odinlog,normalDebug) << "handled=" << (void*)handled << STD_endl;
73  clear_handledobj();
74  handled->Handled<I>::set_handler(*this);
75  handledobj=handled;
76  return *this;
77  }
78 
79 
80 
81 template<class I>
82  I Handler<I>::get_handled() const {
83  return handledobj;
84  }
85 
86 
87 template<class I>
88  const Handler<I>& Handler<I>::handled_remove(Handled<I>* handled) const {
89  Log<HandlerComponent> odinlog("Handler","handled_remove");
90  I handledtype=static_cast<I>(handled);
91  ODINLOG(odinlog,normalDebug) << "handledtype=" << (void*)handledtype << STD_endl;
92  if(handledtype) handledobj=0;
93  else ODINLOG(odinlog,errorLog) << "Unable to remove handled!" << STD_endl;
94  return *this;
95  }
96 
100 
101 
102 
103 template<class T, bool thread_safe>
104  void SingletonHandler<T,thread_safe>::init (const char* unique_label) {
105  // NOTE: Debug uses SingletonHandler -> do not use Debug in here
106  singleton_label=new STD_string;
107  mutex=0;
108  if(thread_safe) mutex=new Mutex();
109  (*singleton_label)=unique_label;
110  if(!get_external_map_ptr(unique_label)) {
111  ptr=new T;
112  ptr->set_label(unique_label);
113  (*get_singleton_map())[unique_label]=this; // make sure singleton_map is allocated
114  } else {
115  ptr=0;
116  }
117  }
118 
119 template<class T, bool thread_safe>
121  if(ptr) {delete ptr; ptr=0;}
122  delete singleton_label;
123  if(mutex) delete mutex;
124  }
125 
126 template<class T, bool thread_safe>
127  void SingletonHandler<T,thread_safe>::copy(T& destination) const {
128  T* p=get_map_ptr();
129  if(p) destination=(*p);
130  }
131 
132 
133 template<class T, bool thread_safe>
135  if(ptr) return ptr; // fast return without touching the map
136  if(singleton_map_external) {
137  T* ext_ptr=(T*)get_external_map_ptr(*singleton_label);
138  if(ext_ptr) ptr=ext_ptr;
139  }
140  return ptr;
141  }
Definition: tjlog.h:218
Definition: tjthread.h:46
void init(const char *unique_label)
void copy(T &destination) const