Skip to content

Commit

Permalink
Add can_increment and can_decrement (#43)
Browse files Browse the repository at this point in the history
### Why 
It can simplify the use of stronk types with custom methods.

### What was changed
- **Add can_increment/decrement**
- **Version bump**
  • Loading branch information
mikaellindemann authored May 2, 2024
1 parent 6369371 commit ffeff8b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
23 changes: 23 additions & 0 deletions include/stronk/can_decrement.h
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
23 changes: 23 additions & 0 deletions include/stronk/can_increment.h
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
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ find_package(absl CONFIG REQUIRED)
# ---- Tests ----
add_executable(
stronk_test
src/can_decrement_tests.cpp
src/can_increment_tests.cpp
src/extensions_tests.cpp
src/specializers_tests.cpp
src/stronk_tests.cpp
Expand Down
22 changes: 22 additions & 0 deletions tests/src/can_decrement_tests.cpp
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
38 changes: 38 additions & 0 deletions tests/src/can_increment_tests.cpp
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
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.7"
__version__ = "0.3.8"

0 comments on commit ffeff8b

Please sign in to comment.