aboutsummaryrefslogtreecommitdiff
path: root/include/jsoncons/detail/string_wrapper.hpp
blob: 88634b018e9e470d505bbac852d9d70593fe8363 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2013 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_DETAIL_STRING_WRAPPER_HPP
#define JSONCONS_DETAIL_STRING_WRAPPER_HPP

#include <stdexcept>
#include <string>
#include <exception>
#include <ostream>
#include <cstring> // std::memcpy
#include <memory> // std::allocator
#include <jsoncons/config/compiler_support.hpp>

namespace jsoncons { 
namespace detail {

    // From boost 1_71
    template <class T, class U>
    T launder_cast(U* u)
    {
    #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
        return std::launder(reinterpret_cast<T>(u));
    #elif defined(__GNUC__) &&  (__GNUC__ * 100 + __GNUC_MINOR__) > 800
        return __builtin_launder(reinterpret_cast<T>(u));
    #else
        return reinterpret_cast<T>(u);
    #endif
    }

    // string_wrapper

    template <class CharT,class Allocator>
    class string_wrapper
    {
    public:
        using char_type = CharT;
    private:
        struct str_base_t
        {
            Allocator alloc_;
        
            Allocator& get_allocator() 
            {
                return alloc_;
            }

            const Allocator& get_allocator() const
            {
                return alloc_;
            }

            str_base_t(const Allocator& alloc)
                : alloc_(alloc)
            {
            }

            ~str_base_t() noexcept = default;
        };

        struct str_t : public str_base_t
        {
            typedef typename std::allocator_traits<Allocator>::template rebind_alloc<CharT> allocator_type;  
            using allocator_traits_type = std::allocator_traits<allocator_type>;
            using pointer = typename allocator_traits_type::pointer;

            pointer p_;
            std::size_t length_;

            ~str_t() noexcept = default; 

            const char_type* c_str() const { return type_traits::to_plain_pointer(p_); }
            const char_type* data() const { return type_traits::to_plain_pointer(p_); }
            std::size_t length() const { return length_; }
        
            str_t(const Allocator& alloc)
                : str_base_t(alloc), p_(nullptr), length_(0)
            {

            }

            str_t(const str_t&) = delete;
            str_t& operator=(const str_t&) = delete;

        };

        typedef typename std::allocator_traits<Allocator>::template rebind_alloc<char> byte_allocator_type;  
        using byte_pointer = typename std::allocator_traits<byte_allocator_type>::pointer;

        typedef typename std::allocator_traits<Allocator>::template rebind_alloc<str_t> string_allocator_type;  
        using string_pointer = typename std::allocator_traits<string_allocator_type>::pointer;

        struct storage_t
        {
            str_t data;
            char_type c[1];
        };
        typedef typename std::aligned_storage<sizeof(storage_t), alignof(storage_t)>::type json_storage_kind;

        string_pointer ptr_;
    public:
        string_wrapper() = default;

        string_wrapper(string_pointer ptr)
            : ptr_(ptr)
        {
        }

        string_wrapper(const char_type* data, std::size_t length, const Allocator& a) 
        {
            ptr_ = create(data,length,a);
        }

        string_wrapper(const string_wrapper& val) 
        {
            ptr_ = create(val.data(),val.length(),val.get_allocator());
        }

        string_wrapper(const string_wrapper& val, const Allocator& a) 
        {
            ptr_ = create(val.data(),val.length(),a);
        }

        ~string_wrapper() noexcept
        {
            if (ptr_ != nullptr)
            {
                destroy(ptr_);
            }
        }

        void swap(string_wrapper& other) noexcept
        {
            std::swap(ptr_,other.ptr_);
        }

        const char_type* data() const
        {
            return ptr_->data();
        }

        const char_type* c_str() const
        {
            return ptr_->c_str();
        }

        std::size_t length() const
        {
            return ptr_->length();
        }

        Allocator get_allocator() const
        {
            return ptr_->get_allocator();
        }
    private:
        static size_t aligned_size(std::size_t n)
        {
            return sizeof(json_storage_kind) + n;
        }

        static string_pointer create(const char_type* s, std::size_t length, const Allocator& alloc)
        {
            std::size_t mem_size = aligned_size(length*sizeof(char_type));

            byte_allocator_type byte_alloc(alloc);
            byte_pointer ptr = byte_alloc.allocate(mem_size);

            char* storage = type_traits::to_plain_pointer(ptr);
            str_t* ps = new(storage)str_t(byte_alloc);

            auto psa = launder_cast<storage_t*>(storage); 

            CharT* p = new(&psa->c)char_type[length + 1];
            std::memcpy(p, s, length*sizeof(char_type));
            p[length] = 0;
            ps->p_ = std::pointer_traits<typename str_t::pointer>::pointer_to(*p);
            ps->length_ = length;
            return std::pointer_traits<string_pointer>::pointer_to(*ps);
        }

        static void destroy(string_pointer ptr)
        {
            str_t* rawp = type_traits::to_plain_pointer(ptr);

            char* p = launder_cast<char*>(rawp);

            std::size_t mem_size = aligned_size(ptr->length_*sizeof(char_type));
            byte_allocator_type byte_alloc(ptr->get_allocator());
            byte_alloc.deallocate(p,mem_size);
        }
    };

    // tagged_string_wrapper

    template <class CharT,class Allocator>
    class tagged_string_wrapper
    {
    public:
        using char_type = CharT;
    private:
        struct str_base_t
        {
            Allocator alloc_;

            Allocator& get_allocator() 
            {
                return alloc_;
            }

            const Allocator& get_allocator() const
            {
                return alloc_;
            }

            str_base_t(const Allocator& alloc)
                : alloc_(alloc)
            {
            }

            ~str_base_t() noexcept = default;
        };

        struct str_t : public str_base_t
        {
            typedef typename std::allocator_traits<Allocator>::template rebind_alloc<CharT> allocator_type;  
            using allocator_traits_type = std::allocator_traits<allocator_type>;
            using pointer = typename allocator_traits_type::pointer;

            pointer p_;
            std::size_t length_;
            uint64_t tag_;

            ~str_t() noexcept = default; 

            const char_type* c_str() const { return type_traits::to_plain_pointer(p_); }
            const char_type* data() const { return type_traits::to_plain_pointer(p_); }
            std::size_t length() const { return length_; }
            uint64_t tag() const { return tag_; }

            str_t(uint64_t tag, const Allocator& alloc)
                : str_base_t(alloc), p_(nullptr), length_(0), tag_(tag)
            {

            }

            str_t(const str_t&) = delete;
            str_t& operator=(const str_t&) = delete;

        };

        typedef typename std::allocator_traits<Allocator>::template rebind_alloc<char> byte_allocator_type;  
        using byte_pointer = typename std::allocator_traits<byte_allocator_type>::pointer;

        typedef typename std::allocator_traits<Allocator>::template rebind_alloc<str_t> string_allocator_type;  
        using string_pointer = typename std::allocator_traits<string_allocator_type>::pointer;

        struct storage_t
        {
            str_t data;
            char_type c[1];
        };
        typedef typename std::aligned_storage<sizeof(storage_t), alignof(storage_t)>::type json_storage_kind;

        string_pointer ptr_;
    public:
        tagged_string_wrapper() = default;

        tagged_string_wrapper(string_pointer ptr)
            : ptr_(ptr)
        {
        }

        tagged_string_wrapper(const char_type* data, std::size_t length, uint64_t tag, const Allocator& alloc) 
        {
            ptr_ = create(data, length, tag, alloc);
        }

        tagged_string_wrapper(const tagged_string_wrapper& val) 
        {
            ptr_ = create(val.data(), val.length(), val.tag(), val.get_allocator());
        }

        tagged_string_wrapper(const tagged_string_wrapper& val, const Allocator& alloc) 
        {
            ptr_ = create(val.data(), val.length(), val.tag(), alloc);
        }

        ~tagged_string_wrapper() noexcept
        {
            if (ptr_ != nullptr)
            {
                destroy(ptr_);
            }
        }

        void swap(tagged_string_wrapper& other) noexcept
        {
            std::swap(ptr_,other.ptr_);
        }

        const char_type* data() const
        {
            return ptr_->data();
        }

        const char_type* c_str() const
        {
            return ptr_->c_str();
        }

        std::size_t length() const
        {
            return ptr_->length();
        }

        uint64_t tag() const
        {
            return ptr_->tag();
        }

        Allocator get_allocator() const
        {
            return ptr_->get_allocator();
        }
    private:
        static size_t aligned_size(std::size_t n)
        {
            return sizeof(json_storage_kind) + n;
        }

        static string_pointer create(const char_type* s, std::size_t length, uint64_t tag, const Allocator& alloc)
        {
            std::size_t mem_size = aligned_size(length*sizeof(char_type));

            byte_allocator_type byte_alloc(alloc);
            byte_pointer ptr = byte_alloc.allocate(mem_size);

            char* storage = type_traits::to_plain_pointer(ptr);
            str_t* ps = new(storage)str_t(tag, byte_alloc);

            auto psa = launder_cast<storage_t*>(storage); 

            CharT* p = new(&psa->c)char_type[length + 1];
            std::memcpy(p, s, length*sizeof(char_type));
            p[length] = 0;
            ps->p_ = std::pointer_traits<typename str_t::pointer>::pointer_to(*p);
            ps->length_ = length;
            return std::pointer_traits<string_pointer>::pointer_to(*ps);
        }

        static void destroy(string_pointer ptr)
        {
            str_t* rawp = type_traits::to_plain_pointer(ptr);

            char* p = launder_cast<char*>(rawp);

            std::size_t mem_size = aligned_size(ptr->length_*sizeof(char_type));
            byte_allocator_type byte_alloc(ptr->get_allocator());
            byte_alloc.deallocate(p,mem_size);
        }
    };

} // namespace detail
} // namespace jsoncons

#endif