Commit 4be50f7f authored by Yutaka Hirano's avatar Yutaka Hirano Committed by Commit Bot

Add explicit conversion support to StrongAlias

To make StrongAlias<Tag, bool> more attractive for replacing enum
used for boolean params, introduce explicit conversion to the
underlying type to StrongAlias.

Bug: 983022
Change-Id: I14599f05c558dbce9d0d86256079eddc6cea0a1f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1697401
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarŁukasz Anforowicz <lukasza@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676824}
parent ca5fe3ad
...@@ -57,6 +57,12 @@ namespace util { ...@@ -57,6 +57,12 @@ namespace util {
// in StrongAlias's interface. It's also potentially unwanted (ex. you don't // in StrongAlias's interface. It's also potentially unwanted (ex. you don't
// want to be able to add two StrongAliases that represent socket handles). // want to be able to add two StrongAliases that represent socket handles).
// A getter is provided in case you need to access the UnderlyingType. // A getter is provided in case you need to access the UnderlyingType.
//
// See also
// - //styleguide/c++.md which provides recommendation and examples of
// using StrongAlias<Tag, bool> instead of a bare bool.
// - base::util::IdType<...> which provides helpers for specializing
// StrongAlias to be used as an id.
template <typename TagType, typename UnderlyingType> template <typename TagType, typename UnderlyingType>
class StrongAlias { class StrongAlias {
public: public:
...@@ -71,6 +77,7 @@ class StrongAlias { ...@@ -71,6 +77,7 @@ class StrongAlias {
StrongAlias& operator=(StrongAlias&& other) = default; StrongAlias& operator=(StrongAlias&& other) = default;
const UnderlyingType& value() const { return value_; } const UnderlyingType& value() const { return value_; }
explicit operator UnderlyingType() const { return value_; }
bool operator==(const StrongAlias& other) const { bool operator==(const StrongAlias& other) const {
return value_ == other.value_; return value_ == other.value_;
......
...@@ -65,6 +65,13 @@ TYPED_TEST(StrongAliasTest, ValueAccessesUnderlyingValue) { ...@@ -65,6 +65,13 @@ TYPED_TEST(StrongAliasTest, ValueAccessesUnderlyingValue) {
"Reference returned by const value getter should be const."); "Reference returned by const value getter should be const.");
} }
TYPED_TEST(StrongAliasTest, ExplicitConversionToUnderlyingValue) {
using FooAlias = StrongAlias<class FooTag, TypeParam>;
const FooAlias const_alias(GetExampleValue<TypeParam>(1));
EXPECT_EQ(GetExampleValue<TypeParam>(1), static_cast<TypeParam>(const_alias));
}
TYPED_TEST(StrongAliasTest, CanBeCopyConstructed) { TYPED_TEST(StrongAliasTest, CanBeCopyConstructed) {
using FooAlias = StrongAlias<class FooTag, TypeParam>; using FooAlias = StrongAlias<class FooTag, TypeParam>;
FooAlias alias(GetExampleValue<TypeParam>(0)); FooAlias alias(GetExampleValue<TypeParam>(0));
...@@ -138,9 +145,6 @@ TYPED_TEST(StrongAliasTest, CannotBeCreatedFromDifferentAlias) { ...@@ -138,9 +145,6 @@ TYPED_TEST(StrongAliasTest, CannotBeCreatedFromDifferentAlias) {
TYPED_TEST(StrongAliasTest, CannotBeImplicitlyConverterToUnderlyingValue) { TYPED_TEST(StrongAliasTest, CannotBeImplicitlyConverterToUnderlyingValue) {
using FooAlias = StrongAlias<class FooTag, TypeParam>; using FooAlias = StrongAlias<class FooTag, TypeParam>;
static_assert(!std::is_constructible<TypeParam, FooAlias>::value,
"Should be impossible to construct an underlying type from a "
"StrongAlias.");
static_assert(!std::is_convertible<FooAlias, TypeParam>::value, static_assert(!std::is_convertible<FooAlias, TypeParam>::value,
"Should be impossible to implicitly convert a StrongAlias into " "Should be impossible to implicitly convert a StrongAlias into "
"an underlying type."); "an underlying type.");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment