summaryrefslogtreecommitdiff
path: root/libs/sigc++2/sigc++/trackable.cc
blob: 46e2592ffb29dad1dfdd6fff205b46cc4d55797a (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// -*- c++ -*-
/*
 * Copyright 2002, The libsigc++ Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <sigc++/trackable.h>
#include <iostream>
using namespace std;

namespace sigc
{

trackable::trackable()
: callback_list_(0)
{}

/* Don't copy the notification list.
   The objects watching src don't need to be notified when the new object dies. */
trackable::trackable(const trackable& /*src*/)
: callback_list_(0)
{}

trackable& trackable::operator=(const trackable& src)
{
  if(this != &src)
    notify_callbacks(); //Make sure that we have finished with existing stuff before replacing it.
  
  return *this;
}

trackable::~trackable()
{
  notify_callbacks();
}

void trackable::add_destroy_notify_callback(void* data, func_destroy_notify func) const
{
  callback_list()->add_callback(data, func);
}

void trackable::remove_destroy_notify_callback(void* data) const
{
  callback_list()->remove_callback(data);
}

void trackable::notify_callbacks()
{
  if (callback_list_)
    delete callback_list_; //This invokes all of the callbacks.

  callback_list_ = 0;
}

internal::trackable_callback_list* trackable::callback_list() const
{
  if (!callback_list_)
    callback_list_ = new internal::trackable_callback_list;

  return callback_list_;
}

      
namespace internal
{

trackable_callback_list::~trackable_callback_list()
{
  clearing_ = true;

  for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
    (*i).func_((*i).data_);
}

void trackable_callback_list::add_callback(void* data, func_destroy_notify func)
{
  if (!clearing_)  // TODO: Is it okay to silently ignore attempts to add dependencies when the list is being cleared?
                   //       I'd consider this a serious application bug, since the app is likely to segfault.
                   //       But then, how should we handle it? Throw an exception? Martin.
    callbacks_.push_back(trackable_callback(data, func));
}

void trackable_callback_list::clear()
{
  clearing_ = true;

  for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
    (*i).func_((*i).data_);

  callbacks_.clear();

  clearing_ = false;
}

void trackable_callback_list::remove_callback(void* data)
{
  if (clearing_) return; // No circular notices

  for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
    if ((*i).data_ == data)
    {
      callbacks_.erase(i);
      return;
    }
}

} /* namespace internal */


} /* namespace sigc */