Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:02:51

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_UNIQUE_PTR_HPP_INCLUDED
0009 #define CATCH_UNIQUE_PTR_HPP_INCLUDED
0010 
0011 #include <cassert>
0012 #include <type_traits>
0013 
0014 #include <catch2/internal/catch_move_and_forward.hpp>
0015 
0016 namespace Catch {
0017 namespace Detail {
0018     /**
0019      * A reimplementation of `std::unique_ptr` for improved compilation performance
0020      *
0021      * Does not support arrays nor custom deleters.
0022      */
0023     template <typename T>
0024     class unique_ptr {
0025         T* m_ptr;
0026     public:
0027         constexpr unique_ptr(std::nullptr_t = nullptr):
0028             m_ptr{}
0029         {}
0030         explicit constexpr unique_ptr(T* ptr):
0031             m_ptr(ptr)
0032         {}
0033 
0034         template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
0035         unique_ptr(unique_ptr<U>&& from):
0036             m_ptr(from.release())
0037         {}
0038 
0039         template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
0040         unique_ptr& operator=(unique_ptr<U>&& from) {
0041             reset(from.release());
0042 
0043             return *this;
0044         }
0045 
0046         unique_ptr(unique_ptr const&) = delete;
0047         unique_ptr& operator=(unique_ptr const&) = delete;
0048 
0049         unique_ptr(unique_ptr&& rhs) noexcept:
0050             m_ptr(rhs.m_ptr) {
0051             rhs.m_ptr = nullptr;
0052         }
0053         unique_ptr& operator=(unique_ptr&& rhs) noexcept {
0054             reset(rhs.release());
0055 
0056             return *this;
0057         }
0058 
0059         ~unique_ptr() {
0060             delete m_ptr;
0061         }
0062 
0063         T& operator*() {
0064             assert(m_ptr);
0065             return *m_ptr;
0066         }
0067         T const& operator*() const {
0068             assert(m_ptr);
0069             return *m_ptr;
0070         }
0071         T* operator->() noexcept {
0072             assert(m_ptr);
0073             return m_ptr;
0074         }
0075         T const* operator->() const noexcept {
0076             assert(m_ptr);
0077             return m_ptr;
0078         }
0079 
0080         T* get() { return m_ptr; }
0081         T const* get() const { return m_ptr; }
0082 
0083         void reset(T* ptr = nullptr) {
0084             delete m_ptr;
0085             m_ptr = ptr;
0086         }
0087 
0088         T* release() {
0089             auto temp = m_ptr;
0090             m_ptr = nullptr;
0091             return temp;
0092         }
0093 
0094         explicit operator bool() const {
0095             return m_ptr;
0096         }
0097 
0098         friend void swap(unique_ptr& lhs, unique_ptr& rhs) {
0099             auto temp = lhs.m_ptr;
0100             lhs.m_ptr = rhs.m_ptr;
0101             rhs.m_ptr = temp;
0102         }
0103     };
0104 
0105     //! Specialization to cause compile-time error for arrays
0106     template <typename T>
0107     class unique_ptr<T[]>;
0108 
0109     template <typename T, typename... Args>
0110     unique_ptr<T> make_unique(Args&&... args) {
0111         return unique_ptr<T>(new T(CATCH_FORWARD(args)...));
0112     }
0113 
0114 
0115 } // end namespace Detail
0116 } // end namespace Catch
0117 
0118 #endif // CATCH_UNIQUE_PTR_HPP_INCLUDED