Skip to content

Commit

Permalink
adjust so that all types are based on the same base class
Browse files Browse the repository at this point in the history
  • Loading branch information
levalup committed Jun 16, 2024
1 parent 7fe453d commit 6088837
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 19 additions & 31 deletions include/uvcxx/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <uv.h>

#include "inner/base.h"

namespace uv {
namespace inner {
/**
Expand All @@ -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);

Expand Down Expand Up @@ -81,72 +81,60 @@ namespace uv {
this->capacity = len;
}
}

operator raw_t *() { return this; }

operator raw_t *() const { return (raw_t *) this; }
};
}

/**
* 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<inner::buf_t> {
public:
using self = buf_t;
using supper = uvcxx::shared_raw_base_t<inner::buf_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<inner::buf_t>()) {}
buf_t() : supper(std::make_shared<inner::buf_t>()) {}

template<class Size, typename = typename std::enable_if_t<std::is_integral_v<Size>>>
explicit buf_t(Size size)
: m_raw(std::make_shared<inner::buf_t>(size)) {}
: supper(std::make_shared<inner::buf_t>(size)) {}

explicit operator bool() const { return m_raw && m_raw->operator bool(); }

void free() { m_raw->free(); }
void free() { raw()->free(); }

template<class Size, typename = typename std::enable_if_t<std::is_integral_v<Size>>>
void malloc(Size size) { m_raw->malloc(size); }
void malloc(Size size) { raw()->malloc(size); }

template<class Size, typename = typename std::enable_if_t<std::is_integral_v<Size>>>
void realloc(Size size) { m_raw->realloc(size); }
void realloc(Size size) { raw()->realloc(size); }

template<class Size, typename = typename std::enable_if_t<std::is_integral_v<Size>>>
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<class Size, typename = typename std::enable_if_t<std::is_integral_v<Size>>>
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<inner::buf_t> m_raw;
void resize(Size size) { raw()->realloc(size); }
};
}

Expand Down
20 changes: 15 additions & 5 deletions include/uvcxx/inner/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
#include <type_traits>

namespace uvcxx {
/**
* The base class of all `libuvcxx` wrapper types.
* It can be used for type detection in certain scenarios.
*/
class base_t {
};

template<typename T>
class shared_raw_base_t {
class shared_raw_base_t : public base_t {
public:
using self = shared_raw_base_t;
using raw_t = T;
Expand All @@ -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(); }
Expand Down Expand Up @@ -51,7 +59,7 @@ namespace uvcxx {
: m_raw(std::move(raw)) {}

explicit shared_raw_base_t(borrow_t borrow)
: m_raw(std::shared_ptr<raw_t>(borrow.raw, [](T *) {})) {}
: m_raw(std::shared_ptr<raw_t>(borrow.raw, [](raw_t *) {})) {}

template<typename K>
static std::shared_ptr<raw_t> cast_raw(const std::shared_ptr<K> &p) {
Expand All @@ -75,7 +83,7 @@ namespace uvcxx {
};

template<typename T>
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;
Expand All @@ -84,6 +92,7 @@ namespace uvcxx {

operator raw_t *() const { return const_cast<self *>(this); }

protected:
raw_t *raw() { return (raw_t *) (*this); }

raw_t *raw() const { return (raw_t *) (*this); }
Expand All @@ -96,7 +105,7 @@ namespace uvcxx {
};

template<typename T>
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;
Expand All @@ -105,6 +114,7 @@ namespace uvcxx {

operator raw_t *() const { return const_cast<raw_t *>(&m_raw); }

protected:
raw_t *raw() { return (raw_t *) (*this); }

raw_t *raw() const { return (raw_t *) (*this); }
Expand All @@ -116,7 +126,7 @@ namespace uvcxx {
K *raw() const { return (K *) (raw()); }

private:
T m_raw{};
raw_t m_raw{};
};
}

Expand Down
9 changes: 2 additions & 7 deletions include/uvcxx/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#include <uv.h>

#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<uv_lib_t> {
public:
using self = lib_t;
using raw_t = uv_lib_t;

lib_t(const lib_t &) = delete;

Expand Down Expand Up @@ -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};
};
}
Expand Down

0 comments on commit 6088837

Please sign in to comment.