Havok: Add hkEnum and hkFlags

This commit is contained in:
Léo Lam 2021-12-16 00:30:22 +01:00
parent fb8f0c636e
commit dbbc835ca2
No known key found for this signature in database
GPG Key ID: 0DF30F9081000741
5 changed files with 66 additions and 0 deletions

3
lib/hkStubs/.clang-tidy Normal file
View File

@ -0,0 +1,3 @@
---
InheritParentConfig: true
Checks: "-modernize-use-equals-default"

View File

@ -2,6 +2,7 @@ project(hkStubs CXX ASM)
add_library(hkStubs OBJECT
Havok/Common/Base/Container/String/hkStringPtr.h
Havok/Common/Base/Types/hkBaseDefs.h
Havok/Common/Base/Types/hkBaseTypes.h
Havok/Common/Base/hkBase.h
Havok/Common/Base/Math/hkMath.h

View File

@ -0,0 +1,5 @@
#pragma once
#define HK_FORCE_INLINE inline
#define HK_ALWAYS_INLINE HK_FORCE_INLINE
#define HK_NEVER_INLINE __attribute__((noinline))

View File

@ -62,3 +62,59 @@ public:
private:
hkInt16 m_value;
};
/// For storing an enum with a particular storage size when specifying the underlying type of the
/// enum is not an option.
template <typename Enum, typename Storage>
struct hkEnum {
HK_ALWAYS_INLINE hkEnum() {}
hkEnum(Enum value) { *this = value; } // NOLINT(google-explicit-constructor)
// NOLINTNEXTLINE(google-explicit-constructor)
operator Enum() const { return static_cast<Enum>(m_storage); }
hkEnum& operator=(Enum value) {
m_storage = static_cast<Storage>(value);
return *this;
}
bool operator==(Enum e) const { return m_storage == static_cast<Storage>(e); }
bool operator!=(Enum e) const { return m_storage != static_cast<Storage>(e); }
Storage m_storage;
};
template <typename, typename Storage>
class hkFlags {
public:
HK_FORCE_INLINE hkFlags() {}
HK_FORCE_INLINE explicit hkFlags(Storage s) : m_storage(s) {}
HK_FORCE_INLINE void clear() { m_storage = 0; }
HK_FORCE_INLINE void clear(Storage mask) { m_storage &= ~mask; }
HK_FORCE_INLINE void setAll(Storage s) { m_storage = s; }
HK_FORCE_INLINE void operator|=(Storage s) { m_storage |= s; }
HK_FORCE_INLINE void operator^=(Storage s) { m_storage ^= s; }
HK_FORCE_INLINE void operator&=(Storage s) { m_storage &= s; }
HK_FORCE_INLINE void setWithMask(Storage s, Storage mask) {
m_storage = (m_storage & ~mask) | (s & mask);
}
HK_FORCE_INLINE Storage get() const { return m_storage; }
HK_FORCE_INLINE bool anyIsSet(Storage mask) const { return (m_storage & mask) != 0; }
HK_FORCE_INLINE bool noneIsSet(Storage mask) const { return (m_storage & mask) == 0; }
HK_FORCE_INLINE bool allAreSet(Storage mask) const { return (m_storage & mask) == mask; }
HK_FORCE_INLINE bool operator==(const hkFlags& other) const {
return other.m_storage == m_storage;
}
HK_FORCE_INLINE bool operator!=(const hkFlags& other) const {
return other.m_storage != m_storage;
}
private:
Storage m_storage;
};

View File

@ -2,6 +2,7 @@
#include <Havok/Common/Base/Object/hkBaseObject.h>
#include <Havok/Common/Base/Object/hkReferencedObject.h>
#include <Havok/Common/Base/Types/hkBaseDefs.h>
#include <Havok/Common/Base/Types/hkBaseTypes.h>
#include <Havok/Common/Base/Math/hkMath.h>