forked from boostorg/smart_ptr
More tests added.
[SVN r16363]
This commit is contained in:
@ -706,8 +706,29 @@ void copy_constructor()
|
||||
|
||||
void weak_ptr_constructor()
|
||||
{
|
||||
boost::shared_ptr<Y> p(new Y);
|
||||
{
|
||||
boost::weak_ptr<Y> wp;
|
||||
|
||||
try
|
||||
{
|
||||
boost::shared_ptr<Y> p2(wp);
|
||||
BOOST_ERROR("shared_ptr<Y> p2(wp) failed to throw");
|
||||
}
|
||||
catch(boost::bad_weak_ptr)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
boost::shared_ptr<X> p3(wp);
|
||||
BOOST_ERROR("shared_ptr<X> p3(wp) failed to throw");
|
||||
}
|
||||
catch(boost::bad_weak_ptr)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
boost::shared_ptr<Y> p(new Y);
|
||||
boost::weak_ptr<Y> wp(p);
|
||||
|
||||
{
|
||||
@ -2639,8 +2660,131 @@ void test()
|
||||
namespace n_spt_another_sp
|
||||
{
|
||||
|
||||
template<class T> class another_ptr: private boost::shared_ptr<T>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef boost::shared_ptr<T> base_type;
|
||||
|
||||
public:
|
||||
|
||||
explicit another_ptr(T * p = 0): base_type(p)
|
||||
{
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
base_type::reset();
|
||||
}
|
||||
|
||||
T * get() const
|
||||
{
|
||||
return base_type::get();
|
||||
}
|
||||
};
|
||||
|
||||
class event_handler
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~event_handler() {}
|
||||
virtual void begin() = 0;
|
||||
virtual void handle(int event) = 0;
|
||||
virtual void end() = 0;
|
||||
};
|
||||
|
||||
int begin_called = 0;
|
||||
int handle_called = 0;
|
||||
int end_called = 0;
|
||||
|
||||
class event_handler_impl: public event_handler
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void begin()
|
||||
{
|
||||
++begin_called;
|
||||
}
|
||||
|
||||
virtual void handle(int event)
|
||||
{
|
||||
handle_called = event;
|
||||
}
|
||||
|
||||
virtual void end()
|
||||
{
|
||||
++end_called;
|
||||
}
|
||||
};
|
||||
|
||||
another_ptr<event_handler> get_event_handler()
|
||||
{
|
||||
another_ptr<event_handler> p(new event_handler_impl);
|
||||
return p;
|
||||
}
|
||||
|
||||
boost::shared_ptr<event_handler> current_handler;
|
||||
|
||||
void install_event_handler(boost::shared_ptr<event_handler> p)
|
||||
{
|
||||
p->begin();
|
||||
current_handler = p;
|
||||
}
|
||||
|
||||
void handle_event(int event)
|
||||
{
|
||||
current_handler->handle(event);
|
||||
}
|
||||
|
||||
void remove_event_handler()
|
||||
{
|
||||
current_handler->end();
|
||||
current_handler.reset();
|
||||
}
|
||||
|
||||
template<class P> class smart_pointer_deleter
|
||||
{
|
||||
private:
|
||||
|
||||
P p_;
|
||||
|
||||
public:
|
||||
|
||||
smart_pointer_deleter(P const & p): p_(p)
|
||||
{
|
||||
}
|
||||
|
||||
void operator()(void const *)
|
||||
{
|
||||
p_.reset();
|
||||
}
|
||||
};
|
||||
|
||||
void test()
|
||||
{
|
||||
another_ptr<event_handler> p = get_event_handler();
|
||||
|
||||
boost::shared_ptr<event_handler> q(p.get(), smart_pointer_deleter< another_ptr<event_handler> >(p));
|
||||
|
||||
p.reset();
|
||||
|
||||
BOOST_TEST(begin_called == 0);
|
||||
|
||||
install_event_handler(q);
|
||||
|
||||
BOOST_TEST(begin_called == 1);
|
||||
|
||||
BOOST_TEST(handle_called == 0);
|
||||
|
||||
handle_event(17041);
|
||||
|
||||
BOOST_TEST(handle_called == 17041);
|
||||
|
||||
BOOST_TEST(end_called == 0);
|
||||
|
||||
remove_event_handler();
|
||||
|
||||
BOOST_TEST(end_called == 1);
|
||||
}
|
||||
|
||||
} // namespace n_spt_another_sp
|
||||
|
Reference in New Issue
Block a user