]> git.lyx.org Git - lyx.git/blob - boost/boost/signals/slot.hpp
update
[lyx.git] / boost / boost / signals / slot.hpp
1 // Boost.Signals library
2 //
3 // Copyright (C) 2001 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 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         // Get the slot so that it can be copied
46         template<typename F> 
47         reference_wrapper<const F> 
48         get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
49           { return reference_wrapper<const F>(f); }
50
51         template<typename F> 
52         const F& get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
53           { return f; }
54
55         template<typename F>
56         const F& get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
57           { return f; }
58
59         // Get the slot so that it can be inspected for trackable objects
60         template<typename F> 
61         const F& get_inspectable_slot(const F& f,
62                                       BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
63           { return f; }
64
65         template<typename F> 
66         const F& get_inspectable_slot(const F& f, 
67                                       BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
68           { return f.get(); }
69
70         template<typename F>
71         const F& get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
72           { return f; }
73
74         // Determines the type of the slot - is it a signal, a reference to a
75         // slot or just a normal slot.
76         template<typename F>
77         typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
78         tag_type(const F&)
79         {
80           typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type tag;
81           return tag;
82         }
83
84         mutable std::vector<const trackable*> bound_objects;
85         connection watch_bound_objects;
86
87       private:
88         static void bound_object_destructed(void*, void*) {}
89       };
90     } // end namespace detail
91   } // end namespace BOOST_SIGNALS_NAMESPACE
92
93   template<typename SlotFunction>
94   class slot : public BOOST_SIGNALS_NAMESPACE::detail::slot_base {
95   public:
96     template<typename F> 
97     slot(const F& f) : slot_function(get_invocable_slot(f, tag_type(f)))
98     {
99       // Visit each of the bound objects and store them for later use
100       // An exception thrown here will allow the basic_connection to be
101       // destroyed when this goes out of scope, and no other connections
102       // have been made.
103       BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor do_bind(bound_objects);
104       visit_each(do_bind, get_inspectable_slot(f, tag_type(f)));
105
106       create_connection();
107     }
108
109 #ifdef __BORLANDC__
110     template<typename F>
111     slot(F* f) : slot_function(f)
112     {
113       create_connection();
114     }
115 #endif // __BORLANDC__
116
117     // We would have to enumerate all of the signalN classes here as friends
118     // to make this private (as it otherwise should be). We can't name all of
119     // them because we don't know how many there are.
120   public: 
121     // Get the slot function to call the actual slot
122     const SlotFunction& get_slot_function() const { return slot_function; }
123
124   private:
125     slot(); // no default constructor
126     slot& operator=(const slot&); // no assignment operator
127
128     SlotFunction slot_function;
129   };
130 } // end namespace boost
131
132 #endif // BOOST_SIGNALS_SLOT_HEADER