/src/spotify-json/include/spotify/json/encoded_value.hpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016 Spotify AB |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | * use this file except in compliance with the License. You may obtain a copy of |
6 | | * the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | * License for the specific language governing permissions and limitations under |
14 | | * the License. |
15 | | */ |
16 | | |
17 | | #pragma once |
18 | | |
19 | | #include <cstdlib> |
20 | | #include <cstring> |
21 | | #include <memory> |
22 | | #include <new> |
23 | | #include <ostream> |
24 | | |
25 | | #include <spotify/json/decode_context.hpp> |
26 | | #include <spotify/json/detail/decode_helpers.hpp> |
27 | | #include <spotify/json/detail/macros.hpp> |
28 | | #include <spotify/json/detail/skip_value.hpp> |
29 | | #include <spotify/json/encode_context.hpp> |
30 | | |
31 | | namespace spotify { |
32 | | namespace json { |
33 | | namespace detail { |
34 | | |
35 | | struct encoded_value_base { |
36 | | struct unsafe_unchecked {}; |
37 | | |
38 | | protected: |
39 | | void validate_json(const char *data, std::size_t size); |
40 | | }; |
41 | | |
42 | | } // namespace detail |
43 | | |
44 | | struct encoded_value; |
45 | | |
46 | | struct encoded_value_ref : public detail::encoded_value_base { |
47 | | encoded_value_ref(); |
48 | | encoded_value_ref(const encoded_value &value); |
49 | | explicit encoded_value_ref(const char *cstr); |
50 | | explicit encoded_value_ref(const char *cstr, const unsafe_unchecked &); |
51 | | explicit encoded_value_ref(const char *data, std::size_t size); |
52 | | explicit encoded_value_ref(const char *data, std::size_t size, const unsafe_unchecked &); |
53 | | |
54 | | template <typename value_with_data_and_size> |
55 | | explicit encoded_value_ref(const value_with_data_and_size &json); |
56 | | |
57 | 0 | const char *data() const { return _data; } |
58 | 0 | std::size_t size() const { return _size; } |
59 | | |
60 | | void swap(encoded_value_ref &value_ref); |
61 | | |
62 | | private: |
63 | | std::size_t _size; |
64 | | const char *_data; |
65 | | }; |
66 | | |
67 | | struct encoded_value : public detail::encoded_value_base { |
68 | | encoded_value(); |
69 | | encoded_value(encoded_value &&value) noexcept; |
70 | | encoded_value(const encoded_value &value); |
71 | | encoded_value(const encoded_value_ref &value_ref); |
72 | | ~encoded_value(); |
73 | | |
74 | | explicit encoded_value(const char *cstr); |
75 | | explicit encoded_value(const char *cstr, const unsafe_unchecked &); |
76 | | explicit encoded_value(const char *data, std::size_t size); |
77 | | explicit encoded_value(const char *data, std::size_t size, const unsafe_unchecked &); |
78 | | explicit encoded_value(encode_context &&context); |
79 | | explicit encoded_value(encode_context &&context, const unsafe_unchecked &); |
80 | | |
81 | | template <typename value_with_data_and_size> |
82 | | explicit encoded_value(const value_with_data_and_size &json); |
83 | | |
84 | | encoded_value &operator=(encoded_value &&value) noexcept; |
85 | | encoded_value &operator=(const encoded_value &value); |
86 | | encoded_value &operator=(const encoded_value_ref &value_ref); |
87 | | |
88 | 0 | const char *data() const { return static_cast<const char *>(_data.get()); } |
89 | 0 | std::size_t size() const { return _size; } |
90 | | |
91 | | void swap(encoded_value &value); |
92 | | |
93 | | private: |
94 | | using data_buffer = std::unique_ptr<void, decltype(std::free) *>; |
95 | | std::size_t _size; |
96 | | data_buffer _data; |
97 | | }; |
98 | | |
99 | | inline encoded_value_ref::encoded_value_ref(const encoded_value &value) |
100 | | : encoded_value_ref(value.data(), value.size(), unsafe_unchecked()) {} |
101 | | |
102 | | inline encoded_value_ref::encoded_value_ref(const char *cstr) |
103 | | : encoded_value_ref(cstr, std::strlen(cstr)) {} |
104 | | |
105 | | inline encoded_value_ref::encoded_value_ref(const char *cstr, const unsafe_unchecked &) |
106 | | : encoded_value_ref(cstr, std::strlen(cstr), unsafe_unchecked()) {} |
107 | | |
108 | | inline encoded_value_ref::encoded_value_ref(const char *data, std::size_t size, const unsafe_unchecked &) |
109 | | : _size(size), |
110 | | _data(data) {} |
111 | | |
112 | | template <typename value_with_data_and_size> |
113 | | encoded_value_ref::encoded_value_ref(const value_with_data_and_size &json) |
114 | | : encoded_value_ref(json.data(), json.size()) {} |
115 | | |
116 | | inline encoded_value::encoded_value(encoded_value &&value) noexcept |
117 | | : encoded_value() { |
118 | | swap(value); |
119 | | } |
120 | | |
121 | | inline encoded_value::encoded_value(const encoded_value &value) |
122 | | : encoded_value(value.data(), value.size(), unsafe_unchecked()) {} |
123 | | |
124 | | inline encoded_value::encoded_value(const encoded_value_ref &value_ref) |
125 | | : encoded_value(value_ref.data(), value_ref.size(), unsafe_unchecked()) {} |
126 | | |
127 | | inline encoded_value::encoded_value(const char *cstr) |
128 | | : encoded_value(cstr, std::strlen(cstr)) {} |
129 | | |
130 | | inline encoded_value::encoded_value(const char *cstr, const unsafe_unchecked &) |
131 | | : encoded_value(cstr, std::strlen(cstr), unsafe_unchecked()) {} |
132 | | |
133 | | inline encoded_value::encoded_value(encode_context &&context, const unsafe_unchecked &) |
134 | | : _size(context.size()), |
135 | | _data(context.steal_data()) {} |
136 | | |
137 | | template <typename value_with_data_and_size> |
138 | | encoded_value::encoded_value(const value_with_data_and_size &json) |
139 | | : encoded_value(json.data(), json.size()) {} |
140 | | |
141 | | std::ostream &operator <<(std::ostream &stream, const encoded_value_ref &value); |
142 | | std::ostream &operator <<(std::ostream &stream, const encoded_value &value); |
143 | | |
144 | | bool operator==(const encoded_value_ref &a, const encoded_value_ref &b); |
145 | | bool operator!=(const encoded_value_ref &a, const encoded_value_ref &b); |
146 | | |
147 | | } // namespace json |
148 | | } // namespace spotify |