Skip to content

Commit

Permalink
feat: support dom::Function
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Sep 25, 2023
1 parent 3aecfc0 commit 93a1bf9
Show file tree
Hide file tree
Showing 22 changed files with 4,734 additions and 3,443 deletions.
95 changes: 76 additions & 19 deletions include/mrdox/Dom/Array.hpp
Expand Up @@ -108,25 +108,6 @@ class MRDOX_DECL
*/
Array(Array const& other);

/** Assignment.
Ownership of the array is transferred
to this, and ownership of the previous
contents is released. The moved-from
array behaves as if default constructed.
*/
Array& operator=(Array&&);

/** Assignment.
This acquires shared ownership of the copied
array, and ownership of the previous contents
is released.
*/
Array& operator=(Array const&) = default;

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

/** Constructor.
This constructs an array from an existing
Expand All @@ -150,6 +131,25 @@ class MRDOX_DECL
*/
Array(storage_type elements);

/** Assignment.
Ownership of the array is transferred
to this, and ownership of the previous
contents is released. The moved-from
array behaves as if default constructed.
*/
Array& operator=(Array&&);

/** Assignment.
This acquires shared ownership of the copied
array, and ownership of the previous contents
is released.
*/
Array& operator=(Array const&) = default;

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

/** Return the implementation used by this object.
*/
auto impl() const noexcept -> impl_type const&
Expand All @@ -175,6 +175,13 @@ class MRDOX_DECL
*/
value_type get(size_type i) const;

/** Set the i-th element, without bounds checking.
@param i The zero-based index of the element.
@param v The value to set.
*/
void set(size_type i, Value v);

/** Return the i-th element, without bounds checking.
*/
value_type operator[](size_type i) const;
Expand All @@ -185,6 +192,18 @@ class MRDOX_DECL
*/
value_type at(size_type i) const;

/** Return the first element.
@throw Exception `empty()`
*/
value_type front() const;

/** Return the last element.
@throw Exception `empty()`
*/
value_type back() const;

/** Return an iterator to the beginning of the range of elements.
*/
iterator begin() const;
Expand All @@ -200,6 +219,27 @@ class MRDOX_DECL
*/
void emplace_back(value_type value);

/** Concatenate two arrays.
*/
friend Array operator+(Array const& lhs, Array const& rhs);

/// @overload
template <std::convertible_to<Array> S>
friend auto operator+(
S const& lhs, Array const& rhs) noexcept
{
return Array(lhs) + rhs;
}

/// @overload
template <std::convertible_to<Array> S>
friend auto operator+(
Array const& lhs, S const& rhs) noexcept
{
return lhs + Array(rhs);
}


/** Swap two arrays.
*/
void swap(Array& other) noexcept
Expand All @@ -214,6 +254,18 @@ class MRDOX_DECL
lhs.swap(rhs);
}

/** Compare two arrays for equality.
*/
friend
bool
operator==(Array const&, Array const&) noexcept;

/** Compare two arrays for precedence.
*/
friend
std::strong_ordering
operator<=>(Array const&, Array const&) noexcept;

/** Return a diagnostic string.
*/
friend
Expand Down Expand Up @@ -261,6 +313,10 @@ class MRDOX_DECL
*/
virtual value_type get(size_type i) const = 0;

/** Set the i-th element, without bounds checking.
*/
virtual void set(size_type, Value);

/** Append an element to the end of the array.
The default implementation throws an exception,
Expand Down Expand Up @@ -298,6 +354,7 @@ class MRDOX_DECL
storage_type elements) noexcept;
size_type size() const override;
value_type get(size_type i) const override;
void set(size_type i, Value v) override;
void emplace_back(value_type value) override;

private:
Expand Down
28 changes: 27 additions & 1 deletion include/mrdox/Dom/Array.ipp
Expand Up @@ -170,6 +170,11 @@ inline auto Array::get(std::size_t i) const -> value_type
return impl_->get(i);
}

inline void Array::set(std::size_t i, Value v)
{
impl_->set(i, std::move(v));
}

inline auto Array::operator[](std::size_t i) const -> value_type
{
return get(i);
Expand All @@ -179,7 +184,17 @@ inline auto Array::at(std::size_t i) const -> value_type
{
if(i < size())
return get(i);
Error("out of range").Throw();
return {};
}

inline auto Array::front() const -> value_type
{
return at(0);
}

inline auto Array::back() const -> value_type
{
return at(size() - 1);
}

inline auto Array::begin() const -> iterator
Expand All @@ -197,6 +212,17 @@ inline void Array::emplace_back(value_type value)
impl_->emplace_back(std::move(value));
}

inline
Array operator+(Array const& lhs, Array const& rhs)
{
Array buf;
for (auto e: lhs)
buf.emplace_back(e);
for (auto e: rhs)
buf.emplace_back(e);
return buf;
}

} // dom
} // mrdox
} // clang
Expand Down

0 comments on commit 93a1bf9

Please sign in to comment.