-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c1a495
commit 51656f0
Showing
7 changed files
with
272 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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,185 @@ | ||
#ifndef _OPTIONAL_H | ||
#define _OPTIONAL_H | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
|
||
template <typename T> | ||
struct Optional { | ||
using value_type = T; | ||
|
||
Optional() noexcept : | ||
populated(false) | ||
{} | ||
|
||
Optional(const Optional<T> &other) noexcept(std::is_nothrow_copy_constructible<T>::value) : | ||
populated(other.populated) | ||
{ | ||
if (populated) { | ||
new (&content.t) T(other.content.t); | ||
} | ||
} | ||
|
||
Optional(Optional<T> &&other) noexcept( | ||
std::is_nothrow_move_constructible<T>::value && | ||
std::is_nothrow_destructible<T>::value) : | ||
populated(other.populated) | ||
{ | ||
if (populated) { | ||
new (&content.t) T(std::move(other.content.t)); | ||
other.content.t.~T(); | ||
other.populated = false; | ||
} | ||
} | ||
|
||
Optional(const T &t) noexcept(std::is_nothrow_copy_constructible<T>::value) : | ||
populated(true) | ||
{ | ||
new (&content.t) T(t); | ||
} | ||
|
||
Optional(T &&t) noexcept(std::is_nothrow_move_constructible<T>::value) : | ||
populated(true) | ||
{ | ||
new (&content.t) T(std::move(t)); | ||
} | ||
|
||
~Optional() noexcept(std::is_nothrow_destructible<T>::value) | ||
{ | ||
if (populated) { | ||
content.t.~T(); | ||
} | ||
} | ||
|
||
Optional &operator=(const Optional<T> &other) noexcept( | ||
std::is_nothrow_copy_constructible<T>::value && | ||
std::is_nothrow_copy_assignable<T>::value && | ||
std::is_nothrow_destructible<T>::value) | ||
{ | ||
if (other.populated) { | ||
if (populated) { | ||
content.t = other.content.t; | ||
} else { | ||
new (&content.t) T(other.content.t); | ||
populated = true; | ||
} | ||
} else if (populated) { | ||
content.t.~T(); | ||
populated = false; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
Optional &operator=(Optional<T> &&other) noexcept( | ||
std::is_nothrow_move_constructible<T>::value && | ||
std::is_nothrow_move_assignable<T>::value && | ||
std::is_nothrow_destructible<T>::value) | ||
{ | ||
if (other.populated) { | ||
if (populated) { | ||
content.t = std::move(other.content.t); | ||
other.content.t.~T(); | ||
other.populated = false; | ||
} else { | ||
new (&content.t) T(std::move(other.content.t)); | ||
other.content.t.~T(); | ||
other.populated = false; | ||
populated = true; | ||
} | ||
} else if (populated) { | ||
content.t.~T(); | ||
populated = false; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
Optional &operator=(const T &other) noexcept( | ||
std::is_nothrow_copy_constructible<T>::value && | ||
std::is_nothrow_copy_assignable<T>::value && | ||
std::is_nothrow_destructible<T>::value) | ||
{ | ||
if (populated) { | ||
content.t = other; | ||
} else { | ||
new (&content.t) T(other); | ||
populated = true; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
Optional &operator=(T &&other) noexcept( | ||
std::is_nothrow_move_constructible<T>::value && | ||
std::is_nothrow_move_assignable<T>::value && | ||
std::is_nothrow_destructible<T>::value) | ||
{ | ||
if (populated) { | ||
content.t = std::move(other); | ||
} else { | ||
new (&content.t) T(std::move(other)); | ||
populated = true; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
operator bool() const noexcept | ||
{ | ||
return populated; | ||
} | ||
|
||
bool has_value() const noexcept | ||
{ | ||
return populated; | ||
} | ||
|
||
const T &operator*() const &noexcept | ||
{ | ||
return content.t; | ||
} | ||
|
||
T &operator*() &noexcept | ||
{ | ||
return content.t; | ||
} | ||
|
||
const T &&operator*() const &&noexcept | ||
{ | ||
return std::move(content.t); | ||
} | ||
|
||
T &&operator*() &&noexcept | ||
{ | ||
return std::move(content.t); | ||
} | ||
|
||
const T *operator->() const noexcept | ||
{ | ||
return &content.t; | ||
} | ||
|
||
T *operator->() noexcept | ||
{ | ||
return &content.t; | ||
} | ||
|
||
void reset() noexcept(std::is_nothrow_destructible<T>::value) | ||
{ | ||
if (populated) { | ||
content.t.~T(); | ||
populated = false; | ||
} | ||
} | ||
|
||
private: | ||
union Content { | ||
T t; | ||
}; | ||
|
||
bool populated; | ||
Content content; | ||
}; | ||
|
||
#endif /* _OPTIONAL_H */ |
This file contains 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 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 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
Oops, something went wrong.