1 // Boost.Signals library
3 // Copyright (C) 2001-2002 Doug Gregor (gregod@cs.rpi.edu)
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.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
16 #ifndef BOOST_SIGNALS_SLOT_HEADER
17 #define BOOST_SIGNALS_SLOT_HEADER
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>
26 namespace BOOST_SIGNALS_NAMESPACE {
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.
33 // Get the set of bound objects
34 std::vector<const trackable*>& get_bound_objects() const
35 { return bound_objects; }
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(); }
42 // Create a connection for this slot
43 void create_connection();
45 mutable std::vector<const trackable*> bound_objects;
46 connection watch_bound_objects;
49 static void bound_object_destructed(void*, void*) {}
51 } // end namespace detail
53 // Get the slot so that it can be copied
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); }
61 get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
66 get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
69 // Get the slot so that it can be inspected for trackable objects
72 get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
77 get_inspectable_slot(const reference_wrapper<F>& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
82 get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
85 // Determines the type of the slot - is it a signal, a reference to a
86 // slot or just a normal slot.
88 typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
91 typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type tag;
95 } // end namespace BOOST_SIGNALS_NAMESPACE
97 template<typename SlotFunction>
98 class slot : public BOOST_SIGNALS_NAMESPACE::detail::slot_base {
101 slot(const F& f) : slot_function(BOOST_SIGNALS_NAMESPACE::get_invocable_slot(f, BOOST_SIGNALS_NAMESPACE::tag_type(f)))
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
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)));
115 slot(F* f) : slot_function(f)
119 #endif // __BORLANDC__
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.
125 // Get the slot function to call the actual slot
126 const SlotFunction& get_slot_function() const { return slot_function; }
129 slot(); // no default constructor
130 slot& operator=(const slot&); // no assignment operator
132 SlotFunction slot_function;
134 } // end namespace boost
136 #endif // BOOST_SIGNALS_SLOT_HEADER