~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/detail/service_registry.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// service_registry.hpp
 
3
// ~~~~~~~~~~~~~~~~~~~~
 
4
//
 
5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
 
6
//
 
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
 
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
9
//
 
10
 
 
11
#ifndef ASIO_DETAIL_SERVICE_REGISTRY_HPP
 
12
#define ASIO_DETAIL_SERVICE_REGISTRY_HPP
 
13
 
 
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 
15
# pragma once
 
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 
17
 
 
18
#include "asio/detail/push_options.hpp"
 
19
 
 
20
#include "asio/detail/push_options.hpp"
 
21
#include <memory>
 
22
#include <typeinfo>
 
23
#include "asio/detail/pop_options.hpp"
 
24
 
 
25
#include "asio/io_service.hpp"
 
26
#include "asio/detail/mutex.hpp"
 
27
#include "asio/detail/noncopyable.hpp"
 
28
#include "asio/detail/service_id.hpp"
 
29
 
 
30
namespace asio {
 
31
namespace detail {
 
32
 
 
33
class service_registry
 
34
  : private noncopyable
 
35
{
 
36
public:
 
37
  // Constructor.
 
38
  service_registry(asio::io_service& o)
 
39
    : owner_(o),
 
40
      first_service_(0)
 
41
  {
 
42
  }
 
43
 
 
44
  // Destructor.
 
45
  ~service_registry()
 
46
  {
 
47
    // Shutdown all services. This must be done in a separate loop before the
 
48
    // services are destroyed since the destructors of user-defined handler
 
49
    // objects may try to access other service objects.
 
50
    asio::io_service::service* service = first_service_;
 
51
    while (service)
 
52
    {
 
53
      service->shutdown_service();
 
54
      service = service->next_;
 
55
    }
 
56
 
 
57
    // Destroy all services.
 
58
    while (first_service_)
 
59
    {
 
60
      asio::io_service::service* next_service = first_service_->next_;
 
61
      delete first_service_;
 
62
      first_service_ = next_service;
 
63
    }
 
64
  }
 
65
 
 
66
  // Get the service object corresponding to the specified service type. Will
 
67
  // create a new service object automatically if no such object already
 
68
  // exists. Ownership of the service object is not transferred to the caller.
 
69
  template <typename Service>
 
70
  Service& use_service()
 
71
  {
 
72
    asio::detail::mutex::scoped_lock lock(mutex_);
 
73
 
 
74
    // First see if there is an existing service object for the given type.
 
75
    asio::io_service::service* service = first_service_;
 
76
    while (service)
 
77
    {
 
78
      if (service_id_matches(*service, Service::id))
 
79
        return *static_cast<Service*>(service);
 
80
      service = service->next_;
 
81
    }
 
82
 
 
83
    // Create a new service object. The service registry's mutex is not locked
 
84
    // at this time to allow for nested calls into this function from the new
 
85
    // service's constructor.
 
86
    lock.unlock();
 
87
    std::auto_ptr<Service> new_service(new Service(owner_));
 
88
    init_service_id(*new_service, Service::id);
 
89
    Service& new_service_ref = *new_service;
 
90
    lock.lock();
 
91
 
 
92
    // Check that nobody else created another service object of the same type
 
93
    // while the lock was released.
 
94
    service = first_service_;
 
95
    while (service)
 
96
    {
 
97
      if (service_id_matches(*service, Service::id))
 
98
        return *static_cast<Service*>(service);
 
99
      service = service->next_;
 
100
    }
 
101
 
 
102
    // Service was successfully initialised, pass ownership to registry.
 
103
    new_service->next_ = first_service_;
 
104
    first_service_ = new_service.release();
 
105
 
 
106
    return new_service_ref;
 
107
  }
 
108
 
 
109
  // Add a service object. Returns false on error, in which case ownership of
 
110
  // the object is retained by the caller.
 
111
  template <typename Service>
 
112
  bool add_service(Service* new_service)
 
113
  {
 
114
    asio::detail::mutex::scoped_lock lock(mutex_);
 
115
 
 
116
    // Check if there is an existing service object for the given type.
 
117
    asio::io_service::service* service = first_service_;
 
118
    while (service)
 
119
    {
 
120
      if (service_id_matches(*service, Service::id))
 
121
        return false;
 
122
      service = service->next_;
 
123
    }
 
124
 
 
125
    // Take ownership of the service object.
 
126
    init_service_id(*new_service, Service::id);
 
127
    new_service->next_ = first_service_;
 
128
    first_service_ = new_service;
 
129
 
 
130
    return true;
 
131
  }
 
132
 
 
133
  // Check whether a service object of the specified type already exists.
 
134
  template <typename Service>
 
135
  bool has_service() const
 
136
  {
 
137
    asio::detail::mutex::scoped_lock lock(mutex_);
 
138
 
 
139
    asio::io_service::service* service = first_service_;
 
140
    while (service)
 
141
    {
 
142
      if (service_id_matches(*service, Service::id))
 
143
        return true;
 
144
      service = service->next_;
 
145
    }
 
146
 
 
147
    return false;
 
148
  }
 
149
 
 
150
private:
 
151
  // Set a service's id.
 
152
  void init_service_id(asio::io_service::service& service,
 
153
      const asio::io_service::id& id)
 
154
  {
 
155
    service.type_info_ = 0;
 
156
    service.id_ = &id;
 
157
  }
 
158
 
 
159
  // Set a service's id.
 
160
  template <typename Service>
 
161
  void init_service_id(asio::io_service::service& service,
 
162
      const asio::detail::service_id<Service>& /*id*/)
 
163
  {
 
164
    service.type_info_ = &typeid(Service);
 
165
    service.id_ = 0;
 
166
  }
 
167
 
 
168
  // Check if a service matches the given id.
 
169
  static bool service_id_matches(
 
170
      const asio::io_service::service& service,
 
171
      const asio::io_service::id& id)
 
172
  {
 
173
    return service.id_ == &id;
 
174
  }
 
175
 
 
176
  // Check if a service matches the given id.
 
177
  template <typename Service>
 
178
  static bool service_id_matches(
 
179
      const asio::io_service::service& service,
 
180
      const asio::detail::service_id<Service>& /*id*/)
 
181
  {
 
182
    return service.type_info_ != 0 && *service.type_info_ == typeid(Service);
 
183
  }
 
184
 
 
185
  // Mutex to protect access to internal data.
 
186
  mutable asio::detail::mutex mutex_;
 
187
 
 
188
  // The owner of this service registry and the services it contains.
 
189
  asio::io_service& owner_;
 
190
 
 
191
  // The first service in the list of contained services.
 
192
  asio::io_service::service* first_service_;
 
193
};
 
194
 
 
195
} // namespace detail
 
196
} // namespace asio
 
197
 
 
198
#include "asio/detail/pop_options.hpp"
 
199
 
 
200
#endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP