Use a simplified, lightweight scoped_ptr implementation.

This commit is contained in:
Christopher Kohlhoff 2011-03-18 16:39:54 +11:00
parent 0eda7606ae
commit 513a3ecfcb
8 changed files with 96 additions and 21 deletions

View File

@ -52,10 +52,10 @@ resolver_service_base::~resolver_service_base()
void resolver_service_base::shutdown_service()
{
work_.reset();
if (work_io_service_)
if (work_io_service_.get())
{
work_io_service_->stop();
if (work_thread_)
if (work_thread_.get())
{
work_thread_->join();
work_thread_.reset();
@ -67,7 +67,7 @@ void resolver_service_base::shutdown_service()
void resolver_service_base::fork_service(
asio::io_service::fork_event event)
{
if (work_thread_)
if (work_thread_.get())
{
if (event == asio::io_service::fork_prepare)
{
@ -115,7 +115,7 @@ void resolver_service_base::start_resolve_op(operation* op)
void resolver_service_base::start_work_thread()
{
asio::detail::mutex::scoped_lock lock(mutex_);
if (!work_thread_)
if (!work_thread_.get())
{
work_thread_.reset(new asio::detail::thread(
work_io_service_runner(*work_io_service_)));

View File

@ -69,7 +69,7 @@ void strand_service::construct(strand_service::implementation_type& impl)
asio::detail::mutex::scoped_lock lock(mutex_);
if (!implementations_[index])
if (!implementations_[index].get())
implementations_[index].reset(new strand_impl);
impl = implementations_[index].get();
}

View File

@ -89,7 +89,7 @@ void win_iocp_io_service::shutdown_service()
{
::InterlockedExchange(&shutdown_, 1);
if (timer_thread_)
if (timer_thread_.get())
{
LARGE_INTEGER timeout;
timeout.QuadPart = 1;
@ -125,7 +125,7 @@ void win_iocp_io_service::shutdown_service()
}
}
if (timer_thread_)
if (timer_thread_.get())
timer_thread_->join();
}
@ -455,7 +455,7 @@ void win_iocp_io_service::do_add_timer_queue(timer_queue_base& queue)
&timeout, max_timeout_msec, 0, 0, FALSE);
}
if (!timer_thread_)
if (!timer_thread_.get())
{
timer_thread_function thread_function = { this };
timer_thread_.reset(new thread(thread_function, 65536));
@ -471,7 +471,7 @@ void win_iocp_io_service::do_remove_timer_queue(timer_queue_base& queue)
void win_iocp_io_service::update_timeout()
{
if (timer_thread_)
if (timer_thread_.get())
{
// There's no point updating the waitable timer if the new timeout period
// exceeds the maximum timeout. In that case, we might as well wait for the

View File

@ -16,7 +16,6 @@
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include <boost/scoped_ptr.hpp>
#include "asio/error.hpp"
#include "asio/io_service.hpp"
#include "asio/detail/mutex.hpp"
@ -24,6 +23,7 @@
#include "asio/detail/operation.hpp"
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/detail/scoped_ptr.hpp"
#include "asio/detail/thread.hpp"
#include "asio/detail/push_options.hpp"
@ -102,16 +102,16 @@ private:
asio::detail::mutex mutex_;
// Private io_service used for performing asynchronous host resolution.
boost::scoped_ptr<asio::io_service> work_io_service_;
asio::detail::scoped_ptr<asio::io_service> work_io_service_;
// The work io_service implementation used to post completions.
io_service_impl& work_io_service_impl_;
// Work for the private io_service to perform.
boost::scoped_ptr<asio::io_service::work> work_;
asio::detail::scoped_ptr<asio::io_service::work> work_;
// Thread used for running the work io_service's run loop.
boost::scoped_ptr<asio::detail::thread> work_thread_;
asio::detail::scoped_ptr<asio::detail::thread> work_thread_;
};
} // namespace detail

View File

@ -0,0 +1,75 @@
//
// detail/scoped_ptr.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_DETAIL_SCOPED_PTR_HPP
#define ASIO_DETAIL_SCOPED_PTR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
namespace asio {
namespace detail {
template <typename T>
class scoped_ptr
{
public:
// Constructor.
explicit scoped_ptr(T* p = 0)
: p_(p)
{
}
// Destructor.
~scoped_ptr()
{
delete p_;
}
// Access.
T* get()
{
return p_;
}
// Access.
T* operator->()
{
return p_;
}
// Dereference.
T& operator*()
{
return *p_;
}
// Reset pointer.
void reset(T* p = 0)
{
delete p_;
p_ = p;
}
private:
// Disallow copying and assignment.
scoped_ptr(const scoped_ptr&);
scoped_ptr& operator=(const scoped_ptr&);
T* p_;
};
} // namespace detail
} // namespace asio
#endif // ASIO_DETAIL_SCOPED_PTR_HPP

View File

@ -16,11 +16,11 @@
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include <boost/scoped_ptr.hpp>
#include "asio/io_service.hpp"
#include "asio/detail/mutex.hpp"
#include "asio/detail/op_queue.hpp"
#include "asio/detail/operation.hpp"
#include "asio/detail/scoped_ptr.hpp"
#include "asio/detail/push_options.hpp"
@ -99,8 +99,8 @@ private:
// Number of implementations shared between all strand objects.
enum { num_implementations = 193 };
// The head of a linked list of all implementations.
boost::scoped_ptr<strand_impl> implementations_[num_implementations];
// Pool of implementations.
scoped_ptr<strand_impl> implementations_[num_implementations];
// Extra value used when hashing to prevent recycled memory locations from
// getting the same strand implementation.

View File

@ -20,10 +20,10 @@
#if defined(ASIO_HAS_IOCP)
#include <boost/limits.hpp>
#include <boost/scoped_ptr.hpp>
#include "asio/io_service.hpp"
#include "asio/detail/mutex.hpp"
#include "asio/detail/op_queue.hpp"
#include "asio/detail/scoped_ptr.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/detail/timer_op.hpp"
#include "asio/detail/timer_queue_base.hpp"
@ -240,7 +240,7 @@ private:
friend struct timer_thread_function;
// Background thread used for processing timeouts.
boost::scoped_ptr<thread> timer_thread_;
scoped_ptr<thread> timer_thread_;
// A waitable timer object used for waiting for timeouts.
auto_handle waitable_timer_;

View File

@ -20,11 +20,11 @@
#if defined(ASIO_HAS_STD_SYSTEM_ERROR)
# include <system_error>
#else // defined(ASIO_HAS_STD_SYSTEM_ERROR)
# include <boost/scoped_ptr.hpp>
# include <cerrno>
# include <exception>
# include <string>
# include "asio/error_code.hpp"
# include "asio/detail/scoped_ptr.hpp"
#endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)
#include "asio/detail/push_options.hpp"
@ -87,7 +87,7 @@ public:
try
#endif // !defined(BOOST_NO_EXCEPTIONS)
{
if (!what_)
if (!what_.get())
{
std::string tmp(context_);
if (tmp.length())
@ -119,7 +119,7 @@ private:
std::string context_;
// The string representation of the error.
mutable boost::scoped_ptr<std::string> what_;
mutable asio::detail::scoped_ptr<std::string> what_;
};
#endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)