1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/json
7  
// Official repository: https://github.com/boostorg/json
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_JSON_DETAIL_OBJECT_HPP
10  
#ifndef BOOST_JSON_DETAIL_OBJECT_HPP
11  
#define BOOST_JSON_DETAIL_OBJECT_HPP
11  
#define BOOST_JSON_DETAIL_OBJECT_HPP
12  

12  

13  
#include <boost/json/storage_ptr.hpp>
13  
#include <boost/json/storage_ptr.hpp>
14  
#include <boost/json/string_view.hpp>
14  
#include <boost/json/string_view.hpp>
15  
#include <cstdlib>
15  
#include <cstdlib>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace json {
18  
namespace json {
19  

19  

20  
class object;
20  
class object;
21  
class value;
21  
class value;
22  
class key_value_pair;
22  
class key_value_pair;
23  

23  

24  
namespace detail {
24  
namespace detail {
25  

25  

26  
class unchecked_object
26  
class unchecked_object
27  
{
27  
{
28  
    // each element is two values,
28  
    // each element is two values,
29  
    // first one is a string key,
29  
    // first one is a string key,
30  
    // second one is the value.
30  
    // second one is the value.
31  
    value* data_;
31  
    value* data_;
32  
    std::size_t size_;
32  
    std::size_t size_;
33  
    storage_ptr const& sp_;
33  
    storage_ptr const& sp_;
34  

34  

35  
public:
35  
public:
36  
    inline
36  
    inline
37  
    ~unchecked_object();
37  
    ~unchecked_object();
38  

38  

39  
    unchecked_object(
39  
    unchecked_object(
40  
        value* data,
40  
        value* data,
41  
        std::size_t size, // # of kv-pairs
41  
        std::size_t size, // # of kv-pairs
42  
        storage_ptr const& sp) noexcept
42  
        storage_ptr const& sp) noexcept
43  
        : data_(data)
43  
        : data_(data)
44  
        , size_(size)
44  
        , size_(size)
45  
        , sp_(sp)
45  
        , sp_(sp)
46  
    {
46  
    {
47  
    }
47  
    }
48  

48  

49  
    unchecked_object(
49  
    unchecked_object(
50  
        unchecked_object&& other) noexcept
50  
        unchecked_object&& other) noexcept
51  
        : data_(other.data_)
51  
        : data_(other.data_)
52  
        , size_(other.size_)
52  
        , size_(other.size_)
53  
        , sp_(other.sp_)
53  
        , sp_(other.sp_)
54  
    {
54  
    {
55  
        other.data_ = nullptr;
55  
        other.data_ = nullptr;
56  
    }
56  
    }
57  

57  

58  
    storage_ptr const&
58  
    storage_ptr const&
59  
    storage() const noexcept
59  
    storage() const noexcept
60  
    {
60  
    {
61  
        return sp_;
61  
        return sp_;
62  
    }
62  
    }
63  

63  

64  
    std::size_t
64  
    std::size_t
65  
    size() const noexcept
65  
    size() const noexcept
66  
    {
66  
    {
67  
        return size_;
67  
        return size_;
68  
    }
68  
    }
69  

69  

70  
    value*
70  
    value*
71  
    release() noexcept
71  
    release() noexcept
72  
    {
72  
    {
73  
        auto const data = data_;
73  
        auto const data = data_;
74  
        data_ = nullptr;
74  
        data_ = nullptr;
75  
        return data;
75  
        return data;
76  
    }
76  
    }
77  
};
77  
};
78  

78  

79  
template<class CharRange>
79  
template<class CharRange>
80  
std::pair<key_value_pair*, std::size_t>
80  
std::pair<key_value_pair*, std::size_t>
81  
find_in_object(
81  
find_in_object(
82  
    object const& obj,
82  
    object const& obj,
83  
    CharRange key) noexcept;
83  
    CharRange key) noexcept;
84  

84  

85  
extern template
85  
extern template
86  
BOOST_JSON_DECL
86  
BOOST_JSON_DECL
87  
std::pair<key_value_pair*, std::size_t>
87  
std::pair<key_value_pair*, std::size_t>
88  
find_in_object<string_view>(
88  
find_in_object<string_view>(
89  
    object const&,
89  
    object const&,
90  
    string_view key) noexcept;
90  
    string_view key) noexcept;
91  

91  

92  
} // detail
92  
} // detail
93  
} // namespace json
93  
} // namespace json
94  
} // namespace boost
94  
} // namespace boost
95  

95  

96  
#endif
96  
#endif