|
| 1 | +// |
| 2 | +// Copyright (c) 2019 Vinnie Falco ([email protected]) |
| 3 | +// Copyright (c) 2020 Krystian Stasiowski ([email protected]) |
| 4 | +// |
| 5 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | +// |
| 8 | +// Official repository: https://github.com/boostorg/json |
| 9 | +// |
| 10 | + |
| 11 | +#ifndef BOOST_JSON_DETAIL_WRITER_HPP |
| 12 | +#define BOOST_JSON_DETAIL_WRITER_HPP |
| 13 | + |
| 14 | +#include <boost/json/detail/stack.hpp> |
| 15 | +#include <boost/json/serialize_options.hpp> |
| 16 | +#include <boost/json/string_view.hpp> |
| 17 | + |
| 18 | +namespace boost { |
| 19 | +namespace json { |
| 20 | +namespace detail { |
| 21 | + |
| 22 | +struct writer |
| 23 | +{ |
| 24 | + char* dest_; |
| 25 | + char const* end_; |
| 26 | + |
| 27 | +public: |
| 28 | + using resume_fn = bool(*)(writer&, serialize_options const&); |
| 29 | + |
| 30 | + detail::stack stack; |
| 31 | + char temp_[29]; |
| 32 | + |
| 33 | + writer() = default; |
| 34 | + |
| 35 | + writer( |
| 36 | + storage_ptr sp, |
| 37 | + unsigned char* buf, |
| 38 | + std::size_t buf_size) noexcept |
| 39 | + : stack( |
| 40 | + std::move(sp), |
| 41 | + buf, |
| 42 | + buf_size) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + char* |
| 47 | + data() noexcept |
| 48 | + { |
| 49 | + return dest_; |
| 50 | + } |
| 51 | + |
| 52 | + // provide an output buffer |
| 53 | + void |
| 54 | + prepare( |
| 55 | + char* dest, |
| 56 | + std::size_t size) |
| 57 | + { |
| 58 | + dest_ = dest; |
| 59 | + end_ = dest + size; |
| 60 | + } |
| 61 | + |
| 62 | + // return true if there is no space |
| 63 | + bool |
| 64 | + empty() const noexcept |
| 65 | + { |
| 66 | + return dest_ == end_; |
| 67 | + } |
| 68 | + |
| 69 | + // return the amount of space available |
| 70 | + std::size_t |
| 71 | + available() const noexcept |
| 72 | + { |
| 73 | + return static_cast< |
| 74 | + std::size_t>(end_ - dest_); |
| 75 | + } |
| 76 | + |
| 77 | + // return true if there is space |
| 78 | + bool |
| 79 | + append(char c) noexcept |
| 80 | + { |
| 81 | + if(! empty()) |
| 82 | + { |
| 83 | + *dest_++ = c; |
| 84 | + return true; |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // append one char |
| 90 | + void |
| 91 | + append_unsafe( |
| 92 | + char c) noexcept |
| 93 | + { |
| 94 | + BOOST_ASSERT(! empty()); |
| 95 | + *dest_++ = c; |
| 96 | + } |
| 97 | + |
| 98 | + // append chars in s, unchecked |
| 99 | + void |
| 100 | + append_unsafe( |
| 101 | + char const* s, |
| 102 | + std::size_t n) noexcept |
| 103 | + { |
| 104 | + BOOST_ASSERT(available() >= n); |
| 105 | + std::memcpy(dest_, s, n); |
| 106 | + dest_ += n; |
| 107 | + } |
| 108 | + |
| 109 | + // push a resume function |
| 110 | + void |
| 111 | + push_resume(resume_fn fn) |
| 112 | + { |
| 113 | + stack.push(fn); |
| 114 | + } |
| 115 | + |
| 116 | + // pop and invoke a resume function |
| 117 | + // or return true if stack is empty |
| 118 | + bool |
| 119 | + do_resume(serialize_options const& opts) |
| 120 | + { |
| 121 | + if(! stack.empty()) |
| 122 | + { |
| 123 | + resume_fn fn; |
| 124 | + stack.pop(fn); |
| 125 | + return fn(*this, opts); |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +} // detail |
| 132 | +} // json |
| 133 | +} // boost |
| 134 | + |
| 135 | +#endif |
0 commit comments