mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-28 05:34:11 -04:00
ams: prefer construct_at/destroy_at over placement new/explicit destructor
This commit is contained in:
parent
aff0da9427
commit
d84dcb653d
49 changed files with 217 additions and 171 deletions
|
@ -29,7 +29,7 @@ namespace ams::util {
|
|||
private:
|
||||
ALWAYS_INLINE void FreeEntry(size_t i) {
|
||||
this->keys[i].reset();
|
||||
GetReference(this->values[i]).~Value();
|
||||
DestroyAt(this->values[i]);
|
||||
}
|
||||
public:
|
||||
constexpr BoundedMap() : keys(), values() { /* ... */ }
|
||||
|
@ -78,7 +78,7 @@ namespace ams::util {
|
|||
for (size_t i = 0; i < N; i++) {
|
||||
if (!this->keys[i]) {
|
||||
this->keys[i] = key;
|
||||
new (GetPointer(this->values[i])) Value(std::move(value));
|
||||
ConstructAt(this->values[i], std::forward<Value>(value));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ namespace ams::util {
|
|||
/* Try to find and assign an existing value. */
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (this->keys[i] && this->keys[i].value() == key) {
|
||||
GetReference(this->values[i]) = std::move(value);
|
||||
GetReference(this->values[i]) = std::forward<Value>(value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ namespace ams::util {
|
|||
for (size_t i = 0; i < N; i++) {
|
||||
if (!this->keys[i]) {
|
||||
this->keys[i] = key;
|
||||
new (GetPointer(this->values[i])) Value(std::move(value));
|
||||
ConstructAt(this->values[i], std::move(value));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ namespace ams::util {
|
|||
for (size_t i = 0; i < N; i++) {
|
||||
if (!this->keys[i]) {
|
||||
this->keys[i] = key;
|
||||
new (GetPointer(this->values[i])) Value(std::forward<Args>(args)...);
|
||||
ConstructAt(this->values[i], std::forward<Args>(args)...);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ namespace ams::util {
|
|||
ALWAYS_INLINE ScopeGuard(ScopeGuard&& rhs) : f(std::move(rhs.f)), active(rhs.active) {
|
||||
rhs.Cancel();
|
||||
}
|
||||
|
||||
ScopeGuard &operator=(ScopeGuard&& rhs) = delete;
|
||||
};
|
||||
|
||||
template<class F>
|
||||
|
|
|
@ -45,4 +45,46 @@ namespace ams::util {
|
|||
return *GetPointer(ts);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static constexpr ALWAYS_INLINE T *ConstructAt(TypedStorage<T> &ts, Args &&... args) {
|
||||
return std::construct_at(GetPointer(ts), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static constexpr ALWAYS_INLINE void DestroyAt(TypedStorage<T> &ts) {
|
||||
return std::destroy_at(GetPointer(ts));
|
||||
}
|
||||
|
||||
namespace impl {
|
||||
|
||||
template<typename T>
|
||||
class TypedStorageGuard {
|
||||
NON_COPYABLE(TypedStorageGuard);
|
||||
private:
|
||||
TypedStorage<T> &m_ts;
|
||||
bool m_active;
|
||||
public:
|
||||
template<typename... Args>
|
||||
constexpr ALWAYS_INLINE TypedStorageGuard(TypedStorage<T> &ts, Args &&... args) : m_ts(ts), m_active(true) {
|
||||
ConstructAt(m_ts, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE ~TypedStorageGuard() { if (m_active) { DestroyAt(m_ts); } }
|
||||
|
||||
ALWAYS_INLINE void Cancel() { m_active = false; }
|
||||
|
||||
ALWAYS_INLINE TypedStorageGuard(TypedStorageGuard&& rhs) : m_ts(rhs.m_ts), m_active(rhs.m_active) {
|
||||
rhs.Cancel();
|
||||
}
|
||||
|
||||
TypedStorageGuard &operator=(TypedStorageGuard&& rhs) = delete;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static constexpr ALWAYS_INLINE impl::TypedStorageGuard<T> ConstructAtGuarded(TypedStorage<T> &ts, Args &&... args) {
|
||||
return impl::TypedStorageGuard<T>(ts, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue