Finish IdValueType

This commit is contained in:
Sebastian Messmer 2018-05-27 21:03:44 -07:00
parent e266fa5e98
commit 07ba168107
2 changed files with 192 additions and 53 deletions

View File

@ -54,41 +54,48 @@ public:
using underlying_type = UnderlyingType;
using concrete_type = ConcreteType;
constexpr IdValueType(IdValueType&&) = default;
constexpr IdValueType(const IdValueType&) = default;
constexpr IdValueType& operator=(IdValueType&&) = default;
constexpr IdValueType& operator=(const IdValueType&) = default;
constexpr IdValueType(IdValueType&& rhs) noexcept(noexcept(UnderlyingType(std::move(rhs.value_)))) = default;
constexpr IdValueType(const IdValueType& rhs) noexcept(noexcept(UnderlyingType(rhs.value_))) = default;
constexpr IdValueType& operator=(IdValueType&& rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() = std::move(rhs.value_))) {
value_ = std::move(rhs.value_);
return *this;
}
constexpr IdValueType& operator=(const IdValueType& rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() = rhs.value_)) {
value_ = rhs.value_;
return *this;
}
protected:
constexpr explicit IdValueType(underlying_type value) : value_(value) {
constexpr explicit IdValueType(underlying_type value) noexcept(noexcept(UnderlyingType(value)))
: value_(value) {
static_assert(std::is_base_of<IdValueType<ConcreteType, UnderlyingType>, ConcreteType>::value,
"CRTP violated. First template parameter of this class must be the concrete class.");
}
constexpr underlying_type& underlying_value() const {
constexpr underlying_type& underlying_value() const noexcept {
return value_;
}
friend struct std::hash<ConcreteType>;
friend constexpr bool operator==(ConcreteType lhs, ConcreteType rhs) {
friend constexpr bool operator==(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(std::declval<UnderlyingType>() == std::declval<UnderlyingType>())) {
return lhs.value_ == rhs.value_;
}
friend constexpr bool operator!=(ConcreteType lhs, ConcreteType rhs) {
friend constexpr bool operator!=(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(lhs == rhs)) {
return !operator==(lhs, rhs);
}
underlying_type value_;
};
#define DEFINE_HASH_FOR_VALUE_TYPE(ClassName) \
namespace std { \
template <> \
struct hash<ClassName> { \
size_t operator()(ClassName x) const { \
return std::hash<ClassName::underlying_type>()(x.value_); \
} \
}; \
#define DEFINE_HASH_FOR_VALUE_TYPE(ClassName) \
namespace std { \
template <> \
struct hash<ClassName> { \
size_t operator()(ClassName x) const noexcept(noexcept(std::hash<ClassName::underlying_type>()(x.value_))) { \
return std::hash<ClassName::underlying_type>()(x.value_); \
} \
}; \
}
@ -97,19 +104,19 @@ class OrderedIdValueType : public IdValueType<ConcreteType, UnderlyingType> {
protected:
using IdValueType<ConcreteType, UnderlyingType>::IdValueType;
friend constexpr bool operator<(ConcreteType lhs, ConcreteType rhs) {
friend constexpr bool operator<(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(lhs.value_ < rhs.value_)) {
return lhs.value_ < rhs.value_;
}
friend constexpr bool operator>(ConcreteType lhs, ConcreteType rhs) {
friend constexpr bool operator>(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(lhs.value_ > rhs.value_)) {
return lhs.value_ > rhs.value_;
}
friend constexpr bool operator>=(ConcreteType lhs, ConcreteType rhs) {
friend constexpr bool operator>=(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(lhs < rhs)) {
return !operator<(lhs, rhs);
}
friend constexpr bool operator<=(ConcreteType lhs, ConcreteType rhs) {
friend constexpr bool operator<=(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(lhs > rhs)) {
return !operator>(lhs, rhs);
}
};
@ -121,83 +128,83 @@ protected:
using OrderedIdValueType<ConcreteType, UnderlyingType>::OrderedIdValueType;
public:
constexpr ConcreteType& operator++() {
constexpr ConcreteType& operator++() noexcept(noexcept(++*std::declval<UnderlyingType*>())) {
++this->value_;
return *this;
return *static_cast<ConcreteType*>(this);
}
constexpr ConcreteType operator++(int) {
ConcreteType tmp = *this;
constexpr ConcreteType operator++(int) noexcept(noexcept(++std::declval<ConcreteType>())) {
ConcreteType tmp = *static_cast<ConcreteType*>(this);
++(*this);
return tmp;
}
constexpr ConcreteType& operator--() {
constexpr ConcreteType& operator--() noexcept(noexcept(--*std::declval<UnderlyingType*>())) {
--this->value_;
return *this;
return *static_cast<ConcreteType*>(this);
}
constexpr ConcreteType operator--(int) {
ConcreteType tmp = *this;
constexpr ConcreteType operator--(int) noexcept(noexcept(--std::declval<ConcreteType>())) {
ConcreteType tmp = *static_cast<ConcreteType*>(this);
--(*this);
return tmp;
}
constexpr ConcreteType& operator+=(ConcreteType rhs) {
constexpr ConcreteType& operator+=(ConcreteType rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() += std::declval<UnderlyingType>())) {
this->value_ += rhs.value_;
return *this;
return *static_cast<ConcreteType*>(this);
}
constexpr ConcreteType& operator-=(ConcreteType rhs) {
constexpr ConcreteType& operator-=(ConcreteType rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() -= std::declval<UnderlyingType>())) {
this->value_ -= rhs.value_;
return *this;
return *static_cast<ConcreteType*>(this);
}
constexpr ConcreteType& operator*=(UnderlyingType rhs) {
constexpr ConcreteType& operator*=(UnderlyingType rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() *= std::declval<UnderlyingType>())) {
this->value_ *= rhs;
return *this;
return *static_cast<ConcreteType*>(this);
}
constexpr ConcreteType& operator/=(UnderlyingType rhs) {
constexpr ConcreteType& operator/=(UnderlyingType rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() /= std::declval<UnderlyingType>())) {
this->value_ /= rhs;
return *this;
return *static_cast<ConcreteType*>(this);
}
constexpr ConcreteType& operator%=(UnderlyingType rhs) {
constexpr ConcreteType& operator%=(UnderlyingType rhs) noexcept(noexcept(*std::declval<UnderlyingType*>() %= std::declval<UnderlyingType>())) {
this->value_ %= rhs;
return *this;
return *static_cast<ConcreteType*>(this);
}
private:
friend constexpr ConcreteType operator+(ConcreteType lhs, ConcreteType rhs) {
friend constexpr ConcreteType operator+(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(std::declval<ConcreteType>() += std::declval<ConcreteType>())) {
return lhs += rhs;
}
friend constexpr ConcreteType operator-(ConcreteType lhs, ConcreteType rhs) {
friend constexpr ConcreteType operator-(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(std::declval<ConcreteType>() -= std::declval<ConcreteType>())) {
return lhs -= rhs;
}
friend constexpr ConcreteType operator*(ConcreteType lhs, UnderlyingType rhs) {
friend constexpr ConcreteType operator*(ConcreteType lhs, UnderlyingType rhs) noexcept(noexcept(std::declval<ConcreteType>() *= std::declval<UnderlyingType>())) {
return lhs *= rhs;
}
friend constexpr ConcreteType operator*(UnderlyingType lhs, ConcreteType rhs) {
friend constexpr ConcreteType operator*(UnderlyingType lhs, ConcreteType rhs) noexcept(noexcept(std::declval<ConcreteType>() * std::declval<UnderlyingType>())) {
return rhs * lhs;
}
friend constexpr ConcreteType operator/(ConcreteType lhs, UnderlyingType rhs) {
friend constexpr ConcreteType operator/(ConcreteType lhs, UnderlyingType rhs) noexcept(noexcept(std::declval<ConcreteType>() /= std::declval<UnderlyingType>())) {
return lhs /= rhs;
}
friend constexpr UnderlyingType operator/(ConcreteType lhs, ConcreteType rhs) {
friend constexpr UnderlyingType operator/(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(std::declval<UnderlyingType>() / std::declval<UnderlyingType>())) {
return lhs.value_ / rhs.value_;
}
friend constexpr ConcreteType operator%(ConcreteType lhs, UnderlyingType rhs) {
friend constexpr ConcreteType operator%(ConcreteType lhs, UnderlyingType rhs) noexcept(noexcept(std::declval<ConcreteType>() %= std::declval<UnderlyingType>())) {
return lhs %= rhs;
}
friend constexpr UnderlyingType operator%(ConcreteType lhs, ConcreteType rhs) {
friend constexpr UnderlyingType operator%(ConcreteType lhs, ConcreteType rhs) noexcept(noexcept(std::declval<UnderlyingType>() % std::declval<UnderlyingType>())) {
return lhs.value_ % rhs.value_;
}
};

View File

@ -11,15 +11,15 @@ using cpputils::value_type::QuantityValueType;
namespace {
struct MyIdValueType : IdValueType<MyIdValueType, int64_t> {
constexpr MyIdValueType(int64_t val): IdValueType(val) {}
constexpr explicit MyIdValueType(int64_t val): IdValueType(val) {}
};
struct MyOrderedIdValueType : OrderedIdValueType<MyOrderedIdValueType, int64_t> {
constexpr MyOrderedIdValueType(int64_t val): OrderedIdValueType(val) {}
constexpr explicit MyOrderedIdValueType(int64_t val): OrderedIdValueType(val) {}
};
struct MyQuantityValueType : QuantityValueType<MyQuantityValueType, int64_t> {
constexpr MyQuantityValueType(int64_t val): QuantityValueType(val) {}
constexpr explicit MyQuantityValueType(int64_t val): QuantityValueType(val) {}
};
}
@ -40,8 +40,11 @@ struct IdValueTypeTest_constexpr_test {
static constexpr Type test_copy_constructor = test_constructor;
static_assert(Type(5) == test_copy_constructor, "");
static constexpr Type test_copy_assignment = (Type(4) = 3);
static_assert(test_copy_assignment == Type(3), "");
static constexpr Type test_copy_assignment = (Type(4) = test_copy_constructor);
static_assert(test_copy_assignment == Type(5), "");
static constexpr Type test_move_assignment = (Type(4) = Type(3));
static_assert(test_move_assignment == Type(3), "");
static_assert(Type(5) == Type(5), "");
static_assert(Type(5) != Type(6), "");
@ -101,6 +104,12 @@ TYPED_TEST(IdValueTypeTest, CopyAssignment) {
EXPECT_TRUE(obj == obj2);
}
TYPED_TEST(IdValueTypeTest, CopyAssignment_Return) {
TypeParam obj(3);
TypeParam obj2(2);
EXPECT_TRUE((obj2 = obj) == TypeParam(3));
}
TYPED_TEST(IdValueTypeTest, MoveAssignment) {
TypeParam obj(3);
TypeParam obj2(2);
@ -108,6 +117,12 @@ TYPED_TEST(IdValueTypeTest, MoveAssignment) {
EXPECT_TRUE(obj2 == TypeParam(3));
}
TYPED_TEST(IdValueTypeTest, MoveAssignment_Return) {
TypeParam obj(3);
TypeParam obj2(2);
EXPECT_TRUE((obj2 = std::move(obj)) == TypeParam(3));
}
TYPED_TEST(IdValueTypeTest, Hash) {
TypeParam obj(3);
TypeParam obj2(3);
@ -234,7 +249,26 @@ TYPED_TEST(OrderedIdValueTypeTest, Set) {
template<class Type>
struct QuantityValueTypeTest_constexpr_test {
// TODO
static_assert(++Type(3) == Type(4), "");
static_assert(Type(3)++ == Type(3), "");
static_assert(--Type(3) == Type(2), "");
static_assert(Type(3)-- == Type(3), "");
static_assert((Type(3) += Type(2)) == Type(5), "");
static_assert((Type(3) -= Type(2)) == Type(1), "");
static_assert((Type(3) *= 2) == Type(6), "");
static_assert((Type(6) /= 2) == Type(3), "");
static_assert((Type(7) /= 3) == Type(2), "");
static_assert((Type(7) %= 3) == Type(1), "");
static_assert(Type(3) + Type(2) == Type(5), "");
static_assert(Type(3) - Type(2) == Type(1), "");
static_assert(Type(3) * 2 == Type(6), "");
static_assert(2 * Type(3) == Type(6), "");
static_assert(Type(6) / 2 == Type(3), "");
static_assert(Type(6) / Type(2) == 3, "");
static_assert(Type(7) / 3 == Type(2), "");
static_assert(Type(7) / Type(3) == 2, "");
static_assert(Type(7) % 3 == Type(1), "");
static_assert(Type(7) % Type(3) == 1, "");
static constexpr bool success = true;
};
@ -244,6 +278,104 @@ template<class Type> class QuantityValueTypeTest : public testing::Test {};
using QuantityValueTypeTest_types = testing::Types<MyQuantityValueType>;
TYPED_TEST_CASE(QuantityValueTypeTest, QuantityValueTypeTest_types);
// TODO Test cases for QuantityValueTypeTest
TYPED_TEST(QuantityValueTypeTest, PreIncrement) {
TypeParam a(3);
EXPECT_EQ(TypeParam(4), ++a);
EXPECT_EQ(TypeParam(4), a);
}
TYPED_TEST(QuantityValueTypeTest, PostIncrement) {
TypeParam a(3);
EXPECT_EQ(TypeParam(3), a++);
EXPECT_EQ(TypeParam(4), a);
}
TYPED_TEST(QuantityValueTypeTest, PreDecrement) {
TypeParam a(3);
EXPECT_EQ(TypeParam(2), --a);
EXPECT_EQ(TypeParam(2), a);
}
TYPED_TEST(QuantityValueTypeTest, PostDecrement) {
TypeParam a(3);
EXPECT_EQ(TypeParam(3), a--);
EXPECT_EQ(TypeParam(2), a);
}
TYPED_TEST(QuantityValueTypeTest, AddAssignment) {
TypeParam a(3);
EXPECT_EQ(TypeParam(5), a += TypeParam(2));
EXPECT_EQ(TypeParam(5), a);
}
TYPED_TEST(QuantityValueTypeTest, SubAssignment) {
TypeParam a(3);
EXPECT_EQ(TypeParam(1), a -= TypeParam(2));
EXPECT_EQ(TypeParam(1), a);
}
TYPED_TEST(QuantityValueTypeTest, MulAssignment) {
TypeParam a(3);
EXPECT_EQ(TypeParam(6), a *= 2);
EXPECT_EQ(TypeParam(6), a);
}
TYPED_TEST(QuantityValueTypeTest, DivScalarAssignment) {
TypeParam a(6);
EXPECT_EQ(TypeParam(3), a /= 2);
EXPECT_EQ(TypeParam(3), a);
}
TYPED_TEST(QuantityValueTypeTest, DivScalarWithRemainderAssignment) {
TypeParam a(7);
EXPECT_EQ(TypeParam(2), a /= 3);
EXPECT_EQ(TypeParam(2), a);
}
TYPED_TEST(QuantityValueTypeTest, ModScalarAssignment) {
TypeParam a(7);
EXPECT_EQ(TypeParam(1), a %= 3);
EXPECT_EQ(TypeParam(1), a);
}
TYPED_TEST(QuantityValueTypeTest, Add) {
EXPECT_EQ(TypeParam(5), TypeParam(3) + TypeParam(2));
}
TYPED_TEST(QuantityValueTypeTest, Sub) {
EXPECT_EQ(TypeParam(1), TypeParam(3) - TypeParam(2));
}
TYPED_TEST(QuantityValueTypeTest, Mul1) {
EXPECT_EQ(TypeParam(6), TypeParam(3) * 2);
}
TYPED_TEST(QuantityValueTypeTest, Mul2) {
EXPECT_EQ(TypeParam(6), 2 * TypeParam(3));
}
TYPED_TEST(QuantityValueTypeTest, DivScalar) {
EXPECT_EQ(TypeParam(3), TypeParam(6) / 2);
}
TYPED_TEST(QuantityValueTypeTest, DivValue) {
EXPECT_EQ(3, TypeParam(6) / TypeParam(2));
}
TYPED_TEST(QuantityValueTypeTest, DivScalarWithRemainder) {
EXPECT_EQ(TypeParam(2), TypeParam(7) / 3);
}
TYPED_TEST(QuantityValueTypeTest, DivValueWithRemainder) {
EXPECT_EQ(2, TypeParam(7) / TypeParam(3));
}
TYPED_TEST(QuantityValueTypeTest, ModScalar) {
EXPECT_EQ(TypeParam(1), TypeParam(7) % 3);
}
TYPED_TEST(QuantityValueTypeTest, ModValue) {
EXPECT_EQ(1, TypeParam(7) % TypeParam(3));
}
}