]> git.lyx.org Git - lyx.git/blob - boost/boost/signals/slot.hpp
update boost to pre-1.30.0
[lyx.git] / boost / boost / signals / slot.hpp
1 // Boost.Signals library
2 //
3 // Copyright (C) 2001-2002 Doug Gregor (gregod@cs.rpi.edu)
4 //
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
10 //
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
13
14 // For more information, see http://www.boost.org
15
16 #ifndef BOOST_SIGNALS_SLOT_HEADER
17 #define BOOST_SIGNALS_SLOT_HEADER
18
19 #include <boost/signals/detail/signals_common.hpp>
20 #include <boost/signals/connection.hpp>
21 #include <boost/signals/trackable.hpp>
22 #include <boost/visit_each.hpp>
23 #include <cassert>
24
25 namespace boost {
26   namespace BOOST_SIGNALS_NAMESPACE {
27     namespace detail {
28       class BOOST_SIGNALS_DECL slot_base {
29         // We would have to enumerate all of the signalN classes here as
30         // friends to make this private (as it otherwise should be). We can't
31         // name all of them because we don't know how many there are.
32       public:
33         // Get the set of bound objects
34         std::vector<const trackable*>& get_bound_objects() const
35         { return bound_objects; }
36
37         // Determine if this slot is still "active", i.e., all of the bound
38         // objects still exist
39         bool is_active() const { return watch_bound_objects.connected(); }
40
41       protected:
42         // Create a connection for this slot
43         void create_connection();
44
45         mutable std::vector<const trackable*> bound_objects;
46         connection watch_bound_objects;
47
48       private:
49         static void bound_object_destructed(void*, void*) {}
50       };
51     } // end namespace detail
52
53     // Get the slot so that it can be copied
54     template<typename F>
55     reference_wrapper<const F>
56     get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
57       { return reference_wrapper<const F>(f); }
58
59     template<typename F>
60     const F&
61     get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
62       { return f; }
63
64     template<typename F>
65     const F&
66     get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
67       { return f; }
68
69     // Get the slot so that it can be inspected for trackable objects
70     template<typename F>
71     const F&
72     get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
73       { return f; }
74
75     template<typename F>
76     const F&
77     get_inspectable_slot(const reference_wrapper<F>& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
78       { return f.get(); }
79
80     template<typename F>
81     const F&
82     get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
83       { return f; }
84
85     // Determines the type of the slot - is it a signal, a reference to a
86     // slot or just a normal slot.
87     template<typename F>
88     typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
89     tag_type(const F&)
90     {
91       typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type tag;
92       return tag;
93     }
94
95   } // end namespace BOOST_SIGNALS_NAMESPACE
96
97   template<typename SlotFunction>
98   class slot : public BOOST_SIGNALS_NAMESPACE::detail::slot_base {
99   public:
100     template<typename F>
101     slot(const F& f) : slot_function(BOOST_SIGNALS_NAMESPACE::get_invocable_slot(f, BOOST_SIGNALS_NAMESPACE::tag_type(f)))
102     {
103       // Visit each of the bound objects and store them for later use
104       // An exception thrown here will allow the basic_connection to be
105       // destroyed when this goes out of scope, and no other connections
106       // have been made.
107       BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor do_bind(bound_objects);
108       visit_each(do_bind, BOOST_SIGNALS_NAMESPACE::get_inspectable_slot(f, BOOST_SIGNALS_NAMESPACE::tag_type(f)));
109
110       create_connection();
111     }
112
113 #ifdef __BORLANDC__
114     template<typename F>
115     slot(F* f) : slot_function(f)
116     {
117       create_connection();
118     }
119 #endif // __BORLANDC__
120
121     // We would have to enumerate all of the signalN classes here as friends
122     // to make this private (as it otherwise should be). We can't name all of
123     // them because we don't know how many there are.
124   public:
125     // Get the slot function to call the actual slot
126     const SlotFunction& get_slot_function() const { return slot_function; }
127
128   private:
129     slot(); // no default constructor
130     slot& operator=(const slot&); // no assignment operator
131
132     SlotFunction slot_function;
133   };
134 } // end namespace boost
135
136 #endif // BOOST_SIGNALS_SLOT_HEADER