升压:: signals2 - 对象的descruction与槽 [英] Boost::signals2 - descruction of an object with the slot

查看:295
本文介绍了升压:: signals2 - 对象的descruction与槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下:

#include <boost/signals2.hpp>
#include <iostream>

struct object_with_slot
{
void operator()()
{
   std::cout << "Slot called!" << std::endl;
   member = 50500;
}
int member;
};


int main()
{
boost::signals2::signal<void ()> sig;

object_with_slot * ptr = new object_with_slot;
sig.connect(*ptr);

delete ptr;

sig();
}

输出为槽叫!无碰撞或任何东西。这就是为什么我有几个问题:

Output is "Slot called!" and no crash or anything. That's why I have a few questions:

1)为什么没有崩溃?

1) Why there is no crash?

2)为什么有即使槽函数分配一些对象不存在不崩溃?

2) Why there is no crash even if the slot function assigns something to object which doesn't exist?

3)如何让自动信号跟踪其槽的寿命?我的意思是,当槽被破坏,它就会断开。

3) How can I make the signal automatically track the lifetime of its slots? I mean when the slot is destroyed, it gets disconnected.

问题3号是最重要的,因为我需要实现观察者模式,而且往往观察员(插槽),一辈子都不会是静态的(因为当应用程序正在运行的全部时间)。

The question number 3 is the most important, as I need to implement observer pattern and very often lifetime of observers (slots) won't be static (for the whole time when app is running).

推荐答案

1)你是幸运的。如果没有,你会得到一个分段错误。

1) You're lucky. If not, you'll get a segmentation fault.

2)的记忆是不以任何方式覆盖。

2) The memory was not overwritten in any way.

3)你可以使用插槽::轨道时被跟踪的对象被删除自动断开。 Boost.Signals2可以跟踪由升压:: shared_ptr的管理对象。

3) You could use slot::track to automatically disconnect when the tracked object gets deleted. Boost.Signals2 could track objects that are managed by boost::shared_ptr.

#include <boost/signals2.hpp>
#include <boost/shared_ptr.hpp>

struct object_with_slot
{
    void operator()()
    {
       std::cout << "Slot called!" << std::endl;
       member = 50500;
    }
    int member;
};

//
int main()
{
    typedef boost::signals2::signal<void ()> sig_type;
    sig_type sig;

    {
        boost::shared_ptr<object_with_slot> ptr(new object_with_slot);
        sig.connect(sig_type::slot_type(*ptr).track(ptr));

        // 'object_with_slot' managed by ptr is destroyed
    }

    sig(); // 'object_with_slot' not called here.

    return 0;
}

更新:结果
增加了code跟踪性病对象:: shared_ptr的和std :: weak_ptr的:

UPDATE:
Added code to track objects for std::shared_ptr and std::weak_ptr:

#include <memory>
#include <boost/signals2.hpp>

// added specializations for std::weak_ptr and std::shared_ptr
namespace boost
{
  namespace signals2
  {
    template<typename T> struct weak_ptr_traits<std::weak_ptr<T> >
    {
      typedef std::shared_ptr<T> shared_type;
    };

    template<typename T> struct shared_ptr_traits<std::shared_ptr<T> >
    {
      typedef std::weak_ptr<T> weak_type;
    };
  }
}

struct object_with_slot
{
    void operator()()
    {
       std::cout << "Slot called!" << std::endl;
       member = 50500;
    }
    int member;
};

//
int main()
{
    typedef boost::signals2::signal<void ()> sig_type;
    sig_type sig;

    std::shared_ptr<object_with_slot> ptr(new object_with_slot);
    sig.connect(sig_type::slot_type(*ptr).track_foreign(ptr)); // ptr is tracked

    sig();

    return 0;
}

这篇关于升压:: signals2 - 对象的descruction与槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆