Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:59

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/json
0008 //
0009 
0010 #ifndef BOOST_JSON_DETAIL_STACK_HPP
0011 #define BOOST_JSON_DETAIL_STACK_HPP
0012 
0013 #include <boost/json/detail/config.hpp>
0014 #include <boost/json/storage_ptr.hpp>
0015 #include <cstring>
0016 
0017 namespace boost {
0018 namespace json {
0019 namespace detail {
0020 
0021 class stack
0022 {
0023     storage_ptr sp_;
0024     std::size_t cap_ = 0;
0025     std::size_t size_ = 0;
0026     unsigned char* base_ = nullptr;
0027     unsigned char* buf_ = nullptr;
0028 
0029 public:
0030     BOOST_JSON_DECL
0031     ~stack();
0032 
0033     stack() = default;
0034 
0035     stack(
0036         storage_ptr sp,
0037         unsigned char* buf,
0038         std::size_t buf_size) noexcept;
0039 
0040     bool
0041     empty() const noexcept
0042     {
0043         return size_ == 0;
0044     }
0045 
0046     void
0047     clear() noexcept
0048     {
0049         size_ = 0;
0050     }
0051 
0052     BOOST_JSON_DECL
0053     void
0054     reserve(std::size_t n);
0055 
0056     template<class T>
0057     void
0058     push(T const& t)
0059     {
0060         auto const n = sizeof(T);
0061         // If this assert goes off, it
0062         // means the calling code did not
0063         // reserve enough to prevent a
0064         // reallocation.
0065         //BOOST_ASSERT(cap_ >= size_ + n);
0066         reserve(size_ + n);
0067         std::memcpy(
0068             base_ + size_, &t, n);
0069         size_ += n;
0070     }
0071 
0072     template<class T>
0073     void
0074     push_unchecked(T const& t)
0075     {
0076         auto const n = sizeof(T);
0077         BOOST_ASSERT(size_ + n <= cap_);
0078         std::memcpy(
0079             base_ + size_, &t, n);
0080         size_ += n;
0081     }
0082 
0083     template<class T>
0084     void
0085     peek(T& t)
0086     {
0087         auto const n = sizeof(T);
0088         BOOST_ASSERT(size_ >= n);
0089         std::memcpy(&t,
0090             base_ + size_ - n, n);
0091     }
0092 
0093     template<class T>
0094     void
0095     pop(T& t)
0096     {
0097         auto const n = sizeof(T);
0098         BOOST_ASSERT(size_ >= n);
0099         size_ -= n;
0100         std::memcpy(
0101             &t, base_ + size_, n);
0102     }
0103 };
0104 
0105 } // detail
0106 } // namespace json
0107 } // namespace boost
0108 
0109 #endif