Clementine
execution_context.hpp
1 //
2 // execution_context.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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_EXECUTION_CONTEXT_HPP
12 #define ASIO_EXECUTION_CONTEXT_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/config.hpp"
19 #include <cstddef>
20 #include <stdexcept>
21 #include <typeinfo>
22 #include "asio/detail/noncopyable.hpp"
23 #include "asio/detail/variadic_templates.hpp"
24 
25 #include "asio/detail/push_options.hpp"
26 
27 namespace asio {
28 
29 class execution_context;
30 class io_context;
31 
32 #if !defined(GENERATING_DOCUMENTATION)
33 template <typename Service> Service& use_service(execution_context&);
34 template <typename Service> Service& use_service(io_context&);
35 template <typename Service> void add_service(execution_context&, Service*);
36 template <typename Service> bool has_service(execution_context&);
37 #endif // !defined(GENERATING_DOCUMENTATION)
38 
39 namespace detail { class service_registry; }
40 
42 
106  : private noncopyable
107 {
108 public:
109  class id;
110  class service;
111 
112 public:
114  ASIO_DECL execution_context();
115 
117  ASIO_DECL ~execution_context();
118 
119 protected:
121 
128  ASIO_DECL void shutdown();
129 
131 
138  ASIO_DECL void destroy();
139 
140 public:
143  {
146 
149 
151  fork_child
152  };
153 
155 
194  ASIO_DECL void notify_fork(fork_event event);
195 
197 
207  template <typename Service>
208  friend Service& use_service(execution_context& e);
209 
211 
224  template <typename Service>
225  friend Service& use_service(io_context& ioc);
226 
227 #if defined(GENERATING_DOCUMENTATION)
228 
230 
241  template <typename Service, typename... Args>
242  friend Service& make_service(execution_context& e, Args&&... args);
243 
244 #elif defined(ASIO_HAS_VARIADIC_TEMPLATES)
245 
246  template <typename Service, typename... Args>
247  friend Service& make_service(execution_context& e,
248  ASIO_MOVE_ARG(Args)... args);
249 
250 #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
251 
252  template <typename Service>
253  friend Service& make_service(execution_context& e);
254 
255 #define ASIO_PRIVATE_MAKE_SERVICE_DEF(n) \
256  template <typename Service, ASIO_VARIADIC_TPARAMS(n)> \
257  friend Service& make_service(execution_context& e, \
258  ASIO_VARIADIC_MOVE_PARAMS(n)); \
259 
260  ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_MAKE_SERVICE_DEF)
261 #undef ASIO_PRIVATE_MAKE_SERVICE_DEF
262 
263 #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
264 
267 
284  template <typename Service>
285  friend void add_service(execution_context& e, Service* svc);
286 
288 
297  template <typename Service>
298  friend bool has_service(execution_context& e);
299 
300 private:
301  // The service registry.
302  asio::detail::service_registry* service_registry_;
303 };
304 
307  : private noncopyable
308 {
309 public:
311  id() {}
312 };
313 
316  : private noncopyable
317 {
318 public:
320  execution_context& context();
321 
322 protected:
324 
327  ASIO_DECL service(execution_context& owner);
328 
330  ASIO_DECL virtual ~service();
331 
332 private:
334  virtual void shutdown() = 0;
335 
338 
342  ASIO_DECL virtual void notify_fork(
344 
345  friend class asio::detail::service_registry;
346  struct key
347  {
348  key() : type_info_(0), id_(0) {}
349  const std::type_info* type_info_;
350  const execution_context::id* id_;
351  } key_;
352 
353  execution_context& owner_;
354  service* next_;
355 };
356 
360  : public std::logic_error
361 {
362 public:
363  ASIO_DECL service_already_exists();
364 };
365 
369  : public std::logic_error
370 {
371 public:
372  ASIO_DECL invalid_service_owner();
373 };
374 
375 namespace detail {
376 
377 // Special derived service id type to keep classes header-file only.
378 template <typename Type>
380  : public execution_context::id
381 {
382 };
383 
384 // Special service base class to keep classes header-file only.
385 template <typename Type>
388 {
389 public:
390  static service_id<Type> id;
391 
392  // Constructor.
393  execution_context_service_base(execution_context& e)
395  {
396  }
397 };
398 
399 template <typename Type>
401 
402 } // namespace detail
403 } // namespace asio
404 
405 #include "asio/detail/pop_options.hpp"
406 
407 #include "asio/impl/execution_context.hpp"
408 #if defined(ASIO_HEADER_ONLY)
409 # include "asio/impl/execution_context.ipp"
410 #endif // defined(ASIO_HEADER_ONLY)
411 
412 #endif // ASIO_EXECUTION_CONTEXT_HPP
id()
Constructor.
Definition: execution_context.hpp:311
Definition: noncopyable.hpp:25
Class used to uniquely identify a service.
Definition: execution_context.hpp:306
Definition: execution_context.hpp:386
Base class for all io_context services.
Definition: execution_context.hpp:315
Definition: service_registry.hpp:36
A context for function object execution.
Definition: execution_context.hpp:105
Provides core I/O functionality.
Definition: io_context.hpp:211
Notify the context that the process is about to fork.
Definition: execution_context.hpp:145
Definition: chrono.h:284
Exception thrown when trying to add a duplicate service to an execution_context.
Definition: execution_context.hpp:359
Exception thrown when trying to add a service object to an execution_context where the service has a ...
Definition: execution_context.hpp:368
fork_event
Fork-related event notifications.
Definition: execution_context.hpp:142
Notify the context that the process has forked and is the parent.
Definition: execution_context.hpp:148
Definition: any_io_executor.hpp:28
Definition: execution_context.hpp:379