Line data Source code
1 : //
2 : // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 : //
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)
6 : //
7 : // Official repository: https://github.com/boostorg/json
8 : //
9 :
10 : #ifndef BOOST_JSON_DETAIL_STACK_HPP
11 : #define BOOST_JSON_DETAIL_STACK_HPP
12 :
13 : #include <boost/json/detail/config.hpp>
14 : #include <boost/json/storage_ptr.hpp>
15 : #include <cstring>
16 :
17 : namespace boost {
18 : namespace json {
19 : namespace detail {
20 :
21 : class stack
22 : {
23 : storage_ptr sp_;
24 : std::size_t cap_ = 0;
25 : std::size_t size_ = 0;
26 : unsigned char* base_ = nullptr;
27 : unsigned char* buf_ = nullptr;
28 :
29 : public:
30 : BOOST_JSON_DECL
31 : ~stack();
32 :
33 2166527 : stack() = default;
34 :
35 : stack(
36 : storage_ptr sp,
37 : unsigned char* buf,
38 : std::size_t buf_size) noexcept;
39 :
40 : bool
41 3243963 : empty() const noexcept
42 : {
43 3243963 : return size_ == 0;
44 : }
45 :
46 : void
47 4173925 : clear() noexcept
48 : {
49 4173925 : size_ = 0;
50 4173925 : }
51 :
52 : BOOST_JSON_DECL
53 : void
54 : reserve(std::size_t n);
55 :
56 : template<class T>
57 : void
58 40069 : push(T const& t)
59 : {
60 40069 : auto const n = sizeof(T);
61 : // If this assert goes off, it
62 : // means the calling code did not
63 : // reserve enough to prevent a
64 : // reallocation.
65 : //BOOST_ASSERT(cap_ >= size_ + n);
66 40069 : reserve(size_ + n);
67 40067 : std::memcpy(
68 40067 : base_ + size_, &t, n);
69 40067 : size_ += n;
70 40067 : }
71 :
72 : template<class T>
73 : void
74 284128 : push_unchecked(T const& t)
75 : {
76 284128 : auto const n = sizeof(T);
77 284128 : BOOST_ASSERT(size_ + n <= cap_);
78 284128 : std::memcpy(
79 284128 : base_ + size_, &t, n);
80 284128 : size_ += n;
81 284128 : }
82 :
83 : template<class T>
84 : void
85 463564 : peek(T& t)
86 : {
87 463564 : auto const n = sizeof(T);
88 463564 : BOOST_ASSERT(size_ >= n);
89 463564 : std::memcpy(&t,
90 463564 : base_ + size_ - n, n);
91 463564 : }
92 :
93 : template<class T>
94 : void
95 320714 : pop(T& t)
96 : {
97 320714 : auto const n = sizeof(T);
98 320714 : BOOST_ASSERT(size_ >= n);
99 320714 : size_ -= n;
100 320714 : std::memcpy(
101 320714 : &t, base_ + size_, n);
102 320714 : }
103 : };
104 :
105 : } // detail
106 : } // namespace json
107 : } // namespace boost
108 :
109 : #endif
|