Skip to content

Commit

Permalink
TryResult -> bool
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Jul 14, 2024
1 parent b2a8f25 commit 1c2bce2
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions include/momo/DataTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,23 @@ class DataTable
typedef typename Indexes::UniqueHashIndex UniqueHashIndex;
typedef typename Indexes::MultiHashIndex MultiHashIndex;

struct TryResult
class TryResult
{
public:
explicit operator bool() const noexcept
{
return uniqueHashIndex == UniqueHashIndex::empty;
}

public:
RowReference rowReference;
UniqueHashIndex uniqueHashIndex;
};

class UniqueIndexViolation : public std::runtime_error, public TryResult
{
public:
explicit UniqueIndexViolation(TryResult tryResult)
explicit UniqueIndexViolation(const TryResult& tryResult)
: std::runtime_error("Unique index violation"),
TryResult(tryResult)
{
Expand Down Expand Up @@ -518,7 +525,7 @@ class DataTable
RowReference AddRow(Row&& row)
{
TryResult res = TryAddRow(std::move(row));
if (res.uniqueHashIndex != UniqueHashIndex::empty)
if (!res)
throw UniqueIndexViolation(res);
return res.rowReference;
}
Expand Down Expand Up @@ -552,7 +559,7 @@ class DataTable
RowReference InsertRow(size_t rowNumber, Row&& row)
{
TryResult res = TryInsertRow(rowNumber, std::move(row));
if (res.uniqueHashIndex != UniqueHashIndex::empty)
if (!res)
throw UniqueIndexViolation(res);
return res.rowReference;
}
Expand All @@ -568,7 +575,7 @@ class DataTable
{
MOMO_CHECK(rowNumber <= GetCount());
TryResult res = TryAddRow(std::move(row));
if (res.uniqueHashIndex == UniqueHashIndex::empty)
if (res)
{
std::rotate(internal::UIntMath<>::Next(mRaws.GetBegin(), rowNumber),
std::prev(mRaws.GetEnd()), mRaws.GetEnd());
Expand Down Expand Up @@ -611,7 +618,7 @@ class DataTable
RowReference UpdateRow(size_t rowNumber, Row&& row)
{
TryResult res = TryUpdateRow(rowNumber, std::move(row));
if (res.uniqueHashIndex != UniqueHashIndex::empty)
if (!res)
throw UniqueIndexViolation(res);
return res.rowReference;
}
Expand All @@ -621,7 +628,7 @@ class DataTable
internal::Identity<Item>&& newItem)
{
TryResult res = pvTryUpdateRow(rowRef, column, std::move(newItem));
if (res.uniqueHashIndex != UniqueHashIndex::empty)
if (!res)
throw UniqueIndexViolation(res);
return res.rowReference;
}
Expand All @@ -631,7 +638,7 @@ class DataTable
const internal::Identity<Item>& newItem)
{
TryResult res = pvTryUpdateRow(rowRef, column, newItem);
if (res.uniqueHashIndex != UniqueHashIndex::empty)
if (!res)
throw UniqueIndexViolation(res);
return res.rowReference;
}
Expand Down

0 comments on commit 1c2bce2

Please sign in to comment.