[libc-commits] [PATCH] D131610: [libc][NFC] add arrow operator to optional
Michael Jones via Phabricator via libc-commits
libc-commits at lists.llvm.org
Wed Aug 10 13:27:49 PDT 2022
michaelrj updated this revision to Diff 451617.
michaelrj added a comment.
add tests and non-const version
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D131610/new/
https://reviews.llvm.org/D131610
Files:
libc/src/__support/CPP/optional.h
libc/test/src/__support/CPP/optional_test.cpp
Index: libc/test/src/__support/CPP/optional_test.cpp
===================================================================
--- libc/test/src/__support/CPP/optional_test.cpp
+++ libc/test/src/__support/CPP/optional_test.cpp
@@ -12,15 +12,19 @@
using __llvm_libc::cpp::nullopt;
using __llvm_libc::cpp::optional;
-// This has clase has two properties for testing:
-// 1) No default constructor
-// 2) A non-trivial destructor with an observable side-effect
+// This class has three properties for testing:
+// 1) No default constructor.
+// 2) A non-trivial destructor with an observable side-effect.
+// 3) Functions that can be called explicitly.
class Contrived {
int *_a;
public:
Contrived(int *a) : _a(a) {}
~Contrived() { (*_a)++; }
+
+ int get_a() { return *_a; }
+ void inc_a() { (*_a)++; }
};
TEST(LlvmLibcOptionalTest, Tests) {
@@ -59,4 +63,16 @@
ASSERT_FALSE(Trivial5.has_value());
optional<int> Trivial6 = Trivial5;
ASSERT_FALSE(Trivial6.has_value());
+
+ // Test operator->
+ int arrow_num = 5;
+ optional<Contrived> arrow_test(&arrow_num);
+ ASSERT_TRUE(arrow_test.has_value());
+ ASSERT_EQ(arrow_test->get_a(), arrow_num);
+ arrow_num = 10;
+ ASSERT_EQ(arrow_test->get_a(), arrow_num);
+ arrow_test->inc_a();
+ ASSERT_EQ(arrow_test->get_a(), arrow_num);
+ ASSERT_EQ(arrow_num, 11);
+ arrow_test.reset();
}
Index: libc/src/__support/CPP/optional.h
===================================================================
--- libc/src/__support/CPP/optional.h
+++ libc/src/__support/CPP/optional.h
@@ -74,6 +74,10 @@
constexpr T &operator*() & { return Storage.StoredValue; }
constexpr const T &operator*() const & { return Storage.StoredValue; }
+
+ constexpr T *operator->() { return &Storage.StoredValue; }
+
+ constexpr const T *operator->() const { return &Storage.StoredValue; }
};
} // namespace cpp
} // namespace __llvm_libc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131610.451617.patch
Type: text/x-patch
Size: 1894 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220810/2e4dfeff/attachment.bin>
More information about the libc-commits
mailing list