connection BOOST_SIGNALS_SIGNAL::connect(const slot_type& in_slot, //引发slot的构造,主要初始化存储信号源信息的结构
connect_position at)
{
using boost::BOOST_SIGNALS_NAMESPACE::detail::stored_group;
if (!in_slot.is_active())
{
return BOOST_SIGNALS_NAMESPACE::connection();
}
return impl->connect_slot(in_slot.get_slot_function(), stored_group(),
in_slot.get_data(), at); //调用了signal_base_impl的connect_slot
}
slot的构造函数简版伪码
slot(const function& f) : slot_function(get_invocable_slot(f, tag_type(f)))
{
this->data.reset(new data_t);
basic_connection* con = new basic_connection();
con->signal = static_cast<void*>(this);
con->signal_data = 0;
con->blocked_ = false ;
con->signal_disconnect = &bound_object_destructed;
data->watch_bound_objects.reset(con);
data->watch_bound_objects.set_controlling(true);
}
connection signal_base_impl::connect_slot(const any& slot_, //这里其实传入的是function
const stored_group& name,
shared_ptr<slot_base::data_t> data, //slot构造的用来存储信号源信息的结构
connect_position at)
{
data->watch_bound_objects.set_controlling(false);
scoped_connection safe_connection(data->watch_bound_objects);
std::auto_ptr<iterator> saved_iter(new iterator);
iterator pos = slots_.insert(name, data->watch_bound_objects, slot_, at); //将连接与function插入到map中
*saved_iter = pos;
data->watch_bound_objects.get_connection()->signal = this; //连接的signal
data->watch_bound_objects.get_connection()->signal_data = saved_iter.release(); //在容器中的迭代位子
data->watch_bound_objects.get_connection()->signal_disconnect = &signal_base_impl::slot_disconnected; //断开连接的函数
pos->first.set_controlling();
return safe_connection.release();
}
//map的插入,将一个连接和function组成一个pair,插入到map中
named_slot_map::iterator
named_slot_map::insert(const stored_group& name, const connection& con,
const any& slot, connect_position at)
{
group_iterator group;
if (name.empty()) {
switch (at) {
case at_front: group = groups.begin(); break;
case at_back: group = back; break;
}
} else {
group = groups.find(name);
if (group == groups.end()) {
slot_container_type::value_type v(name, group_list());
group = groups.insert(v).first;
}
}
iterator it;
it.group = group;
it.last_group = groups.end();
switch (at) {
case at_back:
group->second.push_back(connection_slot_pair(con, slot));
it.slot_ = group->second.end();
it.slot_assigned = true;
--(it.slot_);
break;
case at_front:
group->second.push_front(connection_slot_pair(con, slot));
it.slot_ = group->second.begin();
it.slot_assigned = true;
break;
}
return it;
}
连接的结构
struct basic_connection
{
void* signal; //signal对象指针,是这个connection的管理者
void* signal_data; //在signal中的slot管理器中的迭代器对象的指针
void (*signal_disconnect)(void*, void*); //解除连接的函数执政
bool blocked_;
std::list<bound_object> bound_objects;
};
class BOOST_SIGNALS_DECL connection :
private less_than_comparable1<connection>,
private equality_comparable1<connection>
{
public:
connection() : con(), controlling_connection(false) {}
connection(const connection&);
~connection();
// Block he connection: if the connection is still active, there
// will be no notification
void block(bool should_block = true) { con->blocked_ = should_block; }
void unblock() { con->blocked_ = false; }
bool blocked() const { return !connected() || con->blocked_; }
// Disconnect the signal and slot, if they are connected
void disconnect() const;
// Returns true if the signal and slot are connected
bool connected() const { return con.get() && con->signal_disconnect; }
// Comparison of connections
bool operator==(const connection& other) const;
bool operator<(const connection& other) const;
// Connection assignment
connection& operator=(const connection& other) ;
// Swap connections
void swap(connection& other);
public: // TBD: CHANGE THIS
// Set whether this connection object is controlling or not
void set_controlling(bool control = true)
{ controlling_connection = control; }
shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection>
get_connection() const
{ return con; }
private:
friend class detail::signal_base_impl;
friend class detail::slot_base;
friend class trackable;
// Reset this connection to refer to a different actual connection
void reset(BOOST_SIGNALS_NAMESPACE::detail::basic_connection*);
// Add a bound object to this connection (not for users)
void add_bound_object(const BOOST_SIGNALS_NAMESPACE::detail::bound_object& b);
friend class BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor;
// Pointer to the actual contents of the connection
shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection> con;
// True if the destruction of this connection object should disconnect
bool controlling_connection;
};
posted on 2007-04-23 15:04
walkspeed 阅读(563)
评论(0) 编辑 收藏 引用 所属分类:
boost学习