aboutsummaryrefslogtreecommitdiffstats
path: root/bindings/cpp/include/serd/detail/StaticWrapper.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-16 10:26:47 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:29 -0500
commit8b1ad067d9450b4c9c4384b6bf2859c70a6b0cce (patch)
tree2a5cc8b0031658a472c44b8fd8e95cb583d24fc7 /bindings/cpp/include/serd/detail/StaticWrapper.hpp
parent3f74afa8c0e60a778566db975894044c67a3b386 (diff)
downloadserd-8b1ad067d9450b4c9c4384b6bf2859c70a6b0cce.tar.gz
serd-8b1ad067d9450b4c9c4384b6bf2859c70a6b0cce.tar.bz2
serd-8b1ad067d9450b4c9c4384b6bf2859c70a6b0cce.zip
[WIP] Add C++ bindings
Diffstat (limited to 'bindings/cpp/include/serd/detail/StaticWrapper.hpp')
-rw-r--r--bindings/cpp/include/serd/detail/StaticWrapper.hpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/bindings/cpp/include/serd/detail/StaticWrapper.hpp b/bindings/cpp/include/serd/detail/StaticWrapper.hpp
new file mode 100644
index 00000000..9ca82118
--- /dev/null
+++ b/bindings/cpp/include/serd/detail/StaticWrapper.hpp
@@ -0,0 +1,97 @@
+/*
+ Copyright 2019-2021 David Robillard <d@drobilla.net>
+
+ 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 <type_traits>
+
+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<class T, void Free(Mutable<T>*)>
+struct StaticDeleter {
+ template<class = std::enable_if<!std::is_const<T>::value>>
+ void operator()(Mutable<T>* const ptr)
+ {
+ Free(ptr);
+ }
+
+ template<class = std::enable_if<std::is_const<T>::value>>
+ void operator()(const T*)
+ {}
+};
+
+/// Simple overhead-free wrapper for a C object that can free itself
+template<class T, void Free(std::remove_const_t<T>*)>
+class StaticWrapper : public Wrapper<T, StaticDeleter<T, Free>>
+{
+public:
+ explicit StaticWrapper(T* const ptr)
+ : Wrapper<T, StaticDeleter<T, Free>>{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<class T, void Free(SerdAllocator*, Mutable<T>*)>
+struct StaticAllocatedDeleter {
+ template<class = std::enable_if<!std::is_const<T>::value>>
+ void operator()(Mutable<T>* const ptr)
+ {
+ Free(nullptr, ptr);
+ }
+
+ template<class = std::enable_if<std::is_const<T>::value>>
+ void operator()(const T*)
+ {}
+};
+
+/// Simple overhead-free wrapper for a C object that uses an allocator
+template<class T, void Free(SerdAllocator*, std::remove_const_t<T>*)>
+class StaticAllocatedWrapper
+ : public Wrapper<T, StaticAllocatedDeleter<T, Free>>
+{
+public:
+ explicit StaticAllocatedWrapper(T* const ptr)
+ : Wrapper<T, StaticAllocatedDeleter<T, Free>>{ptr}
+ {}
+};
+
+} // namespace detail
+} // namespace serd
+
+#endif // SERD_DETAIL_STATICWRAPPER_HPP