config.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // impl/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_IMPL_CONFIG_HPP
  11. #define BOOST_ASIO_IMPL_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 <cerrno>
  17. #include <cstdlib>
  18. #include <limits>
  19. #include <stdexcept>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. template <typename T>
  25. T config_get(const config_service& service, const char* section,
  26. const char* key_name, T default_value, false_type /*is_bool*/)
  27. {
  28. if (is_unsigned<T>::value)
  29. {
  30. char buf[std::numeric_limits<unsigned long long>::digits10
  31. + 1 /* sign */ + 1 /* partial digit */ + 1 /* NUL */];
  32. if (const char* str = service.get_value(
  33. section, key_name, buf, sizeof(buf)))
  34. {
  35. char* end = nullptr;
  36. errno = 0;
  37. unsigned long long result = std::strtoull(str, &end, 0);
  38. if (errno == ERANGE
  39. || result > static_cast<unsigned long long>(
  40. (std::numeric_limits<T>::max)()))
  41. detail::throw_exception(std::out_of_range("config out of range"));
  42. return static_cast<T>(result);
  43. }
  44. }
  45. else
  46. {
  47. char buf[std::numeric_limits<unsigned long long>::digits10
  48. + 1 /* sign */ + 1 /* partial digit */ + 1 /* NUL */];
  49. if (const char* str = service.get_value(
  50. section, key_name, buf, sizeof(buf)))
  51. {
  52. char* end = nullptr;
  53. errno = 0;
  54. long long result = std::strtoll(str, &end, 0);
  55. if (errno == ERANGE || result < (std::numeric_limits<T>::min)()
  56. || result > (std::numeric_limits<T>::max)())
  57. detail::throw_exception(std::out_of_range("config out of range"));
  58. return static_cast<T>(result);
  59. }
  60. }
  61. return default_value;
  62. }
  63. template <typename T>
  64. T config_get(const config_service& service, const char* section,
  65. const char* key_name, T default_value, true_type /*is_bool*/)
  66. {
  67. char buf[std::numeric_limits<unsigned long long>::digits10
  68. + 1 /* sign */ + 1 /* partial digit */ + 1 /* NUL */];
  69. if (const char* str = service.get_value(
  70. section, key_name, buf, sizeof(buf)))
  71. {
  72. char* end = nullptr;
  73. errno = 0;
  74. unsigned long long result = std::strtoll(str, &end, 0);
  75. if (errno == ERANGE || (result != 0 && result != 1))
  76. detail::throw_exception(std::out_of_range("config out of range"));
  77. return static_cast<T>(result != 0);
  78. }
  79. return default_value;
  80. }
  81. } // namespace detail
  82. template <typename T>
  83. constraint_t<is_integral<T>::value, T>
  84. config::get(const char* section, const char* key_name, T default_value) const
  85. {
  86. return detail::config_get(service_, section,
  87. key_name, default_value, is_same<T, bool>());
  88. }
  89. } // namespace asio
  90. } // namespace boost
  91. #include <boost/asio/detail/pop_options.hpp>
  92. #endif // BOOST_ASIO_IMPL_CONFIG_HPP