File indexing completed on 2024-05-05 04:38:46
0001 /* 0002 SPDX-FileCopyrightText: 2021 Igor Kushnir <igorkuo@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef KDEVPLATFORM_OWNING_RAW_POINTER_CONTAINER_H 0008 #define KDEVPLATFORM_OWNING_RAW_POINTER_CONTAINER_H 0009 0010 #include <utility> 0011 0012 namespace KDevelop { 0013 0014 /** 0015 * A RAII wrapper for a container of owning raw pointers. 0016 * 0017 * Storing an OwningRawPointerContainer<C> in a class is equivalent to storing 0018 * C @a c itself and calling qDeleteAll(@a c) in the destructor. The only usage 0019 * difference is dereferencing OwningRawPointerContainer via operator* or operator->. 0020 */ 0021 template<typename C> 0022 class OwningRawPointerContainer 0023 { 0024 public: 0025 OwningRawPointerContainer() = default; 0026 0027 OwningRawPointerContainer(OwningRawPointerContainer&& other) 0028 : m_c{std::move(other.m_c)} 0029 { 0030 other.m_c = {}; 0031 } 0032 0033 OwningRawPointerContainer(const OwningRawPointerContainer&) = delete; 0034 0035 ~OwningRawPointerContainer() 0036 { 0037 for (const auto* ptr : std::as_const(m_c)) { 0038 delete ptr; 0039 } 0040 } 0041 0042 OwningRawPointerContainer& operator=(OwningRawPointerContainer&& other) 0043 { 0044 m_c = std::move(other.m_c); 0045 other.m_c = {}; 0046 } 0047 0048 OwningRawPointerContainer& operator=(const OwningRawPointerContainer&) = delete; 0049 0050 const C& operator*() const 0051 { 0052 return m_c; 0053 } 0054 C& operator*() 0055 { 0056 return m_c; 0057 } 0058 0059 const C* operator->() const 0060 { 0061 return &m_c; 0062 } 0063 C* operator->() 0064 { 0065 return &m_c; 0066 } 0067 0068 private: 0069 C m_c{}; 0070 }; 0071 } 0072 0073 #endif // KDEVPLATFORM_OWNING_RAW_POINTER_CONTAINER_H