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
|
//
// Copyright (c) 2022 Dmitry Arkhipov (grisumbras@yandex.ru)
//
// 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)
//
// Official repository: https://github.com/boostorg/json
//
#ifndef BOOST_JSON_DOC_DOC_TYPES_HPP
#define BOOST_JSON_DOC_DOC_TYPES_HPP
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmismatched-tags"
#endif
#include <array>
// tag::snippet_conv_spec_trait1[]
namespace user_ns
{
class ip_address
{
public:
ip_address(
unsigned char oct1,
unsigned char oct2,
unsigned char oct3,
unsigned char oct4 );
const unsigned char*
begin() const;
const unsigned char*
end() const;
private:
std::array<unsigned char, 4> octets_;
};
template< std::size_t N >
unsigned char
get(const ip_address& addr);
} // namespace user_ns
namespace std
{
template<>
struct tuple_size< user_ns::ip_address >
: std::integral_constant<std::size_t, 4>
{ };
template< std::size_t N >
struct tuple_element< N, user_ns::ip_address >
{
using type = unsigned char;
};
} // namespace std
// end::snippet_conv_spec_trait1[]
namespace user_ns
{
inline
ip_address::ip_address(
unsigned char oct1,
unsigned char oct2,
unsigned char oct3,
unsigned char oct4 )
: octets_{{oct1, oct2, oct3, oct4}}
{ }
inline
const unsigned char*
ip_address::begin() const
{
return octets_.data();
}
inline
const unsigned char*
ip_address::end() const
{
return begin() + octets_.size();
}
template< std::size_t N >
unsigned char
get(ip_address const& addr )
{
static_assert( N >= 0 && N <= 4, "incorrect N" );
return *(addr.begin() + N);
}
inline
bool
operator==(ip_address const& l, ip_address const& r) noexcept
{
return
get<0>(l) == get<0>(r) &&
get<1>(l) == get<1>(r) &&
get<2>(l) == get<2>(r) &&
get<3>(l) == get<3>(r);
}
} // namespace user_ns
#endif // BOOST_JSON_DOC_DOC_TYPES_HPP
|