]> git.lyx.org Git - lyx.git/blob - boost/boost/signals/slot.hpp
Boost 1.31.0
[lyx.git] / boost / boost / signals / slot.hpp
1 // Boost.Signals library
2
3 // Copyright Doug Gregor 2001-2003. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // For more information, see http://www.boost.org
9
10 #ifndef BOOST_SIGNALS_SLOT_HEADER
11 #define BOOST_SIGNALS_SLOT_HEADER
12
13 #include <boost/signals/detail/signals_common.hpp>
14 #include <boost/signals/connection.hpp>
15 #include <boost/signals/trackable.hpp>
16 #include <boost/visit_each.hpp>
17 #include <cassert>
18
19 #ifdef BOOST_HAS_ABI_HEADERS
20 #  include BOOST_ABI_PREFIX
21 #endif
22
23 namespace boost {
24   namespace BOOST_SIGNALS_NAMESPACE {
25     namespace detail {
26       class BOOST_SIGNALS_DECL slot_base {
27         // We would have to enumerate all of the signalN classes here as
28         // friends to make this private (as it otherwise should be). We can't
29         // name all of them because we don't know how many there are.
30       public:
31         // Get the set of bound objects
32         std::vector<const trackable*>& get_bound_objects() const
33         { return bound_objects; }
34
35         // Determine if this slot is still "active", i.e., all of the bound
36         // objects still exist
37         bool is_active() const { return watch_bound_objects.connected(); }
38
39       protected:
40         // Create a connection for this slot
41         void create_connection();
42
43         mutable std::vector<const trackable*> bound_objects;
44         connection watch_bound_objects;
45
46       private:
47         static void bound_object_destructed(void*, void*) {}
48       };
49     } // end namespace detail
50
51     // Get the slot so that it can be copied
52     template<typename F>
53     reference_wrapper<const F>
54     get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
55       { return reference_wrapper<const F>(f); }
56
57     template<typename F>
58     const F&
59     get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
60       { return f; }
61
62     template<typename F>
63     const F&
64     get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
65       { return f; }
66
67     // Get the slot so that it can be inspected for trackable objects
68     template<typename F>
69     const F&
70     get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
71       { return f; }
72
73     template<typename F>
74     const F&
75     get_inspectable_slot(const reference_wrapper<F>& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
76       { return f.get(); }
77
78     template<typename F>
79     const F&
80     get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
81       { return f; }
82
83     // Determines the type of the slot - is it a signal, a reference to a
84     // slot or just a normal slot.
85     template<typename F>
86     typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
87     tag_type(const F&)
88     {
89       typedef typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
90         the_tag_type;
91       the_tag_type tag = the_tag_type();
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 #ifdef BOOST_HAS_ABI_HEADERS
137 #  include BOOST_ABI_SUFFIX
138 #endif
139
140 #endif // BOOST_SIGNALS_SLOT_HEADER