mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-04 03:32:27 +00:00
CTL: utility.h, use ctl::swap in string (#1227)
* Add ctl utility.h Implements forward, move, swap, and declval. This commit also adds a def for nullptr_t to cxx.inc. We need it now because the CTL headers stopped including anything from libc++, so we no longer get their basic types. * Use ctl::swap in string The STL spec says that swap is located in the string_view header anyawy. Performance-wise this is a noop, but it’s slightly cleaner.
This commit is contained in:
parent
a795017416
commit
9a5a13854d
13 changed files with 117 additions and 46 deletions
|
@ -3,9 +3,7 @@
|
|||
#ifndef COSMOPOLITAN_CTL_OPTIONAL_H_
|
||||
#define COSMOPOLITAN_CTL_OPTIONAL_H_
|
||||
#include "new.h"
|
||||
#include <__utility/forward.h>
|
||||
#include <__utility/move.h>
|
||||
#include <__utility/swap.h>
|
||||
#include "utility.h"
|
||||
|
||||
namespace ctl {
|
||||
|
||||
|
@ -32,7 +30,7 @@ class optional
|
|||
|
||||
optional(T&& value) : present_(true)
|
||||
{
|
||||
new (&value_) T(std::move(value));
|
||||
new (&value_) T(ctl::move(value));
|
||||
}
|
||||
|
||||
optional(const optional& other) : present_(other.present_)
|
||||
|
@ -44,7 +42,7 @@ class optional
|
|||
optional(optional&& other) noexcept : present_(other.present_)
|
||||
{
|
||||
if (other.present_)
|
||||
new (&value_) T(std::move(other.value_));
|
||||
new (&value_) T(ctl::move(other.value_));
|
||||
}
|
||||
|
||||
optional& operator=(const optional& other)
|
||||
|
@ -63,7 +61,7 @@ class optional
|
|||
if (this != &other) {
|
||||
reset();
|
||||
if (other.present_)
|
||||
new (&value_) T(std::move(other.value_));
|
||||
new (&value_) T(ctl::move(other.value_));
|
||||
present_ = other.present_;
|
||||
}
|
||||
return *this;
|
||||
|
@ -87,7 +85,7 @@ class optional
|
|||
{
|
||||
if (!present_)
|
||||
__builtin_trap();
|
||||
return std::move(value_);
|
||||
return ctl::move(value_);
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept
|
||||
|
@ -113,19 +111,19 @@ class optional
|
|||
{
|
||||
reset();
|
||||
present_ = true;
|
||||
new (&value_) T(std::forward<Args>(args)...);
|
||||
new (&value_) T(ctl::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
void swap(optional& other) noexcept
|
||||
{
|
||||
using std::swap;
|
||||
using ctl::swap;
|
||||
if (present_ && other.present_) {
|
||||
swap(value_, other.value_);
|
||||
} else if (present_) {
|
||||
other.emplace(std::move(value_));
|
||||
other.emplace(ctl::move(value_));
|
||||
reset();
|
||||
} else if (other.present_) {
|
||||
emplace(std::move(other.value_));
|
||||
emplace(ctl::move(other.value_));
|
||||
other.reset();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue