delegate.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2007 by Marc Boris Duerner
3  * Copyright (C) 2005 Stephan Beal
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  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef cxxtools_Delegate_h
31 #define cxxtools_Delegate_h
32 
33 #include <cxxtools/void.h>
34 #include <cxxtools/function.h>
35 #include <cxxtools/method.h>
36 #include <cxxtools/constmethod.h>
37 #include <cxxtools/connectable.h>
38 #include <stdexcept>
39 
40 
41 namespace cxxtools {
42 
45  class DelegateBase : public Connectable
46  {
47  public:
49  { }
50 
52  : Connectable(rhs)
53  { operator=(rhs); }
54 
56  {
57  _target.close();
58 
59  if( !other._target )
60  return *this;
61 
62  const Slot& slot = other._target.slot();
63  _target = Connection( *this, slot.clone() );
64 
65  return *this;
66  }
67 
68  virtual void onConnectionOpen(const Connection& c)
69  {
70  const Connectable& sender = c.sender();
71 
72  if( &sender == this )
73  {
74  _target.close();
75  _target = c;
76  }
77 
79  }
80 
81  virtual void onConnectionClose(const Connection& c)
82  {
84  }
85 
86  bool isConnected() const
87  {
88  return _target.valid();
89  }
90 
91  protected:
93  };
94 
95 
96 #include <cxxtools/delegate.tpp>
97 
98 } // !namespace cxxtools
99 
100 #endif