| 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 | #define BOOST_SIGNALS_SOURCE |
|---|
| 11 | |
|---|
| 12 | #include <boost/signals/detail/named_slot_map.hpp> |
|---|
| 13 | #include <cassert> |
|---|
| 14 | #include <map> |
|---|
| 15 | #include <list> |
|---|
| 16 | #include <typeinfo> |
|---|
| 17 | |
|---|
| 18 | namespace boost { namespace BOOST_SIGNALS_NAMESPACE { namespace detail { |
|---|
| 19 | |
|---|
| 20 | typedef std::list<connection_slot_pair> group_list; |
|---|
| 21 | typedef group_list::iterator slot_pair_iterator; |
|---|
| 22 | typedef std::map<stored_group, group_list, compare_type> slot_container_type; |
|---|
| 23 | typedef slot_container_type::iterator group_iterator; |
|---|
| 24 | typedef slot_container_type::const_iterator const_group_iterator; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #if BOOST_WORKAROUND(_MSC_VER, <= 1400) |
|---|
| 28 | void named_slot_map_iterator::decrement() { assert(false); } |
|---|
| 29 | void named_slot_map_iterator::advance(difference_type) { assert(false); } |
|---|
| 30 | #endif |
|---|
| 31 | |
|---|
| 32 | named_slot_map::named_slot_map(const compare_type& compare) : groups(compare) |
|---|
| 33 | { |
|---|
| 34 | clear(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | void named_slot_map::clear() |
|---|
| 38 | { |
|---|
| 39 | groups.clear(); |
|---|
| 40 | groups[stored_group(stored_group::sk_front)]; |
|---|
| 41 | groups[stored_group(stored_group::sk_back)]; |
|---|
| 42 | back = groups.end(); |
|---|
| 43 | --back; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | named_slot_map::iterator named_slot_map::begin() |
|---|
| 47 | { |
|---|
| 48 | return named_slot_map::iterator(groups.begin(), groups.end()); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | named_slot_map::iterator named_slot_map::end() |
|---|
| 52 | { |
|---|
| 53 | return named_slot_map::iterator(groups.end(), groups.end()); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | named_slot_map::iterator |
|---|
| 57 | named_slot_map::insert(const stored_group& name, const connection& con, |
|---|
| 58 | const any& slot, connect_position at) |
|---|
| 59 | { |
|---|
| 60 | group_iterator group; |
|---|
| 61 | if (name.empty()) { |
|---|
| 62 | switch (at) { |
|---|
| 63 | case at_front: group = groups.begin(); break; |
|---|
| 64 | case at_back: group = back; break; |
|---|
| 65 | } |
|---|
| 66 | } else { |
|---|
| 67 | group = groups.find(name); |
|---|
| 68 | if (group == groups.end()) { |
|---|
| 69 | slot_container_type::value_type v(name, group_list()); |
|---|
| 70 | group = groups.insert(v).first; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | iterator it; |
|---|
| 74 | it.group = group; |
|---|
| 75 | it.last_group = groups.end(); |
|---|
| 76 | |
|---|
| 77 | switch (at) { |
|---|
| 78 | case at_back: |
|---|
| 79 | group->second.push_back(connection_slot_pair(con, slot)); |
|---|
| 80 | it.slot_ = group->second.end(); |
|---|
| 81 | it.slot_assigned = true; |
|---|
| 82 | --(it.slot_); |
|---|
| 83 | break; |
|---|
| 84 | |
|---|
| 85 | case at_front: |
|---|
| 86 | group->second.push_front(connection_slot_pair(con, slot)); |
|---|
| 87 | it.slot_ = group->second.begin(); |
|---|
| 88 | it.slot_assigned = true; |
|---|
| 89 | break; |
|---|
| 90 | } |
|---|
| 91 | return it; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void named_slot_map::disconnect(const stored_group& name) |
|---|
| 95 | { |
|---|
| 96 | group_iterator group = groups.find(name); |
|---|
| 97 | if (group != groups.end()) { |
|---|
| 98 | slot_pair_iterator i = group->second.begin(); |
|---|
| 99 | while (i != group->second.end()) { |
|---|
| 100 | slot_pair_iterator next = i; |
|---|
| 101 | ++next; |
|---|
| 102 | i->first.disconnect(); |
|---|
| 103 | i = next; |
|---|
| 104 | } |
|---|
| 105 | groups.erase(group); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void named_slot_map::erase(iterator pos) |
|---|
| 110 | { |
|---|
| 111 | // Erase the slot |
|---|
| 112 | pos.slot_->first.disconnect(); |
|---|
| 113 | pos.group->second.erase(pos.slot_); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void named_slot_map::remove_disconnected_slots() |
|---|
| 117 | { |
|---|
| 118 | // Remove any disconnected slots |
|---|
| 119 | group_iterator g = groups.begin(); |
|---|
| 120 | while (g != groups.end()) { |
|---|
| 121 | slot_pair_iterator s = g->second.begin(); |
|---|
| 122 | while (s != g->second.end()) { |
|---|
| 123 | if (s->first.connected()) ++s; |
|---|
| 124 | else g->second.erase(s++); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // Clear out empty groups |
|---|
| 128 | if (empty(g)) groups.erase(g++); |
|---|
| 129 | else ++g; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | } } } |
|---|