removed libtool from pre-build process
[ardour.git] / libs / sigc++2 / sigc++ / trackable.cc
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 using namespace std;
24
25 namespace sigc
26 {
27
28 trackable::trackable()
29 : callback_list_(0)
30 {}
31
32 /* Don't copy the notification list.
33    The objects watching src don't need to be notified when the new object dies. */
34 trackable::trackable(const trackable& /*src*/)
35 : callback_list_(0)
36 {}
37
38 trackable& trackable::operator=(const trackable& src)
39 {
40   if(this != &src)
41     notify_callbacks(); //Make sure that we have finished with existing stuff before replacing it.
42   
43   return *this;
44 }
45
46 trackable::~trackable()
47 {
48   notify_callbacks();
49 }
50
51 void trackable::add_destroy_notify_callback(void* data, func_destroy_notify func) const
52 {
53   callback_list()->add_callback(data, func);
54 }
55
56 void trackable::remove_destroy_notify_callback(void* data) const
57 {
58   callback_list()->remove_callback(data);
59 }
60
61 void trackable::notify_callbacks()
62 {
63   if (callback_list_)
64     delete callback_list_; //This invokes all of the callbacks.
65
66   callback_list_ = 0;
67 }
68
69 internal::trackable_callback_list* trackable::callback_list() const
70 {
71   if (!callback_list_)
72     callback_list_ = new internal::trackable_callback_list;
73
74   return callback_list_;
75 }
76
77       
78 namespace internal
79 {
80
81 trackable_callback_list::~trackable_callback_list()
82 {
83   clearing_ = true;
84
85   for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
86     (*i).func_((*i).data_);
87 }
88
89 void trackable_callback_list::add_callback(void* data, func_destroy_notify func)
90 {
91   if (!clearing_)  // TODO: Is it okay to silently ignore attempts to add dependencies when the list is being cleared?
92                    //       I'd consider this a serious application bug, since the app is likely to segfault.
93                    //       But then, how should we handle it? Throw an exception? Martin.
94     callbacks_.push_back(trackable_callback(data, func));
95 }
96
97 void trackable_callback_list::clear()
98 {
99   clearing_ = true;
100
101   for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
102     (*i).func_((*i).data_);
103
104   callbacks_.clear();
105
106   clearing_ = false;
107 }
108
109 void trackable_callback_list::remove_callback(void* data)
110 {
111   if (clearing_) return; // No circular notices
112
113   for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
114     if ((*i).data_ == data)
115     {
116       callbacks_.erase(i);
117       return;
118     }
119 }
120
121 } /* namespace internal */
122
123
124 } /* namespace sigc */