more object lifetime management fixes, plus a couple of tiny cleanups
[ardour.git] / libs / pbd / pbd / shiva.h
1 #ifndef __pbd_shiva_h__
2 #define __pbd_shiva_h__
3
4 #include <sigc++/sigc++.h>
5
6 namespace PBD {
7
8 template<typename ObjectWithGoingAway, typename ObjectToBeDestroyed>
9
10 /* named after the Hindu god Shiva, The Destroyer */
11
12 class Shiva {
13   public:
14         Shiva (ObjectWithGoingAway& emitter, ObjectToBeDestroyed& receiver) {
15
16                 /* if the emitter goes away, destroy the receiver */
17
18                 _connection1 = emitter.GoingAway.connect 
19                         (sigc::bind (sigc::mem_fun 
20                                      (*this, &Shiva<ObjectWithGoingAway,ObjectToBeDestroyed>::destroy),
21                                      &receiver));
22
23                 /* if the receiver goes away, forget all this nonsense */
24
25                 _connection2 = receiver.GoingAway.connect 
26                         (sigc::mem_fun (*this, &Shiva<ObjectWithGoingAway,ObjectToBeDestroyed>::forget));
27         }
28
29         ~Shiva() { 
30                 forget ();
31         }
32
33   private:
34         sigc::connection _connection1;
35         sigc::connection _connection2;
36
37         void destroy (ObjectToBeDestroyed* obj) {
38                 delete obj;
39                 forget ();
40         }
41
42         void forget () {
43                 _connection1.disconnect ();
44                 _connection2.disconnect ();
45         }
46                         
47 };
48
49 }
50
51 #endif /* __pbd_shiva_h__ */