/* 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_STATICWRAPPER_HPP #define SERD_DETAIL_STATICWRAPPER_HPP // IWYU pragma: no_include "serd/serd.h" #include "serd/detail/Wrapper.hpp" #include namespace serd { namespace detail { /** Simple overhead-free deleter for a C object. Can be used with const or mutable pointers, but only mutable pointers will be freed. In other words, mutability implies ownership, and this can not handle unowned mutable pointers. @ingroup serdpp_detail */ template*)> struct StaticDeleter { template::value>> void operator()(Mutable* const ptr) { Free(ptr); } template::value>> void operator()(const T*) {} }; /// Simple overhead-free wrapper for a C object that can free itself template*)> class StaticWrapper : public Wrapper> { public: explicit StaticWrapper(T* const ptr) : Wrapper>{ptr} {} }; /** Simple overhead-free deleter for a C object. Can be used with const or mutable pointers, but only mutable pointers will be freed. In other words, mutability implies ownership, and this can not handle unowned mutable pointers. @ingroup serdpp_detail */ template*)> struct StaticAllocatedDeleter { template::value>> void operator()(Mutable* const ptr) { Free(nullptr, ptr); } template::value>> void operator()(const T*) {} }; /// Simple overhead-free wrapper for a C object that uses an allocator template*)> class StaticAllocatedWrapper : public Wrapper> { public: explicit StaticAllocatedWrapper(T* const ptr) : Wrapper>{ptr} {} }; } // namespace detail } // namespace serd #endif // SERD_DETAIL_STATICWRAPPER_HPP