Skip to content

Commit

Permalink
try to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alifahrri committed May 11, 2024
1 parent 9239805 commit 0fd5627
Show file tree
Hide file tree
Showing 7 changed files with 625 additions and 18 deletions.
12 changes: 12 additions & 0 deletions include/nmtools/utl/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ namespace nmtools::utl
return (size_type)N;
}

// The following is intended to enable copy assignment of maybe<array> type in constexpr function/lambda
// but then it deprecates the definition of implicit copy constructor
#if 0
constexpr array& operator=(const array& other) noexcept
{
for (size_t i=0; i<N; i++) {
buffer[i] = other.buffer[i];
}
return *this;
}
#endif

constexpr reference operator[](index_type i) noexcept
{
return buffer[i];
Expand Down
46 changes: 39 additions & 7 deletions include/nmtools/utl/either.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,37 @@ namespace nmtools::utl
return static_cast<const Derived&>(*this);
}

constexpr Derived& operator=(const Derived& other) noexcept
{
if (other.self().tag == Derived::LEFT) {
self().left = other.self().left;
self().tag = Derived::LEFT;
return self();
} else {
self().right = other.self().right;
self().tag = Derived::RIGHT;
return self();
}
}

template <typename T>
constexpr Derived& operator=(const T& val) noexcept
{
using left_type = typename Derived::left_type;
using right_type = typename Derived::right_type;
static_assert( meta::is_same_v<T,left_type> || meta::is_same_v<T,right_type>
static_assert( meta::is_same_v<T,left_type> || meta::is_same_v<T,right_type> || meta::is_same_v<T,Derived>
, "unsupported type for either assignment"
);
if constexpr (meta::is_same_v<T,left_type>) {
self().left = val;
self().tag = Derived::LEFT;
return self();
} else {
} else if constexpr (meta::is_same_v<T,right_type>) {
self().right = val;
self().tag = Derived::RIGHT;
self().tag = Derived::RIGHT;
return self();
} else {
return operator=(static_cast<Derived>(val));
}
}

Expand Down Expand Up @@ -156,6 +171,11 @@ namespace nmtools::utl
base::operator=(val);
return *this;
}

constexpr either& operator=(const either& other) noexcept
{
return base::operator=(other);
}
};

// TODO: find out if we can move the constructor to base for better composition & brevity
Expand All @@ -182,6 +202,8 @@ namespace nmtools::utl
friend struct impl::get_if_t;
friend base;
public:
using left_type = left_t;
using right_type = right_t;

constexpr either() noexcept
: left{}, tag{LEFT} {}
Expand Down Expand Up @@ -221,8 +243,12 @@ namespace nmtools::utl
template <typename U>
constexpr either& operator=(const U& val) noexcept
{
base::operator=(val);
return *this;
return base::operator=(val);
}

constexpr either& operator=(const either& other) noexcept
{
return base::operator=(other);
}
}; // either

Expand All @@ -249,6 +275,8 @@ namespace nmtools::utl
friend struct impl::get_if_t;
friend base;
public:
using left_type = left_t;
using right_type = right_t;

constexpr either() noexcept
: left{}, tag{LEFT} {}
Expand Down Expand Up @@ -279,8 +307,12 @@ namespace nmtools::utl
template <typename U>
constexpr either& operator=(const U& val) noexcept
{
base::operator=(val);
return *this;
return base::operator=(val);
}

constexpr either& operator=(const either& other) noexcept
{
return base::operator=(other);
}
}; // either
#endif
Expand Down
26 changes: 26 additions & 0 deletions include/nmtools/utl/maybe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace nmtools::utl
// assume copy-constructible
constexpr maybe(const T& t) : base(t) {}

constexpr maybe(const maybe& other) : base(other) {}

constexpr bool has_value() const noexcept
{
return this->tag == base::LEFT;
Expand All @@ -56,6 +58,18 @@ namespace nmtools::utl
return *this;
}

/*constexpr*/ maybe& operator=(const maybe& other)
{
if (other.has_value()) {
this->left = other.left;
this->tag = base::LEFT;
} else {
this->right = other.right;
this->tag = base::RIGHT;
}
return *this;
}

constexpr explicit operator bool() const noexcept
{
return has_value();
Expand Down Expand Up @@ -135,6 +149,18 @@ namespace nmtools::utl
return *this;
}

/*constexpr*/ maybe& operator=(const maybe& other)
{
if (other.has_value()) {
this->left = other.left;
this->tag = base::LEFT;
} else {
this->right = other.right;
this->tag = base::RIGHT;
}
return *this;
}

constexpr explicit operator bool() const noexcept
{
return has_value();
Expand Down
19 changes: 18 additions & 1 deletion include/nmtools/utl/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,25 @@ namespace nmtools::utl

public:

vector() {}
vector()
: buffer_(NULL)
, size_(0)
, buffer_size_(0)
, allocator{}
{}
vector(size_type N)
: buffer_(NULL)
, size_(0)
, buffer_size_(0)
, allocator{}
{
resize(N);
}
vector(const vector& other)
: buffer_(NULL)
, size_(0)
, buffer_size_(0)
, allocator{}
{
resize(other.size_);
// dumb copy
Expand All @@ -154,6 +167,10 @@ namespace nmtools::utl
// TODO: fix initialization
template <typename A, typename B, typename...Ts>
vector(const A& t, const B& u, const Ts&...ts)
: buffer_(NULL)
, size_(0)
, buffer_size_(0)
, allocator{}
{
constexpr auto n = sizeof...(Ts) + 2;
resize(n);
Expand Down
Loading

0 comments on commit 0fd5627

Please sign in to comment.