2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-30 07:22:09 +00:00
Files
asio/test/config.cpp
Christopher Kohlhoff bbda620590 Add asio::config.
The asio::config class provides access to configuration variables that
are associated with an execution context. The class is intended for use
by asio internals, or by libraries or user-provided abstractions that
build on top of asio. These configuration variables will typically be
used to fine tune behaviour, such as enabling or disabling certain
optimisations.

When constructing an execution context, such as an io_context, the
caller may optionally pass a service_maker to install a concrete
configuration service into the context. For example:

  asio::io_context ctx{asio::config_from_env{}};

The configuration variables' values are accessed by using the
asio::config class, passing a section, key and default value:

  asio::config cfg{ctx};
  bool enable_locking = cfg.get("scheduler", "locking", true);

The initial set of configuration variables recognised by the asio
internals correspond to the concurrency hint and its special values:

  "scheduler" / "concurrency_hint" (int)
  "scheduler" / "locking" (bool)
  "reactor" / "registration_locking" (bool)
  "reactor" / "io_locking" (bool)
2024-10-30 23:01:37 +11:00

62 lines
1.9 KiB
C++

//
// config.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2024 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)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include <boost/asio/config.hpp>
#include <boost/asio/io_context.hpp>
#include <cstdlib>
#include "unit_test.hpp"
void config_from_string_test()
{
boost::asio::io_context ctx1(
boost::asio::config_from_string(
"scheduler.concurrency_hint=123\n"
" scheduler.locking = 1 \n"
"# comment\n"
"garbage\n"
"reactor.registration_locking= 0 # comment\n"
"reactor.io_locking=1"));
boost::asio::config cfg1(ctx1);
BOOST_ASIO_CHECK(cfg1.get("scheduler", "concurrency_hint", 0) == 123);
BOOST_ASIO_CHECK(cfg1.get("scheduler", "locking", false) == true);
BOOST_ASIO_CHECK(cfg1.get("reactor", "registration_locking", true) == false);
BOOST_ASIO_CHECK(cfg1.get("reactor", "io_locking", false) == true);
boost::asio::io_context ctx2(
boost::asio::config_from_string(
"prefix.scheduler.concurrency_hint=456\n"
" prefix.scheduler.locking = 1 \n"
"# comment\n"
"garbage\n"
"prefix.reactor.registration_locking= 0 # comment\n"
"prefix.reactor.io_locking=1",
"prefix"));
boost::asio::config cfg2(ctx2);
BOOST_ASIO_CHECK(cfg2.get("scheduler", "concurrency_hint", 0) == 456);
BOOST_ASIO_CHECK(cfg2.get("scheduler", "locking", false) == true);
BOOST_ASIO_CHECK(cfg2.get("reactor", "registration_locking", true) == false);
BOOST_ASIO_CHECK(cfg2.get("reactor", "io_locking", false) == true);
}
BOOST_ASIO_TEST_SUITE
(
"config",
BOOST_ASIO_TEST_CASE(config_from_string_test)
)