2 Copyright (C) 2000-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __pbd_shiva_h__
21 #define __pbd_shiva_h__
23 #include <sigc++/sigc++.h>
27 /* named after the Hindu god Shiva, The Destroyer */
29 template<typename ObjectWithGoingAway, typename ObjectToBeDestroyed>
32 Shiva (ObjectWithGoingAway& emitter, ObjectToBeDestroyed& receiver) {
34 /* if the emitter goes away, destroy the receiver */
36 _connection = emitter.GoingAway.connect
37 (sigc::bind (sigc::mem_fun
38 (*this, &Shiva<ObjectWithGoingAway,ObjectToBeDestroyed>::destroy),
47 sigc::connection _connection;
49 void destroy (ObjectToBeDestroyed* obj) {
55 _connection.disconnect ();
60 template<typename ObjectWithGoingAway, typename ObjectToBeDestroyed>
63 ProxyShiva (ObjectWithGoingAway& emitter, ObjectToBeDestroyed& receiver, void (*callback)(ObjectToBeDestroyed*, ObjectWithGoingAway*)) {
65 /* if the emitter goes away, destroy the receiver */
68 _callback_argument1 = &receiver;
69 _callback_argument2 = &emitter;
71 _connection = emitter.GoingAway.connect
72 (sigc::bind (sigc::mem_fun
73 (*this, &ProxyShiva<ObjectWithGoingAway,ObjectToBeDestroyed>::destroy),
82 sigc::connection _connection;
83 void (*_callback) (ObjectToBeDestroyed*, ObjectWithGoingAway*);
84 ObjectToBeDestroyed* _callback_argument1;
85 ObjectWithGoingAway* _callback_argument2;
87 void destroy (ObjectToBeDestroyed* obj) {
88 /* callback must destroy obj if appropriate, not done here */
89 _callback (obj, _callback_argument2);
94 _connection.disconnect ();
98 template<typename ObjectWithGoingAway, typename ObjectToBeDestroyed>
101 PairedShiva (ObjectWithGoingAway& emitter, ObjectToBeDestroyed& receiver) {
103 /* if the emitter goes away, destroy the receiver */
105 _connection1 = emitter.GoingAway.connect
106 (sigc::bind (sigc::mem_fun
107 (*this, &PairedShiva<ObjectWithGoingAway,ObjectToBeDestroyed>::destroy),
110 /* if the receiver goes away, forget all this nonsense */
112 _connection2 = receiver.GoingAway.connect
113 (sigc::mem_fun (*this, &PairedShiva<ObjectWithGoingAway,ObjectToBeDestroyed>::forget));
121 sigc::connection _connection1;
122 sigc::connection _connection2;
124 void destroy (ObjectToBeDestroyed* obj) {
130 _connection1.disconnect ();
131 _connection2.disconnect ();
138 #endif /* __pbd_shiva_h__ */