Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:14:54

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/json
0008 //
0009 
0010 #ifndef BOOST_JSON_DETAIL_VALUE_HPP
0011 #define BOOST_JSON_DETAIL_VALUE_HPP
0012 
0013 #include <boost/json/kind.hpp>
0014 #include <boost/json/storage_ptr.hpp>
0015 #include <cstdint>
0016 #include <limits>
0017 #include <new>
0018 #include <utility>
0019 
0020 namespace boost {
0021 namespace json {
0022 namespace detail {
0023 
0024 struct key_t
0025 {
0026 };
0027 
0028 #if 0
0029 template<class T>
0030 struct to_number_limit
0031     : std::numeric_limits<T>
0032 {
0033 };
0034 
0035 template<class T>
0036 struct to_number_limit<T const>
0037     : to_number_limit<T>
0038 {
0039 };
0040 
0041 template<>
0042 struct to_number_limit<long long>
0043 {
0044     static constexpr long long (min)() noexcept
0045     {
0046         return -9223372036854774784;
0047     }
0048 
0049     static constexpr long long (max)() noexcept
0050     {
0051         return 9223372036854774784;
0052     }
0053 };
0054 
0055 template<>
0056 struct to_number_limit<unsigned long long>
0057 {
0058     static constexpr
0059     unsigned long long (min)() noexcept
0060     {
0061         return 0;
0062     }
0063 
0064     static constexpr
0065     unsigned long long (max)() noexcept
0066     {
0067         return 18446744073709549568ULL;
0068     }
0069 };
0070 #else
0071 
0072 template<class T>
0073 class to_number_limit
0074 {
0075     // unsigned
0076 
0077     static constexpr
0078     double min1(std::false_type)
0079     {
0080         return 0.0;
0081     }
0082 
0083     static constexpr
0084     double max1(std::false_type)
0085     {
0086         return max2u(std::integral_constant<
0087             bool, (std::numeric_limits<T>::max)() ==
0088             UINT64_MAX>{});
0089     }
0090 
0091     static constexpr
0092     double max2u(std::false_type)
0093     {
0094         return static_cast<double>(
0095             (std::numeric_limits<T>::max)());
0096     }
0097 
0098     static constexpr
0099     double max2u(std::true_type)
0100     {
0101         return 18446744073709549568.0;
0102     }
0103 
0104     // signed
0105 
0106     static constexpr
0107     double min1(std::true_type)
0108     {
0109         return min2s(std::integral_constant<
0110             bool, (std::numeric_limits<T>::max)() ==
0111             INT64_MAX>{});
0112     }
0113 
0114     static constexpr
0115     double min2s(std::false_type)
0116     {
0117         return static_cast<double>(
0118             (std::numeric_limits<T>::min)());
0119     }
0120 
0121     static constexpr
0122     double min2s(std::true_type)
0123     {
0124         return -9223372036854774784.0;
0125     }
0126 
0127     static constexpr
0128     double max1(std::true_type)
0129     {
0130         return max2s(std::integral_constant<
0131             bool, (std::numeric_limits<T>::max)() ==
0132             INT64_MAX>{});
0133     }
0134 
0135     static constexpr
0136     double max2s(std::false_type)
0137     {
0138         return static_cast<double>(
0139             (std::numeric_limits<T>::max)());
0140     }
0141 
0142     static constexpr
0143     double max2s(std::true_type)
0144     {
0145         return 9223372036854774784.0;
0146     }
0147 
0148 public:
0149     static constexpr
0150     double (min)() noexcept
0151     {
0152         return min1(std::is_signed<T>{});
0153     }
0154 
0155     static constexpr
0156     double (max)() noexcept
0157     {
0158         return max1(std::is_signed<T>{});
0159     }
0160 };
0161 
0162 #endif
0163 
0164 struct scalar
0165 {
0166     storage_ptr sp; // must come first
0167     kind k;         // must come second
0168     union
0169     {
0170         bool b;
0171         std::int64_t i;
0172         std::uint64_t u;
0173         double d;
0174     };
0175 
0176     explicit
0177     scalar(storage_ptr sp_ = {}) noexcept
0178         : sp(std::move(sp_))
0179         , k(json::kind::null)
0180     {
0181     }
0182 
0183     explicit
0184     scalar(bool b_,
0185         storage_ptr sp_ = {}) noexcept
0186         : sp(std::move(sp_))
0187         , k(json::kind::bool_)
0188         , b(b_)
0189     {
0190     }
0191 
0192     explicit
0193     scalar(std::int64_t i_,
0194         storage_ptr sp_ = {}) noexcept
0195         : sp(std::move(sp_))
0196         , k(json::kind::int64)
0197         , i(i_)
0198     {
0199     }
0200 
0201     explicit
0202     scalar(std::uint64_t u_,
0203         storage_ptr sp_ = {}) noexcept
0204         : sp(std::move(sp_))
0205         , k(json::kind::uint64)
0206         , u(u_)
0207     {
0208     }
0209 
0210     explicit
0211     scalar(double d_,
0212         storage_ptr sp_ = {}) noexcept
0213         : sp(std::move(sp_))
0214         , k(json::kind::double_)
0215         , d(d_)
0216     {
0217     }
0218 };
0219 
0220 struct access
0221 {
0222     template<class Value, class... Args>
0223     static
0224     Value&
0225     construct_value(Value* p, Args&&... args)
0226     {
0227         return *reinterpret_cast<
0228             Value*>(::new(p) Value(
0229             std::forward<Args>(args)...));
0230     }
0231 
0232     template<class KeyValuePair, class... Args>
0233     static
0234     KeyValuePair&
0235     construct_key_value_pair(
0236         KeyValuePair* p, Args&&... args)
0237     {
0238         return *reinterpret_cast<
0239             KeyValuePair*>(::new(p)
0240                 KeyValuePair(
0241                     std::forward<Args>(args)...));
0242     }
0243 
0244     template<class Value>
0245     static
0246     char const*
0247     release_key(
0248         Value& jv,
0249         std::size_t& len) noexcept
0250     {
0251         BOOST_ASSERT(jv.is_string());
0252         jv.str_.sp_.~storage_ptr();
0253         return jv.str_.impl_.release_key(len);
0254     }
0255 
0256     using index_t = std::uint32_t;
0257 
0258     template<class KeyValuePair>
0259     static
0260     index_t&
0261     next(KeyValuePair& e) noexcept
0262     {
0263         return e.next_;
0264     }
0265 
0266     template<class KeyValuePair>
0267     static
0268     index_t const&
0269     next(KeyValuePair const& e) noexcept
0270     {
0271         return e.next_;
0272     }
0273 };
0274 
0275 BOOST_JSON_DECL
0276 std::size_t
0277 hash_value_impl( value const& jv ) noexcept;
0278 
0279 } // detail
0280 } // namespace json
0281 } // namespace boost
0282 
0283 #endif