/* Copyright 2019 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_WRAPPER_HPP #define SERD_DETAIL_WRAPPER_HPP #include "serd/serd.h" #include #include namespace serd { namespace detail { template class Optional; // Free function for a C object template using FreeFunc = void (*)(T*); template using Mutable = typename std::remove_const::type; /// Callable deleter for a C object, noopt for const pointers template *)> struct Deleter { template ::value>> void operator()(typename std::remove_const::type* ptr) { Free(ptr); } template ::value>> void operator()(const T*) { } }; /// Generic C++ wrapper for a C object template *)> class Wrapper { public: using CType = T; explicit Wrapper(T* ptr) : _ptr(ptr) {} Wrapper(Wrapper&& wrapper) noexcept = default; Wrapper& operator=(Wrapper&& wrapper) noexcept = default; Wrapper(const Wrapper&) = delete; Wrapper& operator=(const Wrapper&) = delete; ~Wrapper() = default; T* cobj() { return _ptr.get(); } const T* cobj() const { return _ptr.get(); } protected: friend class detail::Optional; explicit Wrapper(std::nullptr_t) : _ptr(nullptr) {} void reset() { _ptr.reset(); } std::unique_ptr> _ptr; }; } // namespace detail } // namespace serd #endif // SERD_DETAIL_WRAPPER_HPP