-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c0d774
commit eee954d
Showing
3 changed files
with
52 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,24 @@ | ||
#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: | ||
constexpr explicit Type(const T& value) : value_(value) {} | ||
[[nodiscard]] constexpr T& get() { return value_; } | ||
[[nodiscard]] constexpr const T& get() const { return value_; } | ||
[[nodiscard]] constexpr explicit operator T() const { return value_; } | ||
}; | ||
|
||
} // namespace rsl |
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,26 @@ | ||
#include <rsl/type.hpp> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
TEST_CASE("rsl::Type") { | ||
using StrongInt = rsl::Type<int, struct StrongIntTag>; // For testing constexpr support | ||
using StrongString = rsl::Type<std::string, struct StringStringTag>; // For testing non-constexpr types | ||
|
||
SECTION("Construction") { | ||
constexpr auto strong_int = StrongInt(42); | ||
STATIC_CHECK(strong_int.get() == 42); | ||
STATIC_CHECK(int{strong_int} == 42); | ||
STATIC_CHECK(int(strong_int) == 42); | ||
|
||
auto const strong_string = StrongString("abcdefg"); | ||
CHECK(strong_string.get() == "abcdefg"); | ||
CHECK(std::string{strong_string} == "abcdefg"); | ||
CHECK(std::string(strong_string) == "abcdefg"); | ||
} | ||
|
||
SECTION("get()") { | ||
auto strong_int = StrongInt(1337); | ||
strong_int.get() = 100; | ||
CHECK(strong_int.get() == 100); | ||
} | ||
} |