Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## Next Release

### Types

- add `std::hash` function to strong type definition

<!-- insertion marker -->
## [0.2.2](https://github.com/repo/owner/releases/tag/0.2.2) - 2026-08-22

Expand Down
62 changes: 42 additions & 20 deletions include/mstd/types/strong_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@
namespace mstd
{

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)sss
#define STRONG_TYPE_TRAIT_LIST(X) \
X(NONE, 0) \
X(ORDERED, 1u << 0) \
X(ARITHMETIC, 1u << 1) \
X(INCREMENT, 1u << 2) \
X(HASHABLE, 1u << 3) \
X(STREAMABLE, 1u << 4) \
X(BOOLEAN, 1u << 5)

X(ORDERED, 1U << 0) \
X(ARITHMETIC, 1U << 1) \
X(INCREMENT, 1U << 2) \
X(HASHABLE, 1U << 3) \
X(STREAMABLE, 1U << 4) \
X(BOOLEAN, 1U << 5)

// NOLINTNEXTLINE(hicpp-signed-bitwise)
MSTD_ENUM_BITFLAG(StrongTypeTrait, unsigned, STRONG_TYPE_TRAIT_LIST);

/**
Expand Down Expand Up @@ -115,52 +117,52 @@ namespace mstd
//

constexpr StrongType &operator+=(const StrongType &other)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

constexpr StrongType &operator-=(const StrongType &other)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

constexpr StrongType &operator*=(const T &scalar)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

constexpr StrongType &operator/=(const T &scalar)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

[[nodiscard]]
constexpr StrongType operator+(const StrongType &other) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

[[nodiscard]]
constexpr StrongType operator-(const StrongType &other) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

[[nodiscard]]
constexpr StrongType operator*(const T &scalar) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

[[nodiscard]]
constexpr StrongType operator/(const T &scalar) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

[[nodiscard]]
constexpr StrongType operator-() const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC));
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC));

//
// Increment / decrement
//

constexpr StrongType &operator++()
requires(bool(Traits &StrongTypeTrait::INCREMENT));
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT));

constexpr StrongType operator++(int)
requires(bool(Traits &StrongTypeTrait::INCREMENT));
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT));

constexpr StrongType &operator--()
requires(bool(Traits &StrongTypeTrait::INCREMENT));
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT));

constexpr StrongType operator--(int)
requires(bool(Traits &StrongTypeTrait::INCREMENT));
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT));

std::string toString() const
requires HasToString<Tag, T>;
Expand All @@ -183,4 +185,24 @@ requires(static_cast<bool>(Traits &mstd::StrongTypeTrait::STREAMABLE) && true)
return os << strongType.get();
}

namespace std
{
// NOLINTBEGIN(cert-dcl58-cpp)
template <typename T, typename Tag, mstd::StrongTypeTrait Traits>
struct hash<mstd::StrongType<T, Tag, Traits>>
{
[[nodiscard]] size_t operator()(
const mstd::StrongType<T, Tag, Traits> &value
) const noexcept(noexcept(std::hash<T>{}(value.get())))
requires(
static_cast<bool>(Traits &mstd::StrongTypeTrait::HASHABLE) &&
requires(const T &t) { std::hash<T>{}(t); }
)
{
return std::hash<T>{}(value.get());
}
};
// NOLINTEND(cert-dcl58-cpp)
} // namespace std

#endif // __MSTD__TYPES__STRONG_TYPE_HPP__
26 changes: 13 additions & 13 deletions include/mstd/types/strong_type.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> &StrongType<T, Tag, Traits>::
operator+=(const StrongType &other)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
_value += other._value;
return *this;
Expand All @@ -166,7 +166,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> &StrongType<T, Tag, Traits>::
operator-=(const StrongType &other)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
_value -= other._value;
return *this;
Expand All @@ -190,7 +190,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> &StrongType<T, Tag, Traits>::
operator*=(const T &scalar)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
_value *= scalar;
return *this;
Expand All @@ -214,7 +214,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> &StrongType<T, Tag, Traits>::
operator/=(const T &scalar)
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
_value /= scalar;
return *this;
Expand All @@ -239,7 +239,7 @@ namespace mstd
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator+(
const StrongType &other
) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
return StrongType{_value + other._value};
}
Expand All @@ -264,7 +264,7 @@ namespace mstd
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator-(
const StrongType &other
) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
return StrongType{_value - other._value};
}
Expand All @@ -288,7 +288,7 @@ namespace mstd
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator*(
const T &scalar
) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
return StrongType{_value * scalar};
}
Expand All @@ -312,7 +312,7 @@ namespace mstd
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator/(
const T &scalar
) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
return StrongType{_value / scalar};
}
Expand All @@ -334,7 +334,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator-(
) const
requires(bool(Traits &StrongTypeTrait::ARITHMETIC))
requires(static_cast<bool>(Traits &StrongTypeTrait::ARITHMETIC))
{
return StrongType{-_value};
}
Expand All @@ -356,7 +356,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> &StrongType<T, Tag, Traits>::
operator++()
requires(bool(Traits &StrongTypeTrait::INCREMENT))
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT))
{
++_value;
return *this;
Expand All @@ -380,7 +380,7 @@ namespace mstd
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator++(
int
)
requires(bool(Traits &StrongTypeTrait::INCREMENT))
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT))
{
auto tmp = *this;
++_value;
Expand All @@ -404,7 +404,7 @@ namespace mstd
template <typename T, typename Tag, StrongTypeTrait Traits>
constexpr StrongType<T, Tag, Traits> &StrongType<T, Tag, Traits>::
operator--()
requires(bool(Traits &StrongTypeTrait::INCREMENT))
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT))
{
--_value;
return *this;
Expand All @@ -428,7 +428,7 @@ namespace mstd
constexpr StrongType<T, Tag, Traits> StrongType<T, Tag, Traits>::operator--(
int
)
requires(bool(Traits &StrongTypeTrait::INCREMENT))
requires(static_cast<bool>(Traits &StrongTypeTrait::INCREMENT))
{
auto tmp = *this;
--_value;
Expand Down
Loading