Merge pull request #10 from lioncash/noexcept

Common: Make interfaces constexpr and noexcept where applicable
This commit is contained in:
Phillip Stephens 2019-08-24 21:16:04 -07:00 committed by GitHub
commit 520061a3bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 142 additions and 125 deletions

View File

@ -48,16 +48,16 @@ using LittleDNAV = athena::io::DNAVYaml<athena::Little>;
* SoundMacros, Tables, Keymaps, Layers, Samples, SFX, Songs */ * SoundMacros, Tables, Keymaps, Layers, Samples, SFX, Songs */
struct ObjectId { struct ObjectId {
uint16_t id = 0xffff; uint16_t id = 0xffff;
ObjectId() = default; constexpr ObjectId() noexcept = default;
ObjectId(uint16_t idIn) : id(idIn) {} constexpr ObjectId(uint16_t idIn) noexcept : id(idIn) {}
ObjectId& operator=(uint16_t idIn) { constexpr ObjectId& operator=(uint16_t idIn) noexcept {
id = idIn; id = idIn;
return *this; return *this;
} }
bool operator==(const ObjectId& other) const { return id == other.id; } constexpr bool operator==(const ObjectId& other) const noexcept { return id == other.id; }
bool operator!=(const ObjectId& other) const { return id != other.id; } constexpr bool operator!=(const ObjectId& other) const noexcept { return !operator==(other); }
bool operator<(const ObjectId& other) const { return id < other.id; } constexpr bool operator<(const ObjectId& other) const noexcept { return id < other.id; }
bool operator>(const ObjectId& other) const { return id > other.id; } constexpr bool operator>(const ObjectId& other) const noexcept { return id > other.id; }
static thread_local NameDB* CurNameDB; static thread_local NameDB* CurNameDB;
}; };
template <athena::Endian DNAEn> template <athena::Endian DNAEn>
@ -66,16 +66,16 @@ struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little) ObjectId
void _read(athena::io::YAMLDocReader& r); void _read(athena::io::YAMLDocReader& r);
void _write(athena::io::YAMLDocWriter& w); void _write(athena::io::YAMLDocWriter& w);
ObjectId id; ObjectId id;
ObjectIdDNA() = default; constexpr ObjectIdDNA() noexcept = default;
ObjectIdDNA(ObjectId idIn) : id(idIn) {} constexpr ObjectIdDNA(ObjectId idIn) noexcept : id(idIn) {}
operator ObjectId() const { return id; } constexpr operator ObjectId() const noexcept { return id; }
}; };
#define DECL_ID_TYPE(type) \ #define DECL_ID_TYPE(type) \
struct type : ObjectId { \ struct type : ObjectId { \
using ObjectId::ObjectId; \ using ObjectId::ObjectId; \
type() = default; \ constexpr type() noexcept = default; \
type(const ObjectId& idIn) : ObjectId(idIn) {} \ constexpr type(const ObjectId& idIn) noexcept : ObjectId(idIn) {} \
static thread_local NameDB* CurNameDB; \ static thread_local NameDB* CurNameDB; \
}; \ }; \
template <athena::Endian DNAEn> \ template <athena::Endian DNAEn> \
@ -84,9 +84,9 @@ struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little) ObjectId
void _read(athena::io::YAMLDocReader& r); \ void _read(athena::io::YAMLDocReader& r); \
void _write(athena::io::YAMLDocWriter& w); \ void _write(athena::io::YAMLDocWriter& w); \
type id; \ type id; \
type##DNA() = default; \ constexpr type##DNA() noexcept = default; \
type##DNA(type idIn) : id(idIn) {} \ constexpr type##DNA(type idIn) noexcept : id(idIn) {} \
operator type() const { return id; } \ constexpr operator type() const noexcept { return id; } \
}; };
DECL_ID_TYPE(SoundMacroId) DECL_ID_TYPE(SoundMacroId)
DECL_ID_TYPE(SampleId) DECL_ID_TYPE(SampleId)
@ -106,17 +106,17 @@ struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little) PageObje
void _read(athena::io::YAMLDocReader& r); void _read(athena::io::YAMLDocReader& r);
void _write(athena::io::YAMLDocWriter& w); void _write(athena::io::YAMLDocWriter& w);
ObjectId id; ObjectId id;
PageObjectIdDNA() = default; constexpr PageObjectIdDNA() noexcept = default;
PageObjectIdDNA(ObjectId idIn) : id(idIn) {} constexpr PageObjectIdDNA(ObjectId idIn) noexcept : id(idIn) {}
operator ObjectId() const { return id; } constexpr operator ObjectId() const noexcept { return id; }
}; };
struct SoundMacroStep { struct SoundMacroStep {
uint16_t step = 0; uint16_t step = 0;
operator uint16_t() const { return step; } constexpr operator uint16_t() const noexcept { return step; }
SoundMacroStep() = default; constexpr SoundMacroStep() noexcept = default;
SoundMacroStep(uint16_t idIn) : step(idIn) {} constexpr SoundMacroStep(uint16_t idIn) noexcept : step(idIn) {}
SoundMacroStep& operator=(uint16_t idIn) { constexpr SoundMacroStep& operator=(uint16_t idIn) noexcept {
step = idIn; step = idIn;
return *this; return *this;
} }
@ -126,18 +126,18 @@ template <athena::Endian DNAEn>
struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little) SoundMacroStepDNA : BigDNA { struct AT_SPECIALIZE_PARMS(athena::Endian::Big, athena::Endian::Little) SoundMacroStepDNA : BigDNA {
AT_DECL_EXPLICIT_DNA_YAML AT_DECL_EXPLICIT_DNA_YAML
SoundMacroStep step; SoundMacroStep step;
SoundMacroStepDNA() = default; constexpr SoundMacroStepDNA() noexcept = default;
SoundMacroStepDNA(SoundMacroStep idIn) : step(idIn) {} constexpr SoundMacroStepDNA(SoundMacroStep idIn) noexcept : step(idIn) {}
operator SoundMacroStep() const { return step; } constexpr operator SoundMacroStep() const noexcept { return step; }
}; };
struct LittleUInt24 : LittleDNA { struct LittleUInt24 : LittleDNA {
AT_DECL_EXPLICIT_DNA_YAML AT_DECL_EXPLICIT_DNA_YAML
atUint32 val; atUint32 val{};
operator uint32_t() const { return val; } constexpr operator uint32_t() const noexcept { return val; }
LittleUInt24() = default; constexpr LittleUInt24() noexcept = default;
LittleUInt24(uint32_t valIn) : val(valIn) {} constexpr LittleUInt24(uint32_t valIn) noexcept : val(valIn) {}
LittleUInt24& operator=(uint32_t valIn) { constexpr LittleUInt24& operator=(uint32_t valIn) noexcept {
val = valIn; val = valIn;
return *this; return *this;
} }
@ -150,10 +150,12 @@ protected:
virtual ~IObj() = default; virtual ~IObj() = default;
public: public:
void increment() { m_refCount++; } void increment() noexcept { m_refCount.fetch_add(std::memory_order_relaxed); }
void decrement() { void decrement() noexcept {
if (m_refCount.fetch_sub(1) == 1) if (m_refCount.fetch_sub(1, std::memory_order_release) == 1) {
std::atomic_thread_fence(std::memory_order_acquire);
delete this; delete this;
}
} }
}; };
@ -163,102 +165,115 @@ class ObjWrapper : public IObj {
public: public:
template <class... _Args> template <class... _Args>
ObjWrapper(_Args&&... args) : m_obj(std::forward<_Args>(args)...) {} ObjWrapper(_Args&&... args) noexcept : m_obj(std::forward<_Args>(args)...) {}
SubCls* get() { return &m_obj; } SubCls* get() noexcept { return &m_obj; }
const SubCls* get() const { return &m_obj; } const SubCls* get() const noexcept { return &m_obj; }
}; };
template <class SubCls> template <class SubCls>
class ObjTokenBase { class ObjTokenBase {
protected: protected:
IObj* m_obj = nullptr; IObj* m_obj = nullptr;
ObjTokenBase(IObj* obj) : m_obj(obj) { ObjTokenBase(IObj* obj) noexcept : m_obj(obj) {
if (m_obj) if (m_obj) {
m_obj->increment(); m_obj->increment();
}
} }
public: public:
ObjTokenBase() = default; ObjTokenBase() noexcept = default;
ObjTokenBase(const ObjTokenBase& other) : m_obj(other.m_obj) { ObjTokenBase(const ObjTokenBase& other) noexcept : m_obj(other.m_obj) {
if (m_obj) if (m_obj) {
m_obj->increment(); m_obj->increment();
}
} }
ObjTokenBase(ObjTokenBase&& other) : m_obj(other.m_obj) { other.m_obj = nullptr; } ObjTokenBase(ObjTokenBase&& other) noexcept : m_obj(other.m_obj) { other.m_obj = nullptr; }
ObjTokenBase& operator=(const ObjTokenBase& other) { ObjTokenBase& operator=(const ObjTokenBase& other) noexcept {
if (m_obj) if (m_obj) {
m_obj->decrement(); m_obj->decrement();
}
m_obj = other.m_obj; m_obj = other.m_obj;
if (m_obj) if (m_obj) {
m_obj->increment(); m_obj->increment();
}
return *this; return *this;
} }
ObjTokenBase& operator=(ObjTokenBase&& other) { ObjTokenBase& operator=(ObjTokenBase&& other) noexcept {
if (m_obj) if (m_obj) {
m_obj->decrement(); m_obj->decrement();
}
m_obj = other.m_obj; m_obj = other.m_obj;
other.m_obj = nullptr; other.m_obj = nullptr;
return *this; return *this;
} }
~ObjTokenBase() { ~ObjTokenBase() noexcept {
if (m_obj) if (!m_obj) {
m_obj->decrement(); return;
}
m_obj->decrement();
} }
bool operator==(const ObjTokenBase& other) const { return m_obj == other.m_obj; } bool operator==(const ObjTokenBase& other) const noexcept { return m_obj == other.m_obj; }
bool operator!=(const ObjTokenBase& other) const { return m_obj != other.m_obj; } bool operator!=(const ObjTokenBase& other) const noexcept { return !operator==(other); }
bool operator<(const ObjTokenBase& other) const { return m_obj < other.m_obj; } bool operator<(const ObjTokenBase& other) const noexcept { return m_obj < other.m_obj; }
bool operator>(const ObjTokenBase& other) const { return m_obj > other.m_obj; } bool operator>(const ObjTokenBase& other) const noexcept { return m_obj > other.m_obj; }
operator bool() const { return m_obj != nullptr; } operator bool() const noexcept { return m_obj != nullptr; }
void reset() { void reset() noexcept {
if (m_obj) if (m_obj) {
m_obj->decrement(); m_obj->decrement();
}
m_obj = nullptr; m_obj = nullptr;
} }
}; };
template <class SubCls, class Enable = void> template <class SubCls, class Enable = void>
class ObjToken : public ObjTokenBase<SubCls> { class ObjToken : public ObjTokenBase<SubCls> {
IObj*& _obj() { return ObjTokenBase<SubCls>::m_obj; } IObj*& _obj() noexcept { return ObjTokenBase<SubCls>::m_obj; }
IObj* const& _obj() const { return ObjTokenBase<SubCls>::m_obj; } IObj* const& _obj() const noexcept { return ObjTokenBase<SubCls>::m_obj; }
public: public:
using ObjTokenBase<SubCls>::ObjTokenBase; using ObjTokenBase<SubCls>::ObjTokenBase;
ObjToken() = default; ObjToken() noexcept = default;
ObjToken(ObjWrapper<SubCls>* obj) : ObjTokenBase<SubCls>(obj) {} ObjToken(ObjWrapper<SubCls>* obj) noexcept : ObjTokenBase<SubCls>(obj) {}
ObjToken& operator=(ObjWrapper<SubCls>* obj) { ObjToken& operator=(ObjWrapper<SubCls>* obj) noexcept {
if (_obj()) if (_obj()) {
_obj()->decrement(); _obj()->decrement();
}
_obj() = obj; _obj() = obj;
if (_obj()) if (_obj()) {
_obj()->increment(); _obj()->increment();
}
return *this; return *this;
} }
SubCls* get() const { return static_cast<ObjWrapper<SubCls>*>(_obj())->get(); } SubCls* get() const noexcept { return static_cast<ObjWrapper<SubCls>*>(_obj())->get(); }
SubCls* operator->() const { return get(); } SubCls* operator->() const noexcept { return get(); }
SubCls& operator*() const { return *get(); } SubCls& operator*() const noexcept { return *get(); }
}; };
template <class SubCls> template <class SubCls>
class ObjToken<SubCls, typename std::enable_if_t<std::is_base_of_v<IObj, SubCls>>> : public ObjTokenBase<SubCls> { class ObjToken<SubCls, typename std::enable_if_t<std::is_base_of_v<IObj, SubCls>>> : public ObjTokenBase<SubCls> {
IObj*& _obj() { return ObjTokenBase<SubCls>::m_obj; } IObj*& _obj() noexcept { return ObjTokenBase<SubCls>::m_obj; }
IObj* const& _obj() const { return ObjTokenBase<SubCls>::m_obj; } IObj* const& _obj() const noexcept { return ObjTokenBase<SubCls>::m_obj; }
public: public:
using ObjTokenBase<SubCls>::ObjTokenBase; using ObjTokenBase<SubCls>::ObjTokenBase;
ObjToken() = default; ObjToken() noexcept = default;
ObjToken(IObj* obj) : ObjTokenBase<SubCls>(obj) {} ObjToken(IObj* obj) noexcept : ObjTokenBase<SubCls>(obj) {}
ObjToken& operator=(IObj* obj) { ObjToken& operator=(IObj* obj) noexcept {
if (_obj()) if (_obj()) {
_obj()->decrement(); _obj()->decrement();
}
_obj() = obj; _obj() = obj;
if (_obj()) if (_obj()) {
_obj()->increment(); _obj()->increment();
}
return *this; return *this;
} }
SubCls* get() const { return static_cast<SubCls*>(_obj()); } SubCls* get() const noexcept { return static_cast<SubCls*>(_obj()); }
SubCls* operator->() const { return get(); } SubCls* operator->() const noexcept { return get(); }
SubCls& operator*() const { return *get(); } SubCls& operator*() const noexcept { return *get(); }
template <class T> template <class T>
T* cast() const { T* cast() const noexcept {
return static_cast<T*>(_obj()); return static_cast<T*>(_obj());
} }
}; };
@ -267,26 +282,28 @@ public:
* Bypasses type_traits tests for incomplete type definitions. */ * Bypasses type_traits tests for incomplete type definitions. */
template <class SubCls> template <class SubCls>
class IObjToken : public ObjTokenBase<SubCls> { class IObjToken : public ObjTokenBase<SubCls> {
IObj*& _obj() { return ObjTokenBase<SubCls>::m_obj; } IObj*& _obj() noexcept { return ObjTokenBase<SubCls>::m_obj; }
IObj* const& _obj() const { return ObjTokenBase<SubCls>::m_obj; } IObj* const& _obj() const noexcept { return ObjTokenBase<SubCls>::m_obj; }
public: public:
using ObjTokenBase<SubCls>::ObjTokenBase; using ObjTokenBase<SubCls>::ObjTokenBase;
IObjToken() = default; IObjToken() noexcept = default;
IObjToken(IObj* obj) : ObjTokenBase<SubCls>(obj) {} IObjToken(IObj* obj) noexcept : ObjTokenBase<SubCls>(obj) {}
IObjToken& operator=(IObj* obj) { IObjToken& operator=(IObj* obj) noexcept {
if (_obj()) if (_obj()) {
_obj()->decrement(); _obj()->decrement();
}
_obj() = obj; _obj() = obj;
if (_obj()) if (_obj()) {
_obj()->increment(); _obj()->increment();
}
return *this; return *this;
} }
SubCls* get() const { return static_cast<SubCls*>(_obj()); } SubCls* get() const noexcept { return static_cast<SubCls*>(_obj()); }
SubCls* operator->() const { return get(); } SubCls* operator->() const noexcept { return get(); }
SubCls& operator*() const { return *get(); } SubCls& operator*() const noexcept { return *get(); }
template <class T> template <class T>
T* cast() const { T* cast() const noexcept {
return static_cast<T*>(_obj()); return static_cast<T*>(_obj());
} }
}; };
@ -358,7 +375,7 @@ constexpr T clamp(T a, T val, T b) {
} }
template <typename T> template <typename T>
constexpr T ClampFull(float in) { constexpr T ClampFull(float in) noexcept {
if (std::is_floating_point<T>()) { if (std::is_floating_point<T>()) {
return in; return in;
} else { } else {
@ -443,7 +460,7 @@ bool Copy(const SystemChar* from, const SystemChar* to);
/* Type-sensitive byte swappers */ /* Type-sensitive byte swappers */
template <typename T> template <typename T>
constexpr T bswap16(T val) { constexpr T bswap16(T val) noexcept {
#if __GNUC__ #if __GNUC__
return __builtin_bswap16(val); return __builtin_bswap16(val);
#elif _WIN32 #elif _WIN32
@ -454,7 +471,7 @@ constexpr T bswap16(T val) {
} }
template <typename T> template <typename T>
constexpr T bswap32(T val) { constexpr T bswap32(T val) noexcept {
#if __GNUC__ #if __GNUC__
return __builtin_bswap32(val); return __builtin_bswap32(val);
#elif _WIN32 #elif _WIN32
@ -467,7 +484,7 @@ constexpr T bswap32(T val) {
} }
template <typename T> template <typename T>
constexpr T bswap64(T val) { constexpr T bswap64(T val) noexcept {
#if __GNUC__ #if __GNUC__
return __builtin_bswap64(val); return __builtin_bswap64(val);
#elif _WIN32 #elif _WIN32
@ -481,18 +498,18 @@ constexpr T bswap64(T val) {
} }
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
constexpr int16_t SBig(int16_t val) { return bswap16(val); } constexpr int16_t SBig(int16_t val) noexcept { return bswap16(val); }
constexpr uint16_t SBig(uint16_t val) { return bswap16(val); } constexpr uint16_t SBig(uint16_t val) noexcept { return bswap16(val); }
constexpr int32_t SBig(int32_t val) { return bswap32(val); } constexpr int32_t SBig(int32_t val) noexcept { return bswap32(val); }
constexpr uint32_t SBig(uint32_t val) { return bswap32(val); } constexpr uint32_t SBig(uint32_t val) noexcept { return bswap32(val); }
constexpr int64_t SBig(int64_t val) { return bswap64(val); } constexpr int64_t SBig(int64_t val) noexcept { return bswap64(val); }
constexpr uint64_t SBig(uint64_t val) { return bswap64(val); } constexpr uint64_t SBig(uint64_t val) noexcept { return bswap64(val); }
constexpr float SBig(float val) { constexpr float SBig(float val) noexcept {
union { float f; atInt32 i; } uval1 = {val}; union { float f; atInt32 i; } uval1 = {val};
union { atInt32 i; float f; } uval2 = {bswap32(uval1.i)}; union { atInt32 i; float f; } uval2 = {bswap32(uval1.i)};
return uval2.f; return uval2.f;
} }
constexpr double SBig(double val) { constexpr double SBig(double val) noexcept {
union { double f; atInt64 i; } uval1 = {val}; union { double f; atInt64 i; } uval1 = {val};
union { atInt64 i; double f; } uval2 = {bswap64(uval1.i)}; union { atInt64 i; double f; } uval2 = {bswap64(uval1.i)};
return uval2.f; return uval2.f;
@ -501,29 +518,29 @@ constexpr double SBig(double val) {
#define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24) #define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
#endif #endif
constexpr int16_t SLittle(int16_t val) { return val; } constexpr int16_t SLittle(int16_t val) noexcept { return val; }
constexpr uint16_t SLittle(uint16_t val) { return val; } constexpr uint16_t SLittle(uint16_t val) noexcept { return val; }
constexpr int32_t SLittle(int32_t val) { return val; } constexpr int32_t SLittle(int32_t val) noexcept { return val; }
constexpr uint32_t SLittle(uint32_t val) { return val; } constexpr uint32_t SLittle(uint32_t val) noexcept { return val; }
constexpr int64_t SLittle(int64_t val) { return val; } constexpr int64_t SLittle(int64_t val) noexcept { return val; }
constexpr uint64_t SLittle(uint64_t val) { return val; } constexpr uint64_t SLittle(uint64_t val) noexcept { return val; }
constexpr float SLittle(float val) { return val; } constexpr float SLittle(float val) noexcept { return val; }
constexpr double SLittle(double val) { return val; } constexpr double SLittle(double val) noexcept { return val; }
#ifndef SLITTLE #ifndef SLITTLE
#define SLITTLE(q) (q) #define SLITTLE(q) (q)
#endif #endif
#else #else
constexpr int16_t SLittle(int16_t val) { return bswap16(val); } constexpr int16_t SLittle(int16_t val) noexcept { return bswap16(val); }
constexpr uint16_t SLittle(uint16_t val) { return bswap16(val); } constexpr uint16_t SLittle(uint16_t val) noexcept { return bswap16(val); }
constexpr int32_t SLittle(int32_t val) { return bswap32(val); } constexpr int32_t SLittle(int32_t val) noexcept { return bswap32(val); }
constexpr uint32_t SLittle(uint32_t val) { return bswap32(val); } constexpr uint32_t SLittle(uint32_t val) noexcept { return bswap32(val); }
constexpr int64_t SLittle(int64_t val) { return bswap64(val); } constexpr int64_t SLittle(int64_t val) noexcept { return bswap64(val); }
constexpr uint64_t SLittle(uint64_t val) { return bswap64(val); } constexpr uint64_t SLittle(uint64_t val) noexcept { return bswap64(val); }
constexpr float SLittle(float val) { constexpr float SLittle(float val) noexcept {
int32_t ival = bswap32(*((int32_t*)(&val))); int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival)); return *((float*)(&ival));
} }
constexpr double SLittle(double val) { constexpr double SLittle(double val) noexcept {
int64_t ival = bswap64(*((int64_t*)(&val))); int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival)); return *((double*)(&ival));
} }
@ -531,14 +548,14 @@ constexpr double SLittle(double val) {
#define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24) #define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
#endif #endif
constexpr int16_t SBig(int16_t val) { return val; } constexpr int16_t SBig(int16_t val) noexcept { return val; }
constexpr uint16_t SBig(uint16_t val) { return val; } constexpr uint16_t SBig(uint16_t val) noexcept { return val; }
constexpr int32_t SBig(int32_t val) { return val; } constexpr int32_t SBig(int32_t val) noexcept { return val; }
constexpr uint32_t SBig(uint32_t val) { return val; } constexpr uint32_t SBig(uint32_t val) noexcept { return val; }
constexpr int64_t SBig(int64_t val) { return val; } constexpr int64_t SBig(int64_t val) noexcept { return val; }
constexpr uint64_t SBig(uint64_t val) { return val; } constexpr uint64_t SBig(uint64_t val) noexcept { return val; }
constexpr float SBig(float val) { return val; } constexpr float SBig(float val) noexcept { return val; }
constexpr double SBig(double val) { return val; } constexpr double SBig(double val) noexcept { return val; }
#ifndef SBIG #ifndef SBIG
#define SBIG(q) (q) #define SBIG(q) (q)
#endif #endif