123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #ifndef BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP
- #define BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/push_options.hpp>
- #include <boost/asio/detail/push_options.hpp>
- #include <memory>
- #include <typeinfo>
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/io_service.hpp>
- #include <boost/asio/detail/mutex.hpp>
- #include <boost/asio/detail/noncopyable.hpp>
- #include <boost/asio/detail/service_id.hpp>
- #if defined(BOOST_NO_TYPEID)
- # if !defined(BOOST_ASIO_NO_TYPEID)
- # define BOOST_ASIO_NO_TYPEID
- # endif
- #endif
- namespace boost {
- namespace asio {
- namespace detail {
- #if defined(__GNUC__)
- # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
- # pragma GCC visibility push (default)
- # endif
- #endif
- template <typename T>
- class typeid_wrapper {};
- #if defined(__GNUC__)
- # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
- # pragma GCC visibility pop
- # endif
- #endif
- class service_registry
- : private noncopyable
- {
- public:
-
- service_registry(boost::asio::io_service& o)
- : owner_(o),
- first_service_(0)
- {
- }
-
- ~service_registry()
- {
-
-
-
- boost::asio::io_service::service* service = first_service_;
- while (service)
- {
- service->shutdown_service();
- service = service->next_;
- }
-
- while (first_service_)
- {
- boost::asio::io_service::service* next_service = first_service_->next_;
- delete first_service_;
- first_service_ = next_service;
- }
- }
-
-
-
- template <typename Service>
- Service& use_service()
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
-
- boost::asio::io_service::service* service = first_service_;
- while (service)
- {
- if (service_id_matches(*service, Service::id))
- return *static_cast<Service*>(service);
- service = service->next_;
- }
-
-
-
- lock.unlock();
- std::auto_ptr<Service> new_service(new Service(owner_));
- init_service_id(*new_service, Service::id);
- Service& new_service_ref = *new_service;
- lock.lock();
-
-
- service = first_service_;
- while (service)
- {
- if (service_id_matches(*service, Service::id))
- return *static_cast<Service*>(service);
- service = service->next_;
- }
-
- new_service->next_ = first_service_;
- first_service_ = new_service.release();
- return new_service_ref;
- }
-
-
- template <typename Service>
- bool add_service(Service* new_service)
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
-
- boost::asio::io_service::service* service = first_service_;
- while (service)
- {
- if (service_id_matches(*service, Service::id))
- return false;
- service = service->next_;
- }
-
- init_service_id(*new_service, Service::id);
- new_service->next_ = first_service_;
- first_service_ = new_service;
- return true;
- }
-
- template <typename Service>
- bool has_service() const
- {
- boost::asio::detail::mutex::scoped_lock lock(mutex_);
- boost::asio::io_service::service* service = first_service_;
- while (service)
- {
- if (service_id_matches(*service, Service::id))
- return true;
- service = service->next_;
- }
- return false;
- }
- private:
-
- void init_service_id(boost::asio::io_service::service& service,
- const boost::asio::io_service::id& id)
- {
- service.type_info_ = 0;
- service.id_ = &id;
- }
- #if !defined(BOOST_ASIO_NO_TYPEID)
-
- template <typename Service>
- void init_service_id(boost::asio::io_service::service& service,
- const boost::asio::detail::service_id<Service>& )
- {
- service.type_info_ = &typeid(typeid_wrapper<Service>);
- service.id_ = 0;
- }
- #endif
-
- static bool service_id_matches(
- const boost::asio::io_service::service& service,
- const boost::asio::io_service::id& id)
- {
- return service.id_ == &id;
- }
- #if !defined(BOOST_ASIO_NO_TYPEID)
-
- template <typename Service>
- static bool service_id_matches(
- const boost::asio::io_service::service& service,
- const boost::asio::detail::service_id<Service>& )
- {
- return service.type_info_ != 0
- && *service.type_info_ == typeid(typeid_wrapper<Service>);
- }
- #endif
-
- mutable boost::asio::detail::mutex mutex_;
-
- boost::asio::io_service& owner_;
-
- boost::asio::io_service::service* first_service_;
- };
- }
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #endif
|