Skip to content

Commit

Permalink
Add rsl::Type
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 25, 2023
1 parent 73ca8e1 commit 269ac8d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
25 changes: 25 additions & 0 deletions include/rsl/type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

namespace rsl {

/** @file */

/**
* @brief Class template for creating strong type aliases
*
* @tparam T value type
* @tparam Tag Tag type to diambiguate separate type aliases
*/
template <typename T, typename Tag>
class Type {
T value_;

public:
using ValueType = T;

explicit Type(const T& value) : value_(value) {}
[[nodiscard]] T& get() { return value_; }
[[nodiscard]] const T& get() const { return value_; }
};

} // namespace rsl
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ add_executable(test-rsl
random.cpp
static_string.cpp
static_vector.cpp
try.cpp)
try.cpp
type.cpp)
target_link_libraries(test-rsl PRIVATE
rsl::rsl
Catch2::Catch2WithMain
Expand Down
19 changes: 19 additions & 0 deletions tests/type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <rsl/type.hpp>

#include <catch2/catch_test_macros.hpp>

TEST_CASE("rsl::Type") {
using StrongInt = rsl::Type<int, struct StrongIntTag>;
static_assert(std::is_same_v<StrongInt::ValueType, int>);

SECTION("Construction") {
auto const strong_int = StrongInt(42);
CHECK(strong_int.get() == 42);
}

SECTION("get()") {
auto strong_int = StrongInt(1337);
strong_int.get() = 100;
CHECK(strong_int.get() == 100);
}
}

0 comments on commit 269ac8d

Please sign in to comment.