config.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // config.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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. #ifndef BOOST_ASIO_CONFIG_HPP
  11. #define BOOST_ASIO_CONFIG_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/throw_exception.hpp>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/execution_context.hpp>
  19. #include <cstddef>
  20. #include <string>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// Base class for configuration implementations.
  25. class config_service :
  26. #if defined(GENERATING_DOCUMENTATION)
  27. public execution_context::service
  28. #else // defined(GENERATING_DOCUMENTATION)
  29. public detail::execution_context_service_base<config_service>
  30. #endif // defined(GENERATING_DOCUMENTATION)
  31. {
  32. public:
  33. #if defined(GENERATING_DOCUMENTATION)
  34. typedef config_service key_type;
  35. #endif // defined(GENERATING_DOCUMENTATION)
  36. /// Constructor.
  37. BOOST_ASIO_DECL explicit config_service(execution_context& ctx);
  38. /// Shutdown the service.
  39. BOOST_ASIO_DECL void shutdown() override;
  40. /// Retrieve a configuration value.
  41. BOOST_ASIO_DECL virtual const char* get_value(const char* section,
  42. const char* key_name, char* value, std::size_t value_len) const;
  43. };
  44. /// Provides access to the configuration values associated with an execution
  45. /// context.
  46. class config
  47. {
  48. public:
  49. /// Constructor.
  50. /**
  51. * This constructor initialises a @c config object to retrieve configuration
  52. * values associated with the specified execution context.
  53. */
  54. explicit config(execution_context& context)
  55. : service_(use_service<config_service>(context))
  56. {
  57. }
  58. /// Copy constructor.
  59. config(const config& other) noexcept
  60. : service_(other.service_)
  61. {
  62. }
  63. /// Retrieve an integral configuration value.
  64. template <typename T>
  65. constraint_t<is_integral<T>::value, T>
  66. get(const char* section, const char* key_name, T default_value) const;
  67. private:
  68. config_service& service_;
  69. };
  70. /// Configures an execution context based on a concurrency hint.
  71. /**
  72. * This configuration service is provided for backwards compatibility with
  73. * the existing concurrency hint mechanism.
  74. *
  75. * @par Example
  76. * @code boost::asio::io_context my_io_context{
  77. * boost::asio::config_from_concurrency_hint{1}}; @endcode
  78. */
  79. class config_from_concurrency_hint : public execution_context::service_maker
  80. {
  81. public:
  82. /// Construct with a default concurrency hint.
  83. BOOST_ASIO_DECL config_from_concurrency_hint();
  84. /// Construct with a specified concurrency hint.
  85. explicit config_from_concurrency_hint(int concurrency_hint)
  86. : concurrency_hint_(concurrency_hint)
  87. {
  88. }
  89. /// Add a concrete service to the specified execution context.
  90. BOOST_ASIO_DECL void make(execution_context& ctx) const override;
  91. private:
  92. int concurrency_hint_;
  93. };
  94. /// Configures an execution context by reading variables from a string.
  95. /**
  96. * Each variable must be on a line of its own, and of the form:
  97. *
  98. * <tt>section.key=value</tt>
  99. *
  100. * or, if an optional prefix is specified:
  101. *
  102. * <tt>prefix.section.key=value</tt>
  103. *
  104. * Blank lines and lines starting with <tt>#</tt> are ignored. It is also
  105. * permitted to include a comment starting with <tt>#</tt> after the value.
  106. *
  107. * @par Example
  108. * @code boost::asio::io_context my_io_context{
  109. * boost::asio::config_from_string{
  110. * "scheduler.concurrency_hint=10\n"
  111. * "scheduler.locking=1"}}; @endcode
  112. */
  113. class config_from_string : public execution_context::service_maker
  114. {
  115. public:
  116. /// Construct with the default prefix "asio".
  117. explicit config_from_string(std::string s)
  118. : string_(static_cast<std::string&&>(s)),
  119. prefix_()
  120. {
  121. }
  122. /// Construct with a specified prefix.
  123. config_from_string(std::string s, std::string prefix)
  124. : string_(static_cast<std::string&&>(s)),
  125. prefix_(static_cast<std::string&&>(prefix))
  126. {
  127. }
  128. /// Add a concrete service to the specified execution context.
  129. BOOST_ASIO_DECL void make(execution_context& ctx) const override;
  130. private:
  131. std::string string_;
  132. std::string prefix_;
  133. };
  134. /// Configures an execution context by reading environment variables.
  135. /**
  136. * The environment variable names are formed by concatenating the prefix,
  137. * section, and key, with underscore as delimiter, and then converting the
  138. * resulting string to upper case.
  139. *
  140. * @par Example
  141. * @code boost::asio::io_context my_io_context{
  142. * boost::asio::config_from_env{"my_app"}}; @endcode
  143. */
  144. class config_from_env : public execution_context::service_maker
  145. {
  146. public:
  147. /// Construct with the default prefix "asio".
  148. BOOST_ASIO_DECL config_from_env();
  149. /// Construct with a specified prefix.
  150. explicit config_from_env(std::string prefix)
  151. : prefix_(static_cast<std::string&&>(prefix))
  152. {
  153. }
  154. /// Add a concrete service to the specified execution context.
  155. BOOST_ASIO_DECL void make(execution_context& ctx) const override;
  156. private:
  157. std::string prefix_;
  158. };
  159. } // namespace asio
  160. } // namespace boost
  161. #include <boost/asio/detail/pop_options.hpp>
  162. #include <boost/asio/impl/config.hpp>
  163. #if defined(BOOST_ASIO_HEADER_ONLY)
  164. # include <boost/asio/impl/config.ipp>
  165. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  166. #endif // BOOST_ASIO_CONFIG_HPP