util/Manual: add noexcept

This commit is contained in:
Max Kellermann 2022-05-31 16:10:28 +02:00 committed by Max Kellermann
parent d2983b7fde
commit f92bae887f

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2013-2017 Max Kellermann <max.kellermann@gmail.com> * Copyright 2013-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,8 +27,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef MANUAL_HXX #pragma once
#define MANUAL_HXX
#include <cassert> #include <cassert>
#include <new> #include <new>
@ -56,7 +55,7 @@ class Manual {
public: public:
#ifndef NDEBUG #ifndef NDEBUG
~Manual() { ~Manual() noexcept {
assert(!initialized); assert(!initialized);
} }
#endif #endif
@ -64,7 +63,7 @@ public:
/** /**
* Cast a value reference to the containing Manual instance. * Cast a value reference to the containing Manual instance.
*/ */
static constexpr Manual<T> &Cast(T &value) { static constexpr Manual<T> &Cast(T &value) noexcept {
return reinterpret_cast<Manual<T> &>(value); return reinterpret_cast<Manual<T> &>(value);
} }
@ -80,7 +79,7 @@ public:
#endif #endif
} }
void Destruct() { void Destruct() noexcept {
assert(initialized); assert(initialized);
T &t = Get(); T &t = Get();
@ -91,33 +90,33 @@ public:
#endif #endif
} }
T &Get() { T &Get() noexcept {
assert(initialized); assert(initialized);
void *p = static_cast<void *>(data); void *p = static_cast<void *>(data);
return *static_cast<T *>(p); return *static_cast<T *>(p);
} }
const T &Get() const { const T &Get() const noexcept {
assert(initialized); assert(initialized);
const void *p = static_cast<const void *>(data); const void *p = static_cast<const void *>(data);
return *static_cast<const T *>(p); return *static_cast<const T *>(p);
} }
operator T &() { operator T &() noexcept {
return Get(); return Get();
} }
operator const T &() const { operator const T &() const noexcept {
return Get(); return Get();
} }
T *operator->() { T *operator->() noexcept {
return &Get(); return &Get();
} }
const T *operator->() const { const T *operator->() const noexcept {
return &Get(); return &Get();
} }
}; };
@ -125,5 +124,3 @@ public:
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
#endif