Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:29:12

0001 //
0002 // config.hpp
0003 // ~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_CONFIG_HPP
0012 #define BOOST_ASIO_CONFIG_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/throw_exception.hpp>
0020 #include <boost/asio/detail/type_traits.hpp>
0021 #include <boost/asio/execution_context.hpp>
0022 #include <cstddef>
0023 #include <string>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 
0030 /// Base class for configuration implementations.
0031 class config_service :
0032 #if defined(GENERATING_DOCUMENTATION)
0033   public execution_context::service
0034 #else // defined(GENERATING_DOCUMENTATION)
0035   public detail::execution_context_service_base<config_service>
0036 #endif // defined(GENERATING_DOCUMENTATION)
0037 {
0038 public:
0039 #if defined(GENERATING_DOCUMENTATION)
0040   typedef config_service key_type;
0041 #endif // defined(GENERATING_DOCUMENTATION)
0042 
0043   /// Constructor.
0044   BOOST_ASIO_DECL explicit config_service(execution_context& ctx);
0045 
0046   /// Shutdown the service.
0047   BOOST_ASIO_DECL void shutdown() override;
0048 
0049   /// Retrieve a configuration value.
0050   BOOST_ASIO_DECL virtual const char* get_value(const char* section,
0051       const char* key, char* value, std::size_t value_len) const;
0052 };
0053 
0054 /// Provides access to the configuration values associated with an execution
0055 /// context.
0056 class config
0057 {
0058 public:
0059   /// Constructor.
0060   /**
0061    * This constructor initialises a @c config object to retrieve configuration
0062    * values associated with the specified execution context.
0063    */
0064   explicit config(execution_context& context)
0065     : service_(use_service<config_service>(context))
0066   {
0067   }
0068 
0069   /// Copy constructor.
0070   config(const config& other) noexcept
0071     : service_(other.service_)
0072   {
0073   }
0074 
0075   /// Retrieve an integral configuration value.
0076   template <typename T>
0077   constraint_t<is_integral<T>::value, T>
0078   get(const char* section, const char* key, T default_value) const;
0079 
0080 private:
0081   config_service& service_;
0082 };
0083 
0084 /// Configures an execution context based on a concurrency hint.
0085 /**
0086  * This configuration service is provided for backwards compatibility with
0087  * the existing concurrency hint mechanism.
0088  *
0089  * @par Example
0090  * @code boost::asio::io_context my_io_context{
0091  *     boost::asio::config_from_concurrency_hint{1}}; @endcode
0092  */
0093 class config_from_concurrency_hint : public execution_context::service_maker
0094 {
0095 public:
0096   /// Construct with a default concurrency hint.
0097   BOOST_ASIO_DECL config_from_concurrency_hint();
0098 
0099   /// Construct with a specified concurrency hint.
0100   explicit config_from_concurrency_hint(int concurrency_hint)
0101     : concurrency_hint_(concurrency_hint)
0102   {
0103   }
0104 
0105   /// Add a concrete service to the specified execution context.
0106   BOOST_ASIO_DECL void make(execution_context& ctx) const override;
0107 
0108 private:
0109   int concurrency_hint_;
0110 };
0111 
0112 /// Configures an execution context by reading variables from a string.
0113 /**
0114  * Each variable must be on a line of its own, and of the form:
0115  *
0116  * <tt>section.key=value</tt>
0117  *
0118  * or, if an optional prefix is specified:
0119  *
0120  * <tt>prefix.section.key=value</tt>
0121  *
0122  * Blank lines and lines starting with <tt>#</tt> are ignored. It is also
0123  * permitted to include a comment starting with <tt>#</tt> after the value.
0124  *
0125  * @par Example
0126  * @code boost::asio::io_context my_io_context{
0127  *     boost::asio::config_from_string{
0128  *       "scheduler.concurrency_hint=10\n"
0129  *       "scheduler.locking=1"}}; @endcode
0130  */
0131 class config_from_string : public execution_context::service_maker
0132 {
0133 public:
0134   /// Construct with the default prefix "asio".
0135   explicit config_from_string(std::string s)
0136     : string_(static_cast<std::string&&>(s)),
0137       prefix_()
0138   {
0139   }
0140 
0141   /// Construct with a specified prefix.
0142   config_from_string(std::string s, std::string prefix)
0143     : string_(static_cast<std::string&&>(s)),
0144       prefix_(static_cast<std::string&&>(prefix))
0145   {
0146   }
0147 
0148   /// Add a concrete service to the specified execution context.
0149   BOOST_ASIO_DECL void make(execution_context& ctx) const override;
0150 
0151 private:
0152   std::string string_;
0153   std::string prefix_;
0154 };
0155 
0156 /// Configures an execution context by reading environment variables.
0157 /**
0158  * The environment variable names are formed by concatenating the prefix,
0159  * section, and key, with underscore as delimiter, and then converting the
0160  * resulting string to upper case.
0161  *
0162  * @par Example
0163  * @code boost::asio::io_context my_io_context{
0164  *     boost::asio::config_from_env{"my_app"}}; @endcode
0165  */
0166 class config_from_env : public execution_context::service_maker
0167 {
0168 public:
0169   /// Construct with the default prefix "asio".
0170   BOOST_ASIO_DECL config_from_env();
0171 
0172   /// Construct with a specified prefix.
0173   explicit config_from_env(std::string prefix)
0174     : prefix_(static_cast<std::string&&>(prefix))
0175   {
0176   }
0177 
0178   /// Add a concrete service to the specified execution context.
0179   BOOST_ASIO_DECL void make(execution_context& ctx) const override;
0180 
0181 private:
0182   std::string prefix_;
0183 };
0184 
0185 } // namespace asio
0186 } // namespace boost
0187 
0188 #include <boost/asio/detail/pop_options.hpp>
0189 
0190 #include <boost/asio/impl/config.hpp>
0191 #if defined(BOOST_ASIO_HEADER_ONLY)
0192 # include <boost/asio/impl/config.ipp>
0193 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0194 
0195 #endif // BOOST_ASIO_CONFIG_HPP