1 // -*- c++ -*-
2 /*
3  * Copyright 2002, The libsigc++ Development Team
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20 
21 #include <sigc++/trackable.h>
22 #include <iostream>
23 
24 SIGC_USING_STD(ostream)
25 
26 using namespace std;
27 
28 namespace sigc
29 {
30 
trackable()31 trackable::trackable()
32 : callback_list_(0)
33 {}
34 
35 /* Don't copy the notification list.
36    The objects watching src don't need to be notified when the new object dies. */
trackable(const trackable &)37 trackable::trackable(const trackable& /*src*/)
38 : callback_list_(0)
39 {}
40 
operator =(const trackable & src)41 trackable& trackable::operator=(const trackable& src)
42 {
43   if(this != &src)
44     notify_callbacks(); //Make sure that we have finished with existing stuff before replacing it.
45 
46   return *this;
47 }
48 
~trackable()49 trackable::~trackable()
50 {
51   notify_callbacks();
52 }
53 
add_destroy_notify_callback(void * data,func_destroy_notify func) const54 void trackable::add_destroy_notify_callback(void* data, func_destroy_notify func) const
55 {
56   callback_list()->add_callback(data, func);
57 }
58 
remove_destroy_notify_callback(void * data) const59 void trackable::remove_destroy_notify_callback(void* data) const
60 {
61   callback_list()->remove_callback(data);
62 }
63 
notify_callbacks()64 void trackable::notify_callbacks()
65 {
66   if (callback_list_)
67     delete callback_list_; //This invokes all of the callbacks.
68 
69   callback_list_ = 0;
70 }
71 
callback_list() const72 internal::trackable_callback_list* trackable::callback_list() const
73 {
74   if (!callback_list_)
75     callback_list_ = new internal::trackable_callback_list;
76 
77   return callback_list_;
78 }
79 
80 
81 namespace internal
82 {
83 
~trackable_callback_list()84 trackable_callback_list::~trackable_callback_list()
85 {
86   clearing_ = true;
87 
88   for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
89     (*i).func_((*i).data_);
90 }
91 
add_callback(void * data,func_destroy_notify func)92 void trackable_callback_list::add_callback(void* data, func_destroy_notify func)
93 {
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));
98 }
99 
clear()100 void trackable_callback_list::clear()
101 {
102   clearing_ = true;
103 
104   for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
105     (*i).func_((*i).data_);
106 
107   callbacks_.clear();
108 
109   clearing_ = false;
110 }
111 
remove_callback(void * data)112 void trackable_callback_list::remove_callback(void* data)
113 {
114   if (clearing_) return; // No circular notices
115 
116   for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
117     if ((*i).data_ == data)
118     {
119       callbacks_.erase(i);
120       return;
121     }
122 }
123 
124 } /* namespace internal */
125 
126 
127 } /* namespace sigc */
128