-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add can_increment and can_decrement (#43)
### Why It can simplify the use of stronk types with custom methods. ### What was changed - **Add can_increment/decrement** - **Version bump**
- Loading branch information
1 parent
6369371
commit ffeff8b
Showing
6 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
namespace twig | ||
{ | ||
|
||
template<typename StronkT> | ||
struct can_decrement | ||
{ | ||
constexpr auto operator--() noexcept -> StronkT& | ||
{ | ||
--static_cast<StronkT&>(*this).template unwrap<StronkT>(); | ||
return static_cast<StronkT&>(*this); | ||
} | ||
|
||
constexpr auto operator--(int) noexcept -> StronkT | ||
{ | ||
auto copy = static_cast<StronkT&>(*this); | ||
--static_cast<StronkT&>(*this).template unwrap<StronkT>(); | ||
return copy; | ||
} | ||
}; | ||
|
||
} // namespace twig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
namespace twig | ||
{ | ||
|
||
template<typename StronkT> | ||
struct can_increment | ||
{ | ||
constexpr auto operator++() noexcept -> StronkT& | ||
{ | ||
++static_cast<StronkT&>(*this).template unwrap<StronkT>(); | ||
return static_cast<StronkT&>(*this); | ||
} | ||
|
||
constexpr auto operator++(int) noexcept -> StronkT | ||
{ | ||
auto copy = static_cast<StronkT&>(*this); | ||
++static_cast<StronkT&>(*this).template unwrap<StronkT>(); | ||
return copy; | ||
} | ||
}; | ||
|
||
} // namespace twig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <gtest/gtest.h> | ||
#include <stronk/can_decrement.h> | ||
#include <stronk/stronk.h> | ||
|
||
namespace twig | ||
{ | ||
|
||
struct an_int_decrement_test_type : stronk<an_int_decrement_test_type, int, can_decrement, can_equate> | ||
{ | ||
using stronk::stronk; | ||
}; | ||
|
||
TEST(can_decrement, can_decrement_stronk_integer) | ||
{ | ||
auto a = an_int_decrement_test_type {1}; | ||
|
||
EXPECT_EQ(--a, an_int_decrement_test_type {0}); | ||
EXPECT_EQ(a--, an_int_decrement_test_type {0}); | ||
EXPECT_EQ(a, an_int_decrement_test_type {-1}); | ||
} | ||
|
||
} // namespace twig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <cstdint> | ||
|
||
#include <gtest/gtest.h> | ||
#include <stronk/can_increment.h> | ||
#include <stronk/stronk.h> | ||
|
||
namespace twig | ||
{ | ||
|
||
struct an_int_increment_test_type : stronk<an_int_increment_test_type, int, can_increment, can_equate> | ||
{ | ||
using stronk::stronk; | ||
}; | ||
|
||
TEST(can_increment, can_increment_stronk_integer) | ||
{ | ||
auto a = an_int_increment_test_type {-1}; | ||
|
||
EXPECT_EQ(++a, an_int_increment_test_type {0}); | ||
EXPECT_EQ(a++, an_int_increment_test_type {0}); | ||
EXPECT_EQ(a, an_int_increment_test_type {1}); | ||
} | ||
|
||
struct a_uint64_t_increment_test_type : stronk<a_uint64_t_increment_test_type, uint64_t, can_increment, can_equate> | ||
{ | ||
using stronk::stronk; | ||
}; | ||
|
||
TEST(can_increment, can_increment_stronk_uint64_t) | ||
{ | ||
auto a = a_uint64_t_increment_test_type {0}; | ||
|
||
EXPECT_EQ(++a, a_uint64_t_increment_test_type {1}); | ||
EXPECT_EQ(a++, a_uint64_t_increment_test_type {1}); | ||
EXPECT_EQ(a, a_uint64_t_increment_test_type {2}); | ||
} | ||
|
||
} // namespace twig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.3.7" | ||
__version__ = "0.3.8" |