diff --git a/.idea/misc.xml b/.idea/misc.xml index a04218a..618a927 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,6 +10,7 @@ + \ No newline at end of file diff --git a/include/uvcxx/buf.h b/include/uvcxx/buf.h index b789b01..2ccf119 100644 --- a/include/uvcxx/buf.h +++ b/include/uvcxx/buf.h @@ -11,6 +11,8 @@ #include +#include "inner/base.h" + namespace uv { namespace inner { /** @@ -21,8 +23,6 @@ namespace uv { using self = buf_t; using supper = uv_buf_t; - using raw_t = uv_buf_t; - using base_t = decltype(base); using len_t = decltype(len); @@ -81,10 +81,6 @@ namespace uv { this->capacity = len; } } - - operator raw_t *() { return this; } - - operator raw_t *() const { return (raw_t *) this; } }; } @@ -92,61 +88,53 @@ namespace uv { * This type is a reference type, and all copied objects use the same buffer, * which is not thread-safe. */ - class buf_t { + class buf_t : public uvcxx::shared_raw_base_t { public: using self = buf_t; + using supper = uvcxx::shared_raw_base_t; using raw_t = uv_buf_t; - buf_t(std::nullptr_t) {} + buf_t(std::nullptr_t) : supper(nullptr) {} - buf_t() : m_raw(std::make_shared()) {} + buf_t() : supper(std::make_shared()) {} template>> explicit buf_t(Size size) - : m_raw(std::make_shared(size)) {} + : supper(std::make_shared(size)) {} - explicit operator bool() const { return m_raw && m_raw->operator bool(); } - - void free() { m_raw->free(); } + void free() { raw()->free(); } template>> - void malloc(Size size) { m_raw->malloc(size); } + void malloc(Size size) { raw()->malloc(size); } template>> - void realloc(Size size) { m_raw->realloc(size); } + void realloc(Size size) { raw()->realloc(size); } template>> - void reserve(Size size) { m_raw->reserve(size); } + void reserve(Size size) { raw()->reserve(size); } - char *base() { return m_raw->base; } + char *base() { return raw()->base; } [[nodiscard]] - const char *base() const { return m_raw->base; } + const char *base() const { return raw()->base; } [[nodiscard]] - size_t len() const { return size_t(m_raw->len); } + size_t len() const { return size_t(raw()->len); } [[nodiscard]] - size_t capacity() const { return size_t(m_raw->len); } + size_t capacity() const { return size_t(raw()->capacity); } - char *data() { return m_raw->base; } + char *data() { return raw()->base; } [[nodiscard]] - const char *data() const { return m_raw->base; } + const char *data() const { return raw()->base; } [[nodiscard]] - size_t size() const { return size_t(m_raw->len); } + size_t size() const { return size_t(raw()->len); } template>> - void resize(Size size) { m_raw->realloc(size); } - - operator raw_t *() { return m_raw.get(); } - - operator raw_t *() const { return m_raw.get(); } - - private: - std::shared_ptr m_raw; + void resize(Size size) { raw()->realloc(size); } }; } diff --git a/include/uvcxx/inner/base.h b/include/uvcxx/inner/base.h index 834217e..ca4abd7 100644 --- a/include/uvcxx/inner/base.h +++ b/include/uvcxx/inner/base.h @@ -10,8 +10,15 @@ #include namespace uvcxx { + /** + * The base class of all `libuvcxx` wrapper types. + * It can be used for type detection in certain scenarios. + */ + class base_t { + }; + template - class shared_raw_base_t { + class shared_raw_base_t : public base_t { public: using self = shared_raw_base_t; using raw_t = T; @@ -24,6 +31,7 @@ namespace uvcxx { operator raw_t *() const { return m_raw.get(); } + protected: raw_t *raw() { return m_raw.get(); } raw_t *raw() const { return m_raw.get(); } @@ -51,7 +59,7 @@ namespace uvcxx { : m_raw(std::move(raw)) {} explicit shared_raw_base_t(borrow_t borrow) - : m_raw(std::shared_ptr(borrow.raw, [](T *) {})) {} + : m_raw(std::shared_ptr(borrow.raw, [](raw_t *) {})) {} template static std::shared_ptr cast_raw(const std::shared_ptr &p) { @@ -75,7 +83,7 @@ namespace uvcxx { }; template - class inherit_raw_base_t : public T { + class inherit_raw_base_t : public base_t, public T { public: using self = inherit_raw_base_t; using raw_t = T; @@ -84,6 +92,7 @@ namespace uvcxx { operator raw_t *() const { return const_cast(this); } + protected: raw_t *raw() { return (raw_t *) (*this); } raw_t *raw() const { return (raw_t *) (*this); } @@ -96,7 +105,7 @@ namespace uvcxx { }; template - class extend_raw_base_t : public T { + class extend_raw_base_t : public base_t { public: using self = extend_raw_base_t; using raw_t = T; @@ -105,6 +114,7 @@ namespace uvcxx { operator raw_t *() const { return const_cast(&m_raw); } + protected: raw_t *raw() { return (raw_t *) (*this); } raw_t *raw() const { return (raw_t *) (*this); } @@ -116,7 +126,7 @@ namespace uvcxx { K *raw() const { return (K *) (raw()); } private: - T m_raw{}; + raw_t m_raw{}; }; } diff --git a/include/uvcxx/lib.h b/include/uvcxx/lib.h index 30f4460..eae6d58 100644 --- a/include/uvcxx/lib.h +++ b/include/uvcxx/lib.h @@ -12,15 +12,15 @@ #include #include "cxx/except.h" +#include "inner/base.h" namespace uv { /** * This class is not thread-safe. */ - class lib_t { + class lib_t : uvcxx::extend_raw_base_t { public: using self = lib_t; - using raw_t = uv_lib_t; lib_t(const lib_t &) = delete; @@ -87,12 +87,7 @@ namespace uv { return func; } - operator raw_t *() { return &m_lib; } - - operator raw_t *() const { return (raw_t *) &m_lib; } - private: - uv_lib_t m_lib{}; bool m_opened{false}; }; }