aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Bailey <jeffbailey@google.com>2022-08-05 02:44:02 +0000
committerJeff Bailey <jeffbailey@google.com>2022-08-05 02:51:44 +0000
commit3b631e47fe4cfc366fd107c748e6ac01b4f5974a (patch)
tree89509d857f1f7da7415163bc7ee376da52947099
parent40d74fcb552cef7c78d6dfc36d574440f6ea0e4c (diff)
[libc] Trivial implementation of std::optional
This class has only the minimum functionality in it to provide what the TZ variable parsing needs. In particular, the standard makes guarantees about how trivial the destructors are, throws an expception if it's used incorrectly, etc. There are also missing features. Tested: Trivial testsuite added, and use in development. Reviewed By: gchatelet Differential Revision: https://reviews.llvm.org/D129920
-rw-r--r--libc/src/__support/CPP/CMakeLists.txt6
-rw-r--r--libc/src/__support/CPP/optional.h81
-rw-r--r--libc/test/src/__support/CPP/CMakeLists.txt10
-rw-r--r--libc/test/src/__support/CPP/optional_test.cpp62
-rw-r--r--utils/bazel/llvm-project-overlay/libc/BUILD.bazel6
5 files changed, 165 insertions, 0 deletions
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index ff9ae5f7d9f4..cdd6787d2622 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -69,6 +69,12 @@ add_header_library(
)
add_header_library(
+ optional
+ HDRS
+ optional.h
+)
+
+add_header_library(
type_traits
HDRS
type_traits.h
diff --git a/libc/src/__support/CPP/optional.h b/libc/src/__support/CPP/optional.h
new file mode 100644
index 000000000000..43b6c78de2a5
--- /dev/null
+++ b/libc/src/__support/CPP/optional.h
@@ -0,0 +1,81 @@
+//===-- Standalone implementation std::optional -----------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_OPTIONAL_H
+#define LLVM_LIBC_SRC_SUPPORT_CPP_OPTIONAL_H
+
+namespace __llvm_libc {
+namespace cpp {
+
+// Trivial nullopt_t struct.
+struct nullopt_t {};
+
+// nullopt that can be used and returned
+inline constexpr nullopt_t nullopt;
+
+// This is very simple implementation of the std::optional class. There are a
+// number of guarantees in the standard that are not made here.
+//
+// This class will be extended as needed in future.
+//
+// T currently needs to be a scalar or an object with the following properties:
+// - copy constructible
+// - copy assignable
+// - destructible
+template <class T> class optional {
+
+ template <class E> class OptionalStorage {
+ public:
+ union {
+ E StoredValue;
+ char Placeholder;
+ };
+ bool InUse = false;
+
+ OptionalStorage() : Placeholder(0), InUse(false) {}
+ OptionalStorage(const E &t) : StoredValue(t), InUse(true) {}
+ ~OptionalStorage() {
+ if (InUse)
+ StoredValue.~E();
+ }
+
+ void reset() {
+ if (InUse)
+ StoredValue.~E();
+ InUse = false;
+ }
+ };
+
+ OptionalStorage<T> Storage;
+
+public:
+ optional() {}
+ optional(nullopt_t) {}
+ optional(const T &t) : Storage(t) {}
+
+ T value() const { return Storage.StoredValue; }
+
+ bool has_value() const { return Storage.InUse; }
+
+ void reset() { Storage.reset(); }
+
+ constexpr explicit operator bool() const { return Storage.InUse; }
+
+ constexpr optional &operator=(nullopt_t) {
+ reset();
+ return *this;
+ }
+
+ constexpr T &operator*() & { return Storage.StoredValue; }
+
+ constexpr const T &operator*() const & { return Storage.StoredValue; }
+};
+} // namespace cpp
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SUPPORT_CPP_OPTIONAL_H
diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt
index 01248c32eadd..5bc486038673 100644
--- a/libc/test/src/__support/CPP/CMakeLists.txt
+++ b/libc/test/src/__support/CPP/CMakeLists.txt
@@ -91,3 +91,13 @@ add_libc_unittest(
libc.src.__support.CPP.array_ref
libc.src.__support.CPP.stringstream
)
+
+add_libc_unittest(
+ optional_test
+ SUITE
+ libc_cpp_utils_unittests
+ SRCS
+ optional_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.optional
+)
diff --git a/libc/test/src/__support/CPP/optional_test.cpp b/libc/test/src/__support/CPP/optional_test.cpp
new file mode 100644
index 000000000000..f13f931f7041
--- /dev/null
+++ b/libc/test/src/__support/CPP/optional_test.cpp
@@ -0,0 +1,62 @@
+//===-- Unittests for Optional --------------------------------------------===//
+//
+// 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 "src/__support/CPP/optional.h"
+#include "utils/UnitTest/Test.h"
+
+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
+class Contrived {
+ int *_a;
+
+public:
+ Contrived(int *a) : _a(a) {}
+ ~Contrived() { (*_a)++; }
+};
+
+TEST(LlvmLibcOptionalTest, Tests) {
+ optional<int> Trivial1(12);
+ ASSERT_TRUE(Trivial1.has_value());
+ ASSERT_EQ(Trivial1.value(), 12);
+ ASSERT_EQ(*Trivial1, 12);
+ Trivial1.reset();
+ ASSERT_FALSE(Trivial1.has_value());
+
+ optional<int> Trivial2(12);
+ ASSERT_TRUE(Trivial2.has_value());
+ Trivial2 = nullopt;
+ ASSERT_FALSE(Trivial2.has_value());
+
+ // For this test case, the destructor increments the pointed-to value.
+ int holding = 1;
+ optional<Contrived> Complicated(&holding);
+ // Destructor was run once as part of copying the object.
+ ASSERT_EQ(holding, 2);
+ // Destructor was run a second time as part of destruction.
+ Complicated.reset();
+ ASSERT_EQ(holding, 3);
+ // Destructor was not run a third time as the object is already destroyed.
+ Complicated.reset();
+ ASSERT_EQ(holding, 3);
+
+ // Test that assigning an optional to another works when set
+ optional<int> Trivial3(12);
+ optional<int> Trivial4 = Trivial3;
+ ASSERT_TRUE(Trivial4.has_value());
+ ASSERT_EQ(Trivial4.value(), 12);
+
+ // Test that assigning an option to another works when unset
+ optional<int> Trivial5;
+ ASSERT_FALSE(Trivial5.has_value());
+ optional<int> Trivial6 = Trivial5;
+ ASSERT_FALSE(Trivial6.has_value());
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 607a4c0f866f..65675c9cc34f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -76,6 +76,12 @@ cc_library(
)
cc_library(
+ name = "__support_cpp_optional",
+ hdrs = ["src/__support/CPP/optional.h"],
+ deps = [":libc_root"],
+)
+
+cc_library(
name = "__support_cpp_string_view",
hdrs = ["src/__support/CPP/StringView.h"],
deps = [":libc_root"],