source: NonGTP/Boost/boost/signals/detail/named_slot_map.hpp @ 857

Revision 857, 5.0 KB checked in by igarcia, 19 years ago (diff)
Line 
1// Boost.Signals library
2
3// Copyright Douglas Gregor 2001-2004. 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_NAMED_SLOT_MAP_HPP
11#define BOOST_SIGNALS_NAMED_SLOT_MAP_HPP
12
13#include <boost/signals/detail/config.hpp>
14#include <boost/signals/detail/signals_common.hpp>
15#include <boost/signals/connection.hpp>
16#include <boost/utility.hpp>
17#include <boost/shared_ptr.hpp>
18#include <boost/function/function2.hpp>
19#include <boost/iterator/iterator_facade.hpp>
20#include <map>
21#include <memory>
22#include <utility>
23
24namespace boost { namespace BOOST_SIGNALS_NAMESPACE {
25
26enum connect_position { at_back, at_front };
27
28namespace detail {
29
30class stored_group
31{
32 public:
33  enum storage_kind { sk_empty, sk_front, sk_back, sk_group };
34
35  stored_group(storage_kind kind = sk_empty) : kind(kind), group() { }
36
37  template<typename T>
38  stored_group(const T& group) : kind(sk_group), group(new T(group)) { }
39
40  bool is_front() const { return kind == sk_front; }
41  bool is_back() const { return kind == sk_back; }
42  bool empty() const { return kind == sk_empty; }
43
44  void* get() const { return group.get(); }
45
46 private:
47  storage_kind kind;
48  shared_ptr<void> group;
49};
50
51typedef function2<bool, stored_group, stored_group> compare_type;
52
53// This function object bridges from a pair of any objects that hold
54// values of type Key to the underlying function object that compares
55// values of type Key.
56template<typename Compare, typename Key>
57class group_bridge_compare {
58public:
59  typedef bool result_type;
60  typedef const stored_group& first_argument_type;
61  typedef const stored_group& second_argument_type;
62
63  group_bridge_compare(const Compare& c) : comp(c) {}
64
65  bool operator()(const stored_group& k1, const stored_group& k2) const
66  {
67    if (k1.is_front()) return !k2.is_front();
68    if (k1.is_back()) return false;
69    if (k2.is_front()) return false;
70    if (k2.is_back()) return true;
71
72    // Neither is empty, so compare their values to order them
73    return comp(*static_cast<Key*>(k1.get()), *static_cast<Key*>(k2.get()));
74  }
75
76private:
77  Compare comp;
78};
79
80class BOOST_SIGNALS_DECL named_slot_map_iterator :
81  public iterator_facade<named_slot_map_iterator,
82                         connection_slot_pair,
83                         forward_traversal_tag>
84{
85  typedef std::list<connection_slot_pair> group_list;
86  typedef group_list::iterator slot_pair_iterator;
87  typedef std::map<stored_group, group_list, compare_type> slot_container_type;
88  typedef slot_container_type::iterator group_iterator;
89  typedef slot_container_type::const_iterator const_group_iterator;
90
91  typedef iterator_facade<named_slot_map_iterator,
92                          connection_slot_pair,
93                          forward_traversal_tag> inherited;
94public:
95  named_slot_map_iterator();
96  named_slot_map_iterator(const named_slot_map_iterator&);
97  named_slot_map_iterator& operator=(const named_slot_map_iterator&);
98
99  connection_slot_pair& dereference() const;
100  void increment();
101  bool equal(const named_slot_map_iterator& other) const;
102
103#if BOOST_WORKAROUND(_MSC_VER, <= 1400)
104  void decrement();
105  void advance(difference_type);
106#endif
107
108private:
109  named_slot_map_iterator(group_iterator group, group_iterator last) :
110    group(group), last_group(last), slot_assigned(false)
111  { init_next_group(); }
112  named_slot_map_iterator(group_iterator group, group_iterator last,
113                          slot_pair_iterator slot) :
114    group(group), last_group(last), slot_(slot), slot_assigned(true)
115  { }
116
117  void init_next_group()
118  {
119    while (group != last_group && group->second.empty()) ++group;
120    if (group != last_group) {
121      slot_ = group->second.begin();
122      slot_assigned = true;
123    }
124  }
125
126  group_iterator group;
127  group_iterator last_group;
128  slot_pair_iterator slot_;
129  bool slot_assigned;
130
131  friend class named_slot_map;
132};
133
134class BOOST_SIGNALS_DECL named_slot_map
135{
136public:
137  typedef named_slot_map_iterator iterator;
138
139  named_slot_map(const compare_type& compare);
140
141  void clear();
142  iterator begin();
143  iterator end();
144  iterator insert(const stored_group& name, const connection& con,
145                  const any& slot, connect_position at);
146  void disconnect(const stored_group& name);
147  void erase(iterator pos);
148  void remove_disconnected_slots();
149
150private:
151  typedef std::list<connection_slot_pair> group_list;
152  typedef std::map<stored_group, group_list, compare_type> slot_container_type;
153  typedef slot_container_type::iterator group_iterator;
154  typedef slot_container_type::const_iterator const_group_iterator;
155
156  bool empty(const_group_iterator group) const
157  {
158    return (group->second.empty() && group != groups.begin() && group != back);
159  }
160  slot_container_type groups;
161  group_iterator back;
162};
163
164} } }
165
166#endif // BOOST_SIGNALS_NAMED_SLOT_MAP_HPP
Note: See TracBrowser for help on using the repository browser.