aboutsummaryrefslogtreecommitdiffstats
path: root/bindings/cpp/include/serd/detail/Wrapper.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-16 10:26:47 -0400
committerDavid Robillard <d@drobilla.net>2021-03-08 23:36:35 -0500
commitdc5ccf0913fe716059bb633d8a183cbddc634a7a (patch)
treee2bd280213c648e6a4ff838bd344981f6a60bf4c /bindings/cpp/include/serd/detail/Wrapper.hpp
parent0c347c9701af4595a68bb37eb7c69b5db2d452f8 (diff)
downloadserd-dc5ccf0913fe716059bb633d8a183cbddc634a7a.tar.gz
serd-dc5ccf0913fe716059bb633d8a183cbddc634a7a.tar.bz2
serd-dc5ccf0913fe716059bb633d8a183cbddc634a7a.zip
WIP: Add C++ bindings
Diffstat (limited to 'bindings/cpp/include/serd/detail/Wrapper.hpp')
-rw-r--r--bindings/cpp/include/serd/detail/Wrapper.hpp141
1 files changed, 141 insertions, 0 deletions
diff --git a/bindings/cpp/include/serd/detail/Wrapper.hpp b/bindings/cpp/include/serd/detail/Wrapper.hpp
new file mode 100644
index 00000000..10395ec4
--- /dev/null
+++ b/bindings/cpp/include/serd/detail/Wrapper.hpp
@@ -0,0 +1,141 @@
+/*
+ Copyright 2019-2020 David Robillard <http://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_WRAPPER_HPP
+#define SERD_DETAIL_WRAPPER_HPP
+
+#include <cstddef>
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+namespace serd {
+namespace detail {
+
+template<typename T>
+class Optional;
+
+/// Free function for a C object
+template<typename T>
+using FreeFunc = void (*)(T*);
+
+template<class T>
+using Mutable = typename std::remove_const<T>::type;
+
+/**
+ Simple overhead-free deleter for a C object.
+
+ Can be used with const or mutable pointers, but only mutable pointers will
+ be freed. This makes it simple to wrap APIs where constness conveys
+ ownership, but can not handle unowned mutable pointers.
+*/
+template<typename T, void Free(Mutable<T>*)>
+struct BasicDeleter {
+ template<typename = std::enable_if<!std::is_const<T>::value>>
+ void operator()(typename std::remove_const<T>::type* ptr)
+ {
+ Free(ptr);
+ }
+
+ template<typename = std::enable_if<std::is_const<T>::value>>
+ void operator()(const T*)
+ {}
+};
+
+/// Ownership for `DynamicDeleter`
+enum class Ownership { owned, view };
+
+/**
+ Deleter for a C object that can handle dynamic ownership.
+
+ Unlike `BasicDeleter`, this can be used to handle non-owned references to
+ mutable objects, at the cost of an extra word for tracking the ownership
+ (since constness in the type can't convey this information).
+*/
+template<typename T, void Free(Mutable<T>*)>
+struct DynamicDeleter : BasicDeleter<T, Free> {
+ // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
+ DynamicDeleter(Ownership ownership)
+ : _ownership{ownership}
+ {}
+
+ void operator()(T* ptr)
+ {
+ if (_ownership == Ownership::owned) {
+ BasicDeleter<T, Free>::operator()(ptr);
+ }
+ }
+
+private:
+ Ownership _ownership;
+};
+
+/// Generic C++ wrapper for a C object
+template<typename T, class Deleter>
+class Wrapper
+{
+public:
+ using CType = T;
+ using UPtr = std::unique_ptr<T, Deleter>;
+
+ explicit Wrapper(T* ptr)
+ : _ptr(ptr, Deleter{})
+ {}
+
+ Wrapper(T* ptr, Deleter deleter)
+ : _ptr(ptr, std::move(deleter))
+ {}
+
+ explicit Wrapper(UPtr ptr)
+ : _ptr(std::move(ptr))
+ {}
+
+ Wrapper(Wrapper&&) noexcept = default;
+ Wrapper& operator=(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<T>;
+
+ explicit Wrapper(std::nullptr_t)
+ : _ptr(nullptr)
+ {}
+
+ void reset() { _ptr.reset(); }
+
+ std::unique_ptr<T, Deleter> _ptr;
+};
+
+template<typename T, void Free(Mutable<T>*)>
+class BasicWrapper : public Wrapper<T, BasicDeleter<T, Free>>
+{
+public:
+ explicit BasicWrapper(T* ptr)
+ : Wrapper<T, BasicDeleter<T, Free>>{ptr}
+ {}
+};
+
+} // namespace detail
+} // namespace serd
+
+#endif // SERD_DETAIL_WRAPPER_HPP