3 * Copyright 2002, The libsigc++ Development Team
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sigc++/trackable.h>
24 SIGC_USING_STD(ostream)
31 trackable::trackable()
35 /* Don't copy the notification list.
36 The objects watching src don't need to be notified when the new object dies. */
37 trackable::trackable(const trackable& /*src*/)
41 trackable& trackable::operator=(const trackable& src)
44 notify_callbacks(); //Make sure that we have finished with existing stuff before replacing it.
49 trackable::~trackable()
54 void trackable::add_destroy_notify_callback(void* data, func_destroy_notify func) const
56 callback_list()->add_callback(data, func);
59 void trackable::remove_destroy_notify_callback(void* data) const
61 callback_list()->remove_callback(data);
64 void trackable::notify_callbacks()
67 delete callback_list_; //This invokes all of the callbacks.
72 internal::trackable_callback_list* trackable::callback_list() const
75 callback_list_ = new internal::trackable_callback_list;
77 return callback_list_;
84 trackable_callback_list::~trackable_callback_list()
88 for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
89 (*i).func_((*i).data_);
92 void trackable_callback_list::add_callback(void* data, func_destroy_notify func)
94 if (!clearing_) // TODO: Is it okay to silently ignore attempts to add dependencies when the list is being cleared?
95 // I'd consider this a serious application bug, since the app is likely to segfault.
96 // But then, how should we handle it? Throw an exception? Martin.
97 callbacks_.push_back(trackable_callback(data, func));
100 void trackable_callback_list::clear()
104 for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
105 (*i).func_((*i).data_);
112 void trackable_callback_list::remove_callback(void* data)
114 if (clearing_) return; // No circular notices
116 for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
117 if ((*i).data_ == data)
124 } /* namespace internal */
127 } /* namespace sigc */