Skip to content

Commit

Permalink
chore: tidy up Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 18, 2023
1 parent 118c1b7 commit 1d4a429
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
50 changes: 23 additions & 27 deletions include/mrdox/Support/Dom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,46 @@ enum class Kind
class MRDOX_DECL
Any
{
void* ptr_;
std::atomic<std::size_t> refs_ = 1;

protected:
explicit Any(void* ptr) noexcept
: ptr_(ptr)
{
}
std::atomic<std::size_t> mutable refs_ = 1;

public:
virtual ~Any() = 0;

void* get() const noexcept
Any* addref() noexcept
{
return ptr_;
++refs_;
return this;
}

Any* addref() noexcept
Any const* addref() const noexcept
{
++refs_;
return this;
}

void release() noexcept
void release() const noexcept
{
if(--refs_ > 0)
return;
delete this;
}
};

template<class U, class... Args>
auto makePointer(Args&&... args);

/** A pointer container for object or array.
*/
template<class T>
requires std::derived_from<T, Any>
requires std::convertible_to<T*, Any*>
class Pointer
{
public: // VFALCO FIXME!
Any* any_;

public: // VFALCO FIXME!
template<class U>
requires std::convertible_to<U*, Any*>
friend class Pointer;

explicit
Pointer(Any* any) noexcept
: any_(any)
Expand All @@ -97,8 +96,7 @@ class Pointer

public:
Pointer() = delete;
Pointer& operator=(
Pointer const&) = delete;
Pointer& operator=(Pointer const&) = delete;

~Pointer()
{
Expand All @@ -111,15 +109,15 @@ class Pointer
}

template<class U>
requires std::derived_from<U, T>
requires std::convertible_to<U*, T*>
Pointer(Pointer<U> const& other) noexcept
: any_(other.any_->addref())
{
}

T* get() const noexcept
{
return reinterpret_cast<T*>(any_->get());
return static_cast<T*>(any_);
}

T* operator->() const noexcept
Expand All @@ -136,15 +134,15 @@ class Pointer
{
return any_;
}

template<class U, class... Args>
friend auto makePointer(Args&&... args);
};

template<
class T, class... Args>
requires std::derived_from<T, Any>
Pointer<T>
makePointer(Args&&... args)
template<class U, class... Args>
auto makePointer(Args&&... args)
{
return Pointer<T>(new T(
return Pointer<U>(new U(
std::forward<Args>(args)...));
}

Expand All @@ -154,7 +152,6 @@ class MRDOX_DECL
Array : public Any
{
public:
Array();
virtual std::size_t length() const noexcept = 0;
virtual Value get(std::size_t) const = 0;
};
Expand All @@ -167,7 +164,6 @@ class MRDOX_DECL
Object : public Any
{
public:
Object();
virtual bool empty() const noexcept;
virtual Value get(std::string_view) const = 0;
virtual std::vector<std::string_view> props() const = 0;
Expand Down
12 changes: 0 additions & 12 deletions source/Support/Dom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ namespace dom {

Any::~Any() = default;

Array::
Array()
: Any(this)
{
}

Object::
Object()
: Any(this)
{
}

bool Object::empty() const noexcept
{
return false;
Expand Down

0 comments on commit 1d4a429

Please sign in to comment.