summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/shiva.h
blob: 5110f483324b950485c4e8517edaa817fc8ba504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef __pbd_shiva_h__
#define __pbd_shiva_h__

#include <sigc++/sigc++.h>

namespace PBD {

template<typename ObjectWithGoingAway, typename ObjectToBeDestroyed>

/* named after the Hindu god Shiva, The Destroyer */

class Shiva {
  public:
	Shiva (ObjectWithGoingAway& emitter, ObjectToBeDestroyed& receiver) {

		/* if the emitter goes away, destroy the receiver */

		_connection1 = emitter.GoingAway.connect 
			(sigc::bind (sigc::mem_fun 
				     (*this, &Shiva<ObjectWithGoingAway,ObjectToBeDestroyed>::destroy),
				     &receiver));

		/* if the receiver goes away, forget all this nonsense */

		_connection2 = receiver.GoingAway.connect 
			(sigc::mem_fun (*this, &Shiva<ObjectWithGoingAway,ObjectToBeDestroyed>::forget));
	}

	~Shiva() { 
		forget ();
	}

  private:
	sigc::connection _connection1;
	sigc::connection _connection2;

	void destroy (ObjectToBeDestroyed* obj) {
		delete obj;
		forget ();
	}

	void forget () {
		_connection1.disconnect ();
		_connection2.disconnect ();
	}
			
};

}

#endif /* __pbd_shiva_h__ */