/* Copyright 2019-2021 David Robillard Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef SERD_DETAIL_COPYABLE_HPP #define SERD_DETAIL_COPYABLE_HPP // IWYU pragma: no_include "serd/serd.h" #include "serd/detail/DynamicWrapper.hpp" #include "serd/detail/StaticWrapper.hpp" #include "serd/detail/Wrapper.hpp" #include #include #include #include namespace serd { namespace detail { /** @addtogroup serdpp_detail @{ */ /// Copy function for a C object template using CopyFunc = T* (*)(const T*); template* Copy(SerdAllocator*, const T*)> typename std::enable_if_t::value, T>* copy(const T* ptr) { return ptr; // Making a view (const reference), do not copy } template* Copy(SerdAllocator*, const T*)> typename std::enable_if_t::value, T>* copy(const T* ptr) { return ptr ? Copy(nullptr, ptr) : nullptr; // Making a mutable wrapper, copy } /** Generic wrapper for a "basic" copyable object. This wraps objects with simple ownership semantics where a const pointer is never owned, and a mutable pointer is owned. This has no space overhead compared to a raw pointer since the ownership is encoded in the type. */ template* Copy(SerdAllocator*, const T*), bool Equals(const T*, const T*), void Free(Mutable*)> class StaticCopyable : public StaticWrapper { public: using Deleter = StaticDeleter; using Base = StaticWrapper; explicit StaticCopyable(T* ptr) : Base{ptr} {} StaticCopyable(const StaticCopyable& wrapper) : Base(copy(wrapper.cobj())) {} template*)> explicit StaticCopyable(const StaticCopyable& wrapper) : Base(copy(wrapper.cobj())) {} StaticCopyable(StaticCopyable&&) noexcept = default; ~StaticCopyable() noexcept = default; StaticCopyable& operator=(StaticCopyable&&) noexcept = default; StaticCopyable& operator=(const StaticCopyable& wrapper) { if (&wrapper != this) { this->_ptr = std::unique_ptr(copy(wrapper.cobj())); } return *this; } template bool operator==(const StaticCopyable& wrapper) const { return Equals(this->cobj(), wrapper.cobj()); } template bool operator!=(const StaticCopyable& wrapper) const { return !operator==(wrapper); } }; /** Generic wrapper for a "basic" copyable object. This wraps objects with simple ownership semantics where a const pointer is never owned, and a mutable pointer is owned. This has no space overhead compared to a raw pointer since the ownership is encoded in the type. */ template* Copy(SerdAllocator*, const T*), bool Equals(const T*, const T*), void Free(SerdAllocator*, Mutable*)> class StaticAllocatedCopyable : public StaticAllocatedWrapper { public: using Deleter = StaticAllocatedDeleter; using Base = StaticAllocatedWrapper; explicit StaticAllocatedCopyable(T* ptr) : Base{ptr} {} StaticAllocatedCopyable(const StaticAllocatedCopyable& wrapper) : Base(copy(wrapper.cobj())) {} template*)> explicit StaticAllocatedCopyable( const StaticAllocatedCopyable& wrapper) : Base(copy(wrapper.cobj())) {} StaticAllocatedCopyable(StaticAllocatedCopyable&&) noexcept = default; ~StaticAllocatedCopyable() noexcept = default; StaticAllocatedCopyable& operator=(StaticAllocatedCopyable&&) noexcept = default; StaticAllocatedCopyable& operator=(const StaticAllocatedCopyable& wrapper) { if (&wrapper != this) { this->_ptr = std::unique_ptr(copy(wrapper.cobj())); } return *this; } template bool operator==( const StaticAllocatedCopyable& wrapper) const { return Equals(this->cobj(), wrapper.cobj()); } template bool operator!=( const StaticAllocatedCopyable& wrapper) const { return !operator==(wrapper); } }; /** Wrapper for a "dynamic" copyable C object. This wraps objects that require dynamic tracking of the ownership. */ template* Copy(SerdAllocator*, const T*), bool Equals(const T*, const T*), void Free(Mutable*)> class DynamicCopyable : public Wrapper> { public: using Deleter = DynamicDeleter; using Base = Wrapper; explicit DynamicCopyable(std::unique_ptr ptr) : Base{std::move(ptr)} {} DynamicCopyable(const DynamicCopyable& wrapper) : Base{Copy(nullptr /* FIXME */, wrapper.cobj()), Ownership::owned} {} DynamicCopyable(DynamicCopyable&&) noexcept = default; DynamicCopyable& operator=(DynamicCopyable&&) noexcept = default; ~DynamicCopyable() noexcept = default; DynamicCopyable& operator=(const DynamicCopyable& wrapper) { if (&wrapper != this) { this->_ptr = std::unique_ptr( Copy(nullptr /* FIXME */, wrapper.cobj()), Ownership::owned); } return *this; } template bool operator==(const DynamicCopyable& wrapper) const { return Equals(this->cobj(), wrapper.cobj()); } template bool operator!=(const DynamicCopyable& wrapper) const { return !operator==(wrapper); } protected: explicit DynamicCopyable(std::nullptr_t) : Base(nullptr) {} }; /** @} */ } // namespace detail } // namespace serd #endif // SERD_DETAIL_COPYABLE_HPP