Skip to content

Commit

Permalink
feat: Array::emplace_back
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jul 3, 2023
1 parent 1eda403 commit e92e988
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 31 deletions.
52 changes: 52 additions & 0 deletions include/mrdox/Support/Dom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ class MRDOX_DECL
*/
value_type at(size_type i) const;

/** Append an element to the end of the array.
If the array is read-only, an exception
is thrown.
*/
void emplace_back(value_type value);

/** Return a diagnostic string.
*/
friend
Expand Down Expand Up @@ -162,6 +169,46 @@ class MRDOX_DECL
/** Return the i-th element, without bounds checking.
*/
virtual value_type get(size_type i) const = 0;

/** Append an element to the end of the array.
The default implementation throws an exception,
making the array effectively read-only.
*/
virtual void emplace_back(value_type value);
};

//------------------------------------------------
//
// DefaultArrayImpl
//
//------------------------------------------------

/** The default array implementation.
This implementation is backed by a simple
vector and allows appending.
*/
class MRDOX_DECL
DefaultArrayImpl : public ArrayImpl
{
public:
/// @copydoc Array::value_type
using value_type = Array::value_type;

/// @copydoc Array::size_type
using size_type = Array::size_type;

/** The type of storage used by the default implementation.
*/
using storage_type = std::vector<value_type>;

size_type size() const override;
value_type get(size_type i) const override;
void emplace_back(value_type value) override;

private:
std::vector<value_type> elements_;
};

/** Return a new array using a custom implementation.
Expand Down Expand Up @@ -923,6 +970,11 @@ inline auto Array::at(std::size_t i) const -> value_type
Error("out of range").Throw();
}

inline void Array::emplace_back(value_type value)
{
impl_->emplace_back(std::move(value));
}

//------------------------------------------------

inline bool Object::empty() const
Expand Down
84 changes: 53 additions & 31 deletions source/Support/Dom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ static_assert(std::ranges::random_access_range<Object>);
//
//------------------------------------------------

namespace {

class DefaultArrayImpl : public ArrayImpl
{
std::vector<Value> v_;

public:
std::size_t
size() const noexcept override
{
return v_.size();
}

Value get(std::size_t i) const override
{
MRDOX_ASSERT(i < v_.size());
return v_[i];
}
};

} // (anon)

//------------------------------------------------
//
// Array
//
//------------------------------------------------

Array::
~Array() = default;

Expand Down Expand Up @@ -97,19 +69,60 @@ toString(
return s;
}

//------------------------------------------------
//
// ArrayImpl
//
//------------------------------------------------

ArrayImpl::
~ArrayImpl() = default;

void
ArrayImpl::
emplace_back(
value_type value)
{
Error("Array is const").Throw();
}

//------------------------------------------------
//
// Object
// DefaultArrayImpl
//
//------------------------------------------------

ObjectImpl::
~ObjectImpl() = default;
auto
DefaultArrayImpl::
size() const ->
size_type
{
return elements_.size();
}

auto
DefaultArrayImpl::
get(
size_type i) const ->
value_type
{
MRDOX_ASSERT(i < elements_.size());
return elements_[i];
}

void
DefaultArrayImpl::
emplace_back(
value_type value)
{
elements_.emplace_back(std::move(value));
}

//------------------------------------------------
//
// Object
//
//------------------------------------------------

Object::
~Object() = default;
Expand Down Expand Up @@ -169,6 +182,15 @@ toString(
return s;
}

//------------------------------------------------
//
// ObjectImpl
//
//------------------------------------------------

ObjectImpl::
~ObjectImpl() = default;

//------------------------------------------------
//
// DefaultObjectImpl
Expand Down

0 comments on commit e92e988

Please sign in to comment.