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