[llvm] r354199 - Make Optional<T> Trivially Copyable when T is trivially copyable
Serge Guelton via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 16 01:19:59 PST 2019
Author: serge_sans_paille
Date: Sat Feb 16 01:19:58 2019
New Revision: 354199
URL: http://llvm.org/viewvc/llvm-project?rev=354199&view=rev
Log:
Make Optional<T> Trivially Copyable when T is trivially copyable
This is another attempt in the process, works nicely on my setup,
let's check how it behaves on other targets.
Modified:
llvm/trunk/include/llvm/ADT/Optional.h
llvm/trunk/unittests/ADT/OptionalTest.cpp
Modified: llvm/trunk/include/llvm/ADT/Optional.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Optional.h?rev=354199&r1=354198&r2=354199&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Optional.h (original)
+++ llvm/trunk/include/llvm/ADT/Optional.h Sat Feb 16 01:19:58 2019
@@ -108,6 +108,59 @@ template <typename T, bool = is_triviall
return reinterpret_cast<const T *>(storage.buffer);
}
};
+template <typename T> struct OptionalStorage<T, true> {
+ AlignedCharArrayUnion<T> storage;
+ bool hasVal = false;
+
+ OptionalStorage() = default;
+
+ OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
+ OptionalStorage(const OptionalStorage &O) = default;
+ OptionalStorage(T &&y) : hasVal(true) {
+ new (storage.buffer) T(std::forward<T>(y));
+ }
+ OptionalStorage(OptionalStorage &&O) = default;
+
+ OptionalStorage &operator=(T &&y) {
+ if (hasVal)
+ *getPointer() = std::move(y);
+ else {
+ new (storage.buffer) T(std::move(y));
+ hasVal = true;
+ }
+ return *this;
+ }
+ OptionalStorage &operator=(OptionalStorage &&O) = default;
+
+ OptionalStorage &operator=(const T &y) {
+ if (hasVal)
+ *getPointer() = y;
+ else {
+ new (storage.buffer) T(y);
+ hasVal = true;
+ }
+ return *this;
+ }
+ OptionalStorage &operator=(const OptionalStorage &O) = default;
+
+ ~OptionalStorage() = default;
+
+ void reset() {
+ if (hasVal) {
+ (*getPointer()).~T();
+ hasVal = false;
+ }
+ }
+
+ T *getPointer() {
+ assert(hasVal);
+ return reinterpret_cast<T *>(storage.buffer);
+ }
+ const T *getPointer() const {
+ assert(hasVal);
+ return reinterpret_cast<const T *>(storage.buffer);
+ }
+};
} // namespace optional_detail
Modified: llvm/trunk/unittests/ADT/OptionalTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/OptionalTest.cpp?rev=354199&r1=354198&r2=354199&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/OptionalTest.cpp (original)
+++ llvm/trunk/unittests/ADT/OptionalTest.cpp Sat Feb 16 01:19:58 2019
@@ -14,8 +14,15 @@
#include <array>
+
using namespace llvm;
+static_assert(llvm::is_trivially_copyable<Optional<int>>::value,
+ "trivially copyable");
+
+static_assert(llvm::is_trivially_copyable<Optional<std::array<int, 3>>>::value,
+ "trivially copyable");
+
namespace {
struct NonDefaultConstructible {
More information about the llvm-commits
mailing list