diff --git a/either.h b/either.h index dfbd8086..2fc466f1 100644 --- a/either.h +++ b/either.h @@ -7,30 +7,30 @@ namespace cpputils { template - class Either final { + class either final { public: //TODO Try allowing construction with any type that std::is_convertible to Left or Right. - Either(const Left &left): _side(Side::left) { + either(const Left &left): _side(Side::left) { _construct_left(left); } - Either(Left &&left): _side(Side::left) { + either(Left &&left): _side(Side::left) { _construct_left(std::move(left)); } - Either(const Right &right): _side(Side::right) { + either(const Right &right): _side(Side::right) { _construct_right(right); } - Either(Right &&right): _side(Side::right) { + either(Right &&right): _side(Side::right) { _construct_right(std::move(right)); } //TODO Try allowing copy-construction when Left/Right types are std::is_convertible - Either(const Either &rhs): _side(rhs._side) { + either(const either &rhs): _side(rhs._side) { if(_side == Side::left) { _construct_left(rhs._left); } else { _construct_right(rhs._right); } } - Either(Either &&rhs): _side(rhs._side) { + either(either &&rhs): _side(rhs._side) { if(_side == Side::left) { _construct_left(std::move(rhs._left)); } else { @@ -38,12 +38,12 @@ namespace cpputils { } } - ~Either() { + ~either() { _destruct(); } //TODO Try allowing copy-assignment when Left/Right types are std::is_convertible - Either &operator=(const Either &rhs) { + either &operator=(const either &rhs) { _destruct(); _side = rhs._side; if (_side == Side::left) { @@ -54,7 +54,7 @@ namespace cpputils { return *this; } - Either &operator=(Either &&rhs) { + either &operator=(either &&rhs) { _destruct(); _side = rhs._side; if (_side == Side::left) { @@ -79,7 +79,7 @@ namespace cpputils { return _left; } Left &left() & { - return const_cast(const_cast*>(this)->left()); + return const_cast(const_cast*>(this)->left()); } Left &&left() && { return std::move(left()); @@ -89,7 +89,7 @@ namespace cpputils { return _right; } Right &right() & { - return const_cast(const_cast*>(this)->right()); + return const_cast(const_cast*>(this)->right()); } Right &&right() && { return std::move(right()); @@ -146,7 +146,7 @@ namespace cpputils { }; enum class Side : unsigned char {left, right} _side; - Either(Side side): _side(side) {} + either(Side side): _side(side) {} template void _construct_left(Args&&... args) { @@ -165,14 +165,14 @@ namespace cpputils { } template - friend Either make_left(Args&&... args); + friend either make_left(Args&&... args); template - friend Either make_right(Args&&... args); + friend either make_right(Args&&... args); }; template - bool operator==(const Either &lhs, const Either &rhs) { + bool operator==(const either &lhs, const either &rhs) { if (lhs.is_left() != rhs.is_left()) { return false; } @@ -184,12 +184,12 @@ namespace cpputils { } template - bool operator!=(const Either &lhs, const Either &rhs) { + bool operator!=(const either &lhs, const either &rhs) { return !operator==(lhs, rhs); } template - std::ostream &operator<<(std::ostream &stream, const Either &value) { + std::ostream &operator<<(std::ostream &stream, const either &value) { if (value.is_left()) { stream << "Left(" << value.left() << ")"; } else { @@ -199,15 +199,15 @@ namespace cpputils { } template - Either make_left(Args&&... args) { - Either result(Either::Side::left); + either make_left(Args&&... args) { + either result(either::Side::left); result._construct_left(std::forward(args)...); return result; } template - Either make_right(Args&&... args) { - Either result(Either::Side::right); + either make_right(Args&&... args) { + either result(either::Side::right); result._construct_right(std::forward(args)...); return result; } diff --git a/test/EitherTest.cpp b/test/EitherTest.cpp index f6427f43..eef851bf 100644 --- a/test/EitherTest.cpp +++ b/test/EitherTest.cpp @@ -36,33 +36,33 @@ struct StoreWith1ByteFlag { class EitherTest: public Test { public: template - void EXPECT_IS_LEFT(const Either &val) { + void EXPECT_IS_LEFT(const either &val) { EXPECT_TRUE(val.is_left()); EXPECT_FALSE(val.is_right()); } template - void EXPECT_IS_RIGHT(const Either &val) { + void EXPECT_IS_RIGHT(const either &val) { EXPECT_FALSE(val.is_left()); EXPECT_TRUE(val.is_right()); } template - void EXPECT_LEFT_IS(const Expected &expected, Either &value) { + void EXPECT_LEFT_IS(const Expected &expected, either &value) { EXPECT_IS_LEFT(value); EXPECT_EQ(expected, value.left()); EXPECT_EQ(expected, value.left_opt().get()); EXPECT_EQ(boost::none, value.right_opt()); - const Either &const_value = value; + const either &const_value = value; EXPECT_EQ(expected, const_value.left()); EXPECT_EQ(expected, const_value.left_opt().get()); EXPECT_EQ(boost::none, const_value.right_opt()); } template - void EXPECT_RIGHT_IS(const Expected &expected, Either &value) { + void EXPECT_RIGHT_IS(const Expected &expected, either &value) { EXPECT_IS_RIGHT(value); EXPECT_EQ(expected, value.right()); EXPECT_EQ(expected, value.right_opt().get()); EXPECT_EQ(boost::none, value.left_opt()); - const Either &const_value = value; + const either &const_value = value; EXPECT_EQ(expected, const_value.right()); EXPECT_EQ(expected, const_value.right_opt().get()); EXPECT_EQ(boost::none, const_value.left_opt()); @@ -71,7 +71,7 @@ public: template void TestSpaceUsage() { - EXPECT_EQ(std::max(sizeof(StoreWith1ByteFlag), sizeof(StoreWith1ByteFlag)), sizeof(Either)); + EXPECT_EQ(std::max(sizeof(StoreWith1ByteFlag), sizeof(StoreWith1ByteFlag)), sizeof(either)); } TEST_F(EitherTest, SpaceUsage) { @@ -83,180 +83,180 @@ TEST_F(EitherTest, SpaceUsage) { } TEST_F(EitherTest, LeftCanBeConstructed) { - Either val = 3; + either val = 3; UNUSED(val); } TEST_F(EitherTest, RightCanBeConstructed) { - Either val = string("string"); + either val = string("string"); UNUSED(val); } TEST_F(EitherTest, IsLeft) { - Either val = 3; + either val = 3; EXPECT_IS_LEFT(val); } TEST_F(EitherTest, IsRight) { - Either val = string("string"); + either val = string("string"); EXPECT_IS_RIGHT(val); } TEST_F(EitherTest, LeftIsStored) { - Either val = 3; + either val = 3; EXPECT_LEFT_IS(3, val); } TEST_F(EitherTest, RightIsStored) { - Either val = string("string"); + either val = string("string"); EXPECT_RIGHT_IS("string", val); } TEST_F(EitherTest, LeftCanBeMoveContructed) { - Either val = OnlyMoveable(1); + either val = OnlyMoveable(1); UNUSED(val); } TEST_F(EitherTest, RightCanBeMoveContructed) { - Either val = OnlyMoveable(1); + either val = OnlyMoveable(1); UNUSED(val); } TEST_F(EitherTest, IsLeftWhenMoveContructed) { - Either val = OnlyMoveable(1); + either val = OnlyMoveable(1); EXPECT_IS_LEFT(val); } TEST_F(EitherTest, IsRightWhenMoveContructed) { - Either val = OnlyMoveable(1); + either val = OnlyMoveable(1); EXPECT_IS_RIGHT(val); } TEST_F(EitherTest, LeftIsStoredWhenMoveContructed) { - Either val = OnlyMoveable(2); + either val = OnlyMoveable(2); EXPECT_LEFT_IS(OnlyMoveable(2), val); } TEST_F(EitherTest, RightIsStoredWhenMoveContructed) { - Either val = OnlyMoveable(3); + either val = OnlyMoveable(3); EXPECT_RIGHT_IS(OnlyMoveable(3), val); } TEST_F(EitherTest, LeftCanBeCopied) { - Either val = string("string"); - Either val2 = val; + either val = string("string"); + either val2 = val; EXPECT_LEFT_IS("string", val2); } TEST_F(EitherTest, CopyingLeftDoesntChangeSource) { - Either val = string("string"); - Either val2 = val; + either val = string("string"); + either val2 = val; EXPECT_LEFT_IS("string", val); } TEST_F(EitherTest, RightCanBeCopied) { - Either val = string("string"); - Either val2 = val; + either val = string("string"); + either val2 = val; EXPECT_RIGHT_IS("string", val2); } TEST_F(EitherTest, CopyingRightDoesntChangeSource) { - Either val = string("string"); - Either val2 = val; + either val = string("string"); + either val2 = val; EXPECT_RIGHT_IS("string", val); } TEST_F(EitherTest, LeftCanBeMoved) { - Either val = OnlyMoveable(5); - Either val2 = std::move(val); + either val = OnlyMoveable(5); + either val2 = std::move(val); EXPECT_LEFT_IS(OnlyMoveable(5), val2); } TEST_F(EitherTest, RightCanBeMoved) { - Either val = OnlyMoveable(5); - Either val2 = std::move(val); + either val = OnlyMoveable(5); + either val2 = std::move(val); EXPECT_RIGHT_IS(OnlyMoveable(5), val2); } TEST_F(EitherTest, LeftCanBeAssigned) { - Either val = string("string"); - Either val2 = string("otherstring"); + either val = string("string"); + either val2 = string("otherstring"); val2 = val; EXPECT_LEFT_IS("string", val2); } TEST_F(EitherTest, RightCanBeAssigned) { - Either val = string("string"); - Either val2 = string("otherstring"); + either val = string("string"); + either val2 = string("otherstring"); val2 = val; EXPECT_RIGHT_IS("string", val2); } TEST_F(EitherTest, LeftCanBeMoveAssigned) { - Either val = OnlyMoveable(3); - Either val2 = OnlyMoveable(4); + either val = OnlyMoveable(3); + either val2 = OnlyMoveable(4); val2 = std::move(val); EXPECT_LEFT_IS(OnlyMoveable(3), val2); } TEST_F(EitherTest, RightCanBeMoveAssigned) { - Either val = OnlyMoveable(3); - Either val2 = OnlyMoveable(4); + either val = OnlyMoveable(3); + either val2 = OnlyMoveable(4); val2 = std::move(val); EXPECT_RIGHT_IS(OnlyMoveable(3), val2); } TEST_F(EitherTest, LeftCanBeDirectlyAssigned) { - Either val = string("string"); + either val = string("string"); val = string("otherstring"); EXPECT_LEFT_IS("otherstring", val); } TEST_F(EitherTest, RightCanBeDirectlyAssigned) { - Either val = string("string"); + either val = string("string"); val = string("otherstring"); EXPECT_RIGHT_IS("otherstring", val); } TEST_F(EitherTest, LeftCanBeDirectlyMoveAssigned) { - Either val = OnlyMoveable(3); + either val = OnlyMoveable(3); val = OnlyMoveable(5); EXPECT_LEFT_IS(OnlyMoveable(5), val); } TEST_F(EitherTest, RightCanBeDirectlyMoveAssigned) { - Either val = OnlyMoveable(3); + either val = OnlyMoveable(3); val = OnlyMoveable(5); EXPECT_RIGHT_IS(OnlyMoveable(5), val); } TEST_F(EitherTest, ModifyLeft) { - Either val = string("mystring1"); + either val = string("mystring1"); val.left() = "mystring2"; EXPECT_LEFT_IS("mystring2", val); } TEST_F(EitherTest, ModifyRight) { - Either val = string("mystring1"); + either val = string("mystring1"); val.right() = "mystring2"; EXPECT_RIGHT_IS("mystring2", val); } TEST_F(EitherTest, ModifyLeftOpt) { - Either val = string("mystring1"); + either val = string("mystring1"); val.left_opt().get() = "mystring2"; EXPECT_LEFT_IS("mystring2", val); } TEST_F(EitherTest, ModifyRightOpt) { - Either val = string("mystring1"); + either val = string("mystring1"); val.right_opt().get() = "mystring2"; EXPECT_RIGHT_IS("mystring2", val); } TEST_F(EitherTest, LeftEquals) { - Either val1 = string("mystring"); - Either val2 = string("mystring"); + either val1 = string("mystring"); + either val2 = string("mystring"); EXPECT_TRUE(val1 == val2); EXPECT_TRUE(val2 == val1); EXPECT_FALSE(val1 != val2); @@ -264,8 +264,8 @@ TEST_F(EitherTest, LeftEquals) { } TEST_F(EitherTest, LeftNotEquals) { - Either val1 = string("mystring"); - Either val2 = string("mystring2"); + either val1 = string("mystring"); + either val2 = string("mystring2"); EXPECT_TRUE(val1 != val2); EXPECT_TRUE(val2 != val1); EXPECT_FALSE(val1 == val2); @@ -273,8 +273,8 @@ TEST_F(EitherTest, LeftNotEquals) { } TEST_F(EitherTest, RightEquals) { - Either val1 = string("mystring"); - Either val2 = string("mystring"); + either val1 = string("mystring"); + either val2 = string("mystring"); EXPECT_TRUE(val1 == val2); EXPECT_TRUE(val2 == val1); EXPECT_FALSE(val1 != val2); @@ -282,8 +282,8 @@ TEST_F(EitherTest, RightEquals) { } TEST_F(EitherTest, RightNotEquals) { - Either val1 = string("mystring"); - Either val2 = string("mystring2"); + either val1 = string("mystring"); + either val2 = string("mystring2"); EXPECT_TRUE(val1 != val2); EXPECT_TRUE(val2 != val1); EXPECT_FALSE(val1 == val2); @@ -291,8 +291,8 @@ TEST_F(EitherTest, RightNotEquals) { } TEST_F(EitherTest, LeftNotEqualsRight) { - Either val1 = string("mystring"); - Either val2 = 3; + either val1 = string("mystring"); + either val2 = 3; EXPECT_TRUE(val1 != val2); EXPECT_TRUE(val2 != val1); EXPECT_FALSE(val1 == val2); @@ -312,32 +312,32 @@ TEST_F(EitherTest, OutputRight) { } TEST_F(EitherTest, MakeLeft) { - Either var = make_left("mystring"); + either var = make_left("mystring"); EXPECT_LEFT_IS("mystring", var); } TEST_F(EitherTest, MakeLeft_OnlyMoveable) { - Either var = make_left(4); + either var = make_left(4); EXPECT_LEFT_IS(OnlyMoveable(4), var); } TEST_F(EitherTest, MakeLeft_MultiParam) { - Either, int> var = make_left, int>(4, 5); + either, int> var = make_left, int>(4, 5); EXPECT_LEFT_IS(make_pair(4,5), var); } TEST_F(EitherTest, MakeRight) { - Either var = make_right("mystring"); + either var = make_right("mystring"); EXPECT_RIGHT_IS("mystring", var); } TEST_F(EitherTest, MakeRight_OnlyMoveable) { - Either var = make_right(4); + either var = make_right(4); EXPECT_RIGHT_IS(OnlyMoveable(4), var); } TEST_F(EitherTest, MakeRight_MultiParam) { - Either> var = make_right>(4, 5); + either> var = make_right>(4, 5); EXPECT_RIGHT_IS(make_pair(4,5), var); } @@ -403,7 +403,7 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalled) { destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs ClassWithDestructorCallback temp(&destructorCallback); - Either var = temp; + either var = temp; } TEST_F(EitherTest_Destructor, RightDestructorIsCalled) { @@ -411,7 +411,7 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalled) { destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs ClassWithDestructorCallback temp(&destructorCallback); - Either var = temp; + either var = temp; } TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) { @@ -419,8 +419,8 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) { destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 ClassWithDestructorCallback temp(&destructorCallback); - Either var1 = temp; - Either var2 = var1; + either var1 = temp; + either var2 = var1; } TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) { @@ -428,8 +428,8 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) { destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 ClassWithDestructorCallback temp(&destructorCallback); - Either var1 = temp; - Either var2 = var1; + either var1 = temp; + either var2 = var1; } TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) { @@ -437,8 +437,8 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) { destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 OnlyMoveableClassWithDestructorCallback temp(&destructorCallback); - Either var1 = std::move(temp); - Either var2 = std::move(var1); + either var1 = std::move(temp); + either var2 = std::move(var1); } TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) { @@ -446,8 +446,8 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) { destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 OnlyMoveableClassWithDestructorCallback temp(&destructorCallback); - Either var1 = std::move(temp); - Either var2 = std::move(var1); + either var1 = std::move(temp); + either var2 = std::move(var1); } TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) { @@ -457,9 +457,9 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) { destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 ClassWithDestructorCallback temp1(&destructorCallback1); - Either var1 = temp1; + either var1 = temp1; ClassWithDestructorCallback temp2(&destructorCallback2); - Either var2 = temp2; + either var2 = temp2; var1 = var2; } @@ -470,9 +470,9 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterAssignment) { destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 ClassWithDestructorCallback temp1(&destructorCallback1); - Either var1 = temp1; + either var1 = temp1; ClassWithDestructorCallback temp2(&destructorCallback2); - Either var2 = temp2; + either var2 = temp2; var1 = var2; } @@ -483,9 +483,9 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoveAssignment) { destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1); - Either var1 = std::move(temp1); + either var1 = std::move(temp1); OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2); - Either var2 = std::move(temp2); + either var2 = std::move(temp2); var1 = std::move(var2); } @@ -496,8 +496,8 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoveAssignment) { destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1); - Either var1 = std::move(temp1); + either var1 = std::move(temp1); OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2); - Either var2 = std::move(temp2); + either var2 = std::move(temp2); var1 = std::move(var2); }