basic_resolver_entry.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // ip/basic_resolver_entry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_IP_BASIC_RESOLVER_ENTRY_HPP
  11. #define BOOST_ASIO_IP_BASIC_RESOLVER_ENTRY_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 <string>
  17. #include <boost/asio/detail/string_view.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace ip {
  22. /// An entry produced by a resolver.
  23. /**
  24. * The boost::asio::ip::basic_resolver_entry class template describes an entry
  25. * as returned by a resolver.
  26. *
  27. * @par Thread Safety
  28. * @e Distinct @e objects: Safe.@n
  29. * @e Shared @e objects: Unsafe.
  30. */
  31. template <typename InternetProtocol>
  32. class basic_resolver_entry
  33. {
  34. public:
  35. /// The protocol type associated with the endpoint entry.
  36. typedef InternetProtocol protocol_type;
  37. /// The endpoint type associated with the endpoint entry.
  38. typedef typename InternetProtocol::endpoint endpoint_type;
  39. /// Default constructor.
  40. basic_resolver_entry()
  41. {
  42. }
  43. /// Construct with specified endpoint, host name and service name.
  44. basic_resolver_entry(const endpoint_type& ep,
  45. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service)
  46. : endpoint_(ep),
  47. host_name_(static_cast<std::string>(host)),
  48. service_name_(static_cast<std::string>(service))
  49. {
  50. }
  51. /// Get the endpoint associated with the entry.
  52. endpoint_type endpoint() const
  53. {
  54. return endpoint_;
  55. }
  56. /// Convert to the endpoint associated with the entry.
  57. operator endpoint_type() const
  58. {
  59. return endpoint_;
  60. }
  61. /// Get the host name associated with the entry.
  62. std::string host_name() const
  63. {
  64. return host_name_;
  65. }
  66. /// Get the host name associated with the entry.
  67. template <class Allocator>
  68. std::basic_string<char, std::char_traits<char>, Allocator> host_name(
  69. const Allocator& alloc = Allocator()) const
  70. {
  71. return std::basic_string<char, std::char_traits<char>, Allocator>(
  72. host_name_.c_str(), alloc);
  73. }
  74. /// Get the service name associated with the entry.
  75. std::string service_name() const
  76. {
  77. return service_name_;
  78. }
  79. /// Get the service name associated with the entry.
  80. template <class Allocator>
  81. std::basic_string<char, std::char_traits<char>, Allocator> service_name(
  82. const Allocator& alloc = Allocator()) const
  83. {
  84. return std::basic_string<char, std::char_traits<char>, Allocator>(
  85. service_name_.c_str(), alloc);
  86. }
  87. private:
  88. endpoint_type endpoint_;
  89. std::string host_name_;
  90. std::string service_name_;
  91. };
  92. } // namespace ip
  93. } // namespace asio
  94. } // namespace boost
  95. #include <boost/asio/detail/pop_options.hpp>
  96. #endif // BOOST_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP