GCC Code Coverage Report


Directory: libs/json/include/boost/json/
File: detail/stack.hpp
Date: 2025-12-23 17:22:01
Exec Total Coverage
Lines: 33 33 100.0%
Functions: 181 248 73.0%
Branches: 3 6 50.0%

Line Branch Exec Source
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 80138 push(T const& t)
59 {
60 80138 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 80138 reserve(size_ + n);
67 80134 std::memcpy(
68 80134 base_ + size_, &t, n);
69 80134 size_ += n;
70 80134 }
71
72 template<class T>
73 void
74 568256 push_unchecked(T const& t)
75 {
76 568256 auto const n = sizeof(T);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284128 times.
568256 BOOST_ASSERT(size_ + n <= cap_);
78 568256 std::memcpy(
79 568256 base_ + size_, &t, n);
80 568256 size_ += n;
81 568256 }
82
83 template<class T>
84 void
85 927128 peek(T& t)
86 {
87 927128 auto const n = sizeof(T);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 463564 times.
927128 BOOST_ASSERT(size_ >= n);
89 927128 std::memcpy(&t,
90 927128 base_ + size_ - n, n);
91 927128 }
92
93 template<class T>
94 void
95 641428 pop(T& t)
96 {
97 641428 auto const n = sizeof(T);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320714 times.
641428 BOOST_ASSERT(size_ >= n);
99 641428 size_ -= n;
100 641428 std::memcpy(
101 641428 &t, base_ + size_, n);
102 641428 }
103 };
104
105 } // detail
106 } // namespace json
107 } // namespace boost
108
109 #endif
110