Skip to content

Commit 1ab42ba

Browse files
Revert "[libc] Extend optional to support non trivially destructible objects"
This reverts commit 7d06f59. This patch broke libc compilation with gcc as it doesn't seem to have __is_trivially_destructible(T).
1 parent 1faa479 commit 1ab42ba

File tree

2 files changed

+40
-52
lines changed

2 files changed

+40
-52
lines changed

libc/src/__support/CPP/optional.h

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,93 +36,85 @@ LIBC_INLINE_VAR constexpr in_place_t in_place{};
3636
// several assumptions that the underlying type is trivially constructable,
3737
// copyable, or movable.
3838
template <typename T> class optional {
39-
template <typename U, bool = !is_trivially_destructible<U>::value>
40-
struct OptionalStorage {
39+
template <typename U> class OptionalStorage {
4140
union {
4241
char empty;
4342
U stored_value;
4443
};
44+
bool in_use;
4545

46-
LIBC_INLINE ~OptionalStorage() { stored_value.~U(); }
47-
LIBC_INLINE constexpr OptionalStorage() : empty() {}
46+
public:
47+
LIBC_INLINE ~OptionalStorage() { reset(); }
48+
49+
LIBC_INLINE constexpr OptionalStorage() : empty(), in_use(false) {}
4850

4951
template <typename... Args>
5052
LIBC_INLINE constexpr explicit OptionalStorage(in_place_t, Args &&...args)
51-
: stored_value(forward<Args>(args)...) {}
52-
};
53+
: stored_value(forward<Args>(args)...), in_use(true) {}
5354

54-
template <typename U> struct OptionalStorage<U, false> {
55-
union {
56-
char empty;
57-
U stored_value;
58-
};
55+
LIBC_INLINE void reset() {
56+
if (in_use)
57+
stored_value.~U();
58+
in_use = false;
59+
}
5960

60-
// The only difference is that this class doesn't have a destructor.
61-
LIBC_INLINE constexpr OptionalStorage() : empty() {}
61+
LIBC_INLINE constexpr bool has_value() const { return in_use; }
6262

63-
template <typename... Args>
64-
LIBC_INLINE constexpr explicit OptionalStorage(in_place_t, Args &&...args)
65-
: stored_value(forward<Args>(args)...) {}
63+
LIBC_INLINE U &value() & { return stored_value; }
64+
LIBC_INLINE constexpr U const &value() const & { return stored_value; }
65+
LIBC_INLINE U &&value() && { return move(stored_value); }
6666
};
6767

6868
OptionalStorage<T> storage;
69-
bool in_use = false;
7069

7170
public:
7271
LIBC_INLINE constexpr optional() = default;
7372
LIBC_INLINE constexpr optional(nullopt_t) {}
7473

75-
LIBC_INLINE constexpr optional(const T &t)
76-
: storage(in_place, t), in_use(true) {}
74+
LIBC_INLINE constexpr optional(const T &t) : storage(in_place, t) {}
7775
LIBC_INLINE constexpr optional(const optional &) = default;
7876

79-
LIBC_INLINE constexpr optional(T &&t)
80-
: storage(in_place, move(t)), in_use(true) {}
77+
LIBC_INLINE constexpr optional(T &&t) : storage(in_place, move(t)) {}
8178
LIBC_INLINE constexpr optional(optional &&O) = default;
8279

8380
template <typename... ArgTypes>
8481
LIBC_INLINE constexpr optional(in_place_t, ArgTypes &&...Args)
85-
: storage(in_place, forward<ArgTypes>(Args)...), in_use(true) {}
82+
: storage(in_place, forward<ArgTypes>(Args)...) {}
8683

87-
LIBC_INLINE constexpr optional &operator=(T &&t) {
84+
LIBC_INLINE optional &operator=(T &&t) {
8885
storage = move(t);
8986
return *this;
9087
}
91-
LIBC_INLINE constexpr optional &operator=(optional &&) = default;
88+
LIBC_INLINE optional &operator=(optional &&) = default;
89+
90+
LIBC_INLINE static constexpr optional create(const T *t) {
91+
return t ? optional(*t) : optional();
92+
}
9293

93-
LIBC_INLINE constexpr optional &operator=(const T &t) {
94+
LIBC_INLINE optional &operator=(const T &t) {
9495
storage = t;
9596
return *this;
9697
}
97-
LIBC_INLINE constexpr optional &operator=(const optional &) = default;
98+
LIBC_INLINE optional &operator=(const optional &) = default;
9899

99-
LIBC_INLINE constexpr void reset() {
100-
if (in_use)
101-
storage.~OptionalStorage();
102-
in_use = false;
103-
}
100+
LIBC_INLINE void reset() { storage.reset(); }
104101

105-
LIBC_INLINE constexpr const T &value() const & {
106-
return storage.stored_value;
107-
}
102+
LIBC_INLINE constexpr const T &value() const & { return storage.value(); }
103+
LIBC_INLINE T &value() & { return storage.value(); }
108104

109-
LIBC_INLINE constexpr T &value() & { return storage.stored_value; }
105+
LIBC_INLINE constexpr explicit operator bool() const { return has_value(); }
106+
LIBC_INLINE constexpr bool has_value() const { return storage.has_value(); }
107+
LIBC_INLINE constexpr const T *operator->() const { return &storage.value(); }
108+
LIBC_INLINE T *operator->() { return &storage.value(); }
109+
LIBC_INLINE constexpr const T &operator*() const & { return value(); }
110+
LIBC_INLINE T &operator*() & { return value(); }
110111

111-
LIBC_INLINE constexpr explicit operator bool() const { return in_use; }
112-
LIBC_INLINE constexpr bool has_value() const { return in_use; }
113-
LIBC_INLINE constexpr const T *operator->() const {
114-
return &storage.stored_value;
115-
}
116-
LIBC_INLINE constexpr T *operator->() { return &storage.stored_value; }
117-
LIBC_INLINE constexpr const T &operator*() const & {
118-
return storage.stored_value;
112+
template <typename U> LIBC_INLINE constexpr T value_or(U &&value) const & {
113+
return has_value() ? value() : forward<U>(value);
119114
}
120-
LIBC_INLINE constexpr T &operator*() & { return storage.stored_value; }
121115

122-
LIBC_INLINE constexpr T &&value() && { return move(storage.stored_value); }
123-
LIBC_INLINE constexpr T &&operator*() && {
124-
return move(storage.stored_value);
125-
}
116+
LIBC_INLINE T &&value() && { return move(storage.value()); }
117+
LIBC_INLINE T &&operator*() && { return move(storage.value()); }
126118
};
127119

128120
} // namespace cpp

libc/src/__support/CPP/type_traits.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ constexpr bool
214214
details::void_t<decltype(details::convertible_to_helper<T>(
215215
declval<F>()))>> = true;
216216

217-
template <typename T>
218-
struct is_trivially_destructible
219-
: public integral_constant<bool, __is_trivially_destructible(T)> {};
220-
221217
} // namespace cpp
222218
} // namespace __llvm_libc
223219

0 commit comments

Comments
 (0)