-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[ADT] Add and use (for AArch64) ValueOrSentinel<T, Sentinel>
#158120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MacDue
wants to merge
9
commits into
llvm:main
Choose a base branch
from
MacDue:sentinel_value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6f14b6d
[ADT] Add and use (for AArch64) `ValueWithSentinel<T, Sentinel>`
MacDue 13c85fd
Fixups
MacDue 0b1db69
Fixups
MacDue 9ea7165
Rename
MacDue b52e4c0
Support adjusted values
MacDue 82b9d71
Undo test name change
MacDue 421c7e0
Format
MacDue e450ef0
Mark some more methods as constexpr
MacDue c0f645e
Fixups
MacDue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,75 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file defines the ValueWithSentinel class, which is a type akin to a | ||
/// std::optional, but uses a sentinel rather than an additional "valid" flag. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_ADT_VALUEWITHSENTINEL_H | ||
#define LLVM_ADT_VALUEWITHSENTINEL_H | ||
|
||
#include <cassert> | ||
#include <limits> | ||
#include <utility> | ||
|
||
namespace llvm { | ||
|
||
template <typename T, T Sentinel> class ValueWithSentinel { | ||
public: | ||
ValueWithSentinel() = default; | ||
|
||
ValueWithSentinel(T Value) : Value(std::move(Value)) { | ||
assert(Value != Sentinel && "Value is sentinel (use default constructor)"); | ||
}; | ||
|
||
ValueWithSentinel &operator=(T const &NewValue) { | ||
assert(NewValue != Sentinel && "Assigned to sentinel (use .clear())"); | ||
Value = NewValue; | ||
return *this; | ||
} | ||
|
||
bool operator==(ValueWithSentinel const &Other) const { | ||
return Value == Other.Value; | ||
} | ||
|
||
bool operator!=(ValueWithSentinel const &Other) const { | ||
return !(*this == Other); | ||
} | ||
|
||
T &operator*() { | ||
assert(has_value() && "Invalid value"); | ||
return Value; | ||
} | ||
const T &operator*() const { | ||
return const_cast<ValueWithSentinel &>(*this).operator*(); | ||
kuhar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
T *operator->() { return &operator*(); } | ||
T const *operator->() const { return &operator*(); } | ||
|
||
T &value() { return operator*(); } | ||
T const &value() const { return operator*(); } | ||
|
||
bool has_value() const { return Value != Sentinel; } | ||
explicit operator bool() const { return has_value(); } | ||
kuhar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
void clear() { Value = Sentinel; } | ||
|
||
private: | ||
T Value{Sentinel}; | ||
}; | ||
|
||
template <typename T> | ||
using ValueWithSentinelNumericMax = | ||
ValueWithSentinel<T, std::numeric_limits<T>::max()>; | ||
|
||
} // namespace llvm | ||
|
||
#endif |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/ValueWithSentinel.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
TEST(ValueWithSentinelTest, Basic) { | ||
ValueWithSentinelNumericMax<int> Value; | ||
EXPECT_FALSE(Value.has_value()); | ||
|
||
Value = 1000; | ||
EXPECT_TRUE(Value.has_value()); | ||
|
||
EXPECT_EQ(Value, 1000); | ||
EXPECT_EQ(Value.value(), 1000); | ||
|
||
Value.clear(); | ||
EXPECT_FALSE(Value.has_value()); | ||
|
||
ValueWithSentinelNumericMax<int> OtherValue(99); | ||
EXPECT_TRUE(OtherValue.has_value()); | ||
EXPECT_NE(Value, OtherValue); | ||
|
||
Value = OtherValue; | ||
EXPECT_EQ(Value, OtherValue); | ||
} | ||
kuhar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
} // end anonymous namespace |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.