~nux-team/nuxplayground/nux-visualstudio

« back to all changes in this revision

Viewing changes to libsigc++/sigc++/functors/slot_base.cc

  • Committer: Jay Taoko
  • Date: 2012-07-12 21:09:46 UTC
  • Revision ID: jay.taoko@canonical.com-20120712210946-5xpkonw92ivp1xl0
VisualStudio build files for Nux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- c++ -*-
 
2
/*
 
3
 * Copyright 2003, 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++/functors/slot_base.h>
 
22
 
 
23
namespace sigc
 
24
{
 
25
 
 
26
namespace internal {
 
27
 
 
28
// only MSVC needs this to guarantee that all new/delete are executed from the DLL module
 
29
#ifdef SIGC_NEW_DELETE_IN_LIBRARY_ONLY
 
30
void* slot_rep::operator new(size_t size_)
 
31
{
 
32
  return malloc(size_);
 
33
}
 
34
 
 
35
void slot_rep::operator delete(void* p)
 
36
{
 
37
  free(p);
 
38
}
 
39
#endif
 
40
 
 
41
void slot_rep::disconnect()
 
42
{
 
43
  if (parent_)
 
44
  {
 
45
    call_ = 0;          // Invalidate the slot.
 
46
                        // _Must_ be done here because parent_ might defer the actual
 
47
                        // destruction of the slot_rep and try to invoke it before that point.
 
48
    void* data_ = parent_;
 
49
    parent_ = 0;        // Just a precaution.
 
50
    (cleanup_)(data_);  // Notify the parent (might lead to destruction of this!).
 
51
  }
 
52
  else
 
53
    call_ = 0;
 
54
}
 
55
 
 
56
//static
 
57
void* slot_rep::notify(void* data)
 
58
{
 
59
  struct destroy_notify_struct
 
60
  {
 
61
    destroy_notify_struct() : deleted_(false) { }
 
62
 
 
63
    static void* notify(void* data)
 
64
    {
 
65
      destroy_notify_struct* self_ = reinterpret_cast<destroy_notify_struct*>(data);
 
66
      self_->deleted_ = true;
 
67
      return 0;
 
68
    }
 
69
 
 
70
    bool deleted_;
 
71
  };
 
72
 
 
73
  slot_rep* self_ = reinterpret_cast<slot_rep*>(data);
 
74
 
 
75
  self_->call_ = 0; // Invalidate the slot.
 
76
  
 
77
  // Make sure we are notified if disconnect() deletes self_, which is trackable.
 
78
  destroy_notify_struct notifier;
 
79
  self_->add_destroy_notify_callback(&notifier, destroy_notify_struct::notify);
 
80
  self_->disconnect(); // Disconnect the slot (might lead to deletion of self_!).
 
81
  // If self_ has been deleted, the destructor has called destroy().
 
82
  if (!notifier.deleted_)
 
83
  {
 
84
    self_->remove_destroy_notify_callback(&notifier);
 
85
    self_->destroy(); // Detach the stored functor from the other referred trackables and destroy it.
 
86
                      // destroy() might lead to deletion of self_. Bug #564005.
 
87
  }
 
88
  return 0;
 
89
}
 
90
 
 
91
} // namespace internal
 
92
  
 
93
slot_base::slot_base()
 
94
: rep_(0),
 
95
  blocked_(false)
 
96
{}
 
97
 
 
98
slot_base::slot_base(rep_type* rep)
 
99
: rep_(rep),
 
100
  blocked_(false)
 
101
{}
 
102
 
 
103
slot_base::slot_base(const slot_base& src)
 
104
: rep_(0),
 
105
  blocked_(src.blocked_)
 
106
{
 
107
  if (src.rep_)
 
108
  {
 
109
    //Check call_ so we can ignore invalidated slots.
 
110
    //Otherwise, destroyed bound reference parameters (whose destruction caused the slot's invalidation) may be used during dup().
 
111
    //Note: I'd prefer to check somewhere during dup(). murrayc.
 
112
    if (src.rep_->call_)
 
113
      rep_ = src.rep_->dup();
 
114
    else
 
115
    {
 
116
      *this = slot_base(); //Return the default invalid slot.
 
117
    }
 
118
  }
 
119
}
 
120
 
 
121
slot_base::~slot_base()
 
122
{
 
123
  if (rep_)
 
124
    delete rep_;
 
125
}
 
126
 
 
127
slot_base::operator bool() const
 
128
{
 
129
  return rep_ != 0;
 
130
}
 
131
 
 
132
slot_base& slot_base::operator=(const slot_base& src)
 
133
{
 
134
  if (src.rep_ == rep_) return *this;
 
135
 
 
136
  if (src.empty())
 
137
  {
 
138
    disconnect();
 
139
    return *this;
 
140
  }
 
141
 
 
142
  internal::slot_rep* new_rep_ = src.rep_->dup();
 
143
 
 
144
  if (rep_)               // Silently exchange the slot_rep.
 
145
  {
 
146
    new_rep_->set_parent(rep_->parent_, rep_->cleanup_);
 
147
    delete rep_;
 
148
  }
 
149
 
 
150
  rep_ = new_rep_;
 
151
 
 
152
  return *this;
 
153
}
 
154
 
 
155
void slot_base::set_parent(void* parent, void* (*cleanup)(void*)) const
 
156
{
 
157
  if (rep_)
 
158
    rep_->set_parent(parent, cleanup);
 
159
}
 
160
 
 
161
void slot_base::add_destroy_notify_callback(void* data, func_destroy_notify func) const
 
162
{
 
163
  if (rep_)
 
164
    rep_->add_destroy_notify_callback(data, func);
 
165
}
 
166
 
 
167
void slot_base::remove_destroy_notify_callback(void* data) const
 
168
{
 
169
  if (rep_)
 
170
    rep_->remove_destroy_notify_callback(data);
 
171
}
 
172
 
 
173
bool slot_base::block(bool should_block)
 
174
{
 
175
  bool old = blocked_;
 
176
  blocked_ = should_block;
 
177
  return old;
 
178
}
 
179
 
 
180
bool slot_base::unblock()
 
181
{
 
182
  return block(false);
 
183
}
 
184
 
 
185
void slot_base::disconnect()
 
186
{
 
187
  if (rep_)
 
188
    rep_->disconnect();
 
189
}
 
190
 
 
191
 
 
192
/*bool slot_base::empty() const // having this function not inline is killing performance !!!
 
193
{
 
194
  if (rep_ && !rep_->call_)
 
195
    {
 
196
      delete rep_;        // This is not strictly necessary here. I'm convinced that it is
 
197
      rep_ = 0;           // safe to wait for the destructor to delete the slot_rep. Martin.
 
198
    }
 
199
  return (rep_ == 0);
 
200
}*/
 
201
 
 
202
} //namespace sigc