This commit is contained in:
Бранимир Караџић
2022-08-28 14:33:21 -07:00
parent 68dfd59820
commit 0ee20c6c83
6 changed files with 89 additions and 64 deletions

View File

@@ -31,9 +31,14 @@
#define BX_NEW(_allocator, _type) BX_PLACEMENT_NEW(BX_ALLOC(_allocator, sizeof(_type) ), _type)
#define BX_ALIGNED_NEW(_allocator, _type, _align) BX_PLACEMENT_NEW(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align), _type)
#define BX_PLACEMENT_NEW(_ptr, _type) ::new(bx::PlacementNewTag(), _ptr) _type
#define BX_PLACEMENT_NEW(_ptr, _type) ::new(bx::PlacementNew, _ptr) _type
namespace bx { struct PlacementNewTag {}; }
namespace bx
{
struct PlacementNewTag {};
constexpr PlacementNewTag PlacementNew;
} // namespace bx
void* operator new(size_t, bx::PlacementNewTag, void* _ptr);
void operator delete(void*, bx::PlacementNewTag, void*) throw();

View File

@@ -41,6 +41,14 @@ namespace bx
template<typename Ty>
constexpr bool isTriviallyCopyable();
/// Returns true if type `Ty` is signed type.
template<typename Ty>
constexpr bool isSigned();
/// Arithmetic type `Ty` limits.
template<typename Ty, bool SignT = isSigned<Ty>()>
struct LimitsT;
/// Find the address of an object of a class that has an overloaded unary ampersand (&) operator.
template<typename Ty>
Ty* addressOf(Ty& _a);
@@ -49,13 +57,25 @@ namespace bx
template<typename Ty>
const Ty* addressOf(const Ty& _a);
/// Returns typed pointer from typeless pointer offseted.
///
/// @param[in] _ptr Pointer to get offset from.
/// @param[in] _offsetInBytes Offset from pointer in bytes.
///
/// @returns Typed pointer from typeless pointer offseted.
///
template<typename Ty>
Ty* addressOf(void* _ptr, ptrdiff_t _offset);
Ty* addressOf(void* _ptr, ptrdiff_t _offsetInBytes = 0);
/// Returns typed pointer from typeless pointer offseted.
///
/// @param[in] _ptr Pointer to get offset from.
/// @param[in] _offsetInBytes Offset from pointer in bytes.
///
/// @returns Typed pointer from typeless pointer offseted.
///
template<typename Ty>
const Ty* addressOf(const void* _ptr, ptrdiff_t _offset);
const Ty* addressOf(const void* _ptr, ptrdiff_t _offsetInBytes = 0);
/// Swap two values.
template<typename Ty>

View File

@@ -54,15 +54,15 @@ namespace bx
}
template<typename Ty>
inline Ty* addressOf(void* _ptr, ptrdiff_t _offset)
inline Ty* addressOf(void* _ptr, ptrdiff_t _offsetInBytes)
{
return (Ty*)( (uint8_t*)_ptr + _offset);
return (Ty*)( (uint8_t*)_ptr + _offsetInBytes);
}
template<typename Ty>
inline const Ty* addressOf(const void* _ptr, ptrdiff_t _offset)
inline const Ty* addressOf(const void* _ptr, ptrdiff_t _offsetInBytes)
{
return (const Ty*)( (const uint8_t*)_ptr + _offset);
return (const Ty*)( (const uint8_t*)_ptr + _offsetInBytes);
}
template<typename Ty>
@@ -72,34 +72,34 @@ namespace bx
}
template<typename Ty>
struct IsSignedT { static constexpr bool value = Ty(-1) < Ty(0); };
template<typename Ty, bool SignT = IsSignedT<Ty>::value>
struct Limits;
constexpr bool isSigned()
{
return Ty(-1) < Ty(0);
}
template<typename Ty>
struct Limits<Ty, true>
struct LimitsT<Ty, true>
{
static constexpr Ty max = ( ( (Ty(1) << ( (sizeof(Ty) * 8) - 2) ) - Ty(1) ) << 1) | Ty(1);
static constexpr Ty min = -max - Ty(1);
};
template<typename Ty>
struct Limits<Ty, false>
struct LimitsT<Ty, false>
{
static constexpr Ty min = 0;
static constexpr Ty max = Ty(-1);
};
template<>
struct Limits<float, true>
struct LimitsT<float, true>
{
static constexpr float min = -kFloatLargest;
static constexpr float max = kFloatLargest;
};
template<>
struct Limits<double, true>
struct LimitsT<double, true>
{
static constexpr double min = -kDoubleLargest;
static constexpr double max = kDoubleLargest;
@@ -108,13 +108,13 @@ namespace bx
template<typename Ty>
inline constexpr Ty max()
{
return bx::Limits<Ty>::max;
return bx::LimitsT<Ty>::max;
}
template<typename Ty>
inline constexpr Ty min()
{
return bx::Limits<Ty>::min;
return bx::LimitsT<Ty>::min;
}
template<typename Ty>

View File

@@ -406,18 +406,18 @@ namespace bx
memCopy(_ptr, &_a, sizeof(Ty) );
}
inline Vec3::Vec3(init::NoneType)
inline Vec3::Vec3(init::NoneTag)
{
}
constexpr Vec3::Vec3(init::ZeroType)
constexpr Vec3::Vec3(init::ZeroTag)
: x(0.0f)
, y(0.0f)
, z(0.0f)
{
}
constexpr Vec3::Vec3(init::IdentityType)
constexpr Vec3::Vec3(init::IdentityTag)
: x(0.0f)
, y(0.0f)
, z(0.0f)
@@ -438,18 +438,18 @@ namespace bx
{
}
inline Plane::Plane(init::NoneType)
inline Plane::Plane(init::NoneTag)
: normal(init::None)
{
}
constexpr Plane::Plane(init::ZeroType)
constexpr Plane::Plane(init::ZeroTag)
: normal(init::Zero)
, dist(0.0f)
{
}
constexpr Plane::Plane(init::IdentityType)
constexpr Plane::Plane(init::IdentityTag)
: normal(0.0f, 1.0f, 0.0f)
, dist(0.0f)
{
@@ -461,11 +461,11 @@ namespace bx
{
}
inline Quaternion::Quaternion(init::NoneType)
inline Quaternion::Quaternion(init::NoneTag)
{
}
constexpr Quaternion::Quaternion(init::ZeroType)
constexpr Quaternion::Quaternion(init::ZeroTag)
: x(0.0f)
, y(0.0f)
, z(0.0f)
@@ -473,7 +473,7 @@ namespace bx
{
}
constexpr Quaternion::Quaternion(init::IdentityType)
constexpr Quaternion::Quaternion(init::IdentityTag)
: x(0.0f)
, y(0.0f)
, z(0.0f)

View File

@@ -39,18 +39,18 @@ namespace bx
{
/// Fields are left uninitialized.
///
struct NoneType {};
constexpr NoneType None;
struct NoneTag {};
constexpr NoneTag None;
/// Fields are initialized to zero.
///
struct ZeroType {};
constexpr ZeroType Zero;
struct ZeroTag {};
constexpr ZeroTag Zero;
/// Fields are initialized to identity value.
///
struct IdentityType {};
constexpr IdentityType Identity;
struct IdentityTag {};
constexpr IdentityTag Identity;
}
///
@@ -59,13 +59,13 @@ namespace bx
Vec3() = delete;
///
Vec3(init::NoneType);
Vec3(init::NoneTag);
///
constexpr Vec3(init::ZeroType);
constexpr Vec3(init::ZeroTag);
///
constexpr Vec3(init::IdentityType);
constexpr Vec3(init::IdentityTag);
///
explicit constexpr Vec3(float _v);
@@ -82,13 +82,13 @@ namespace bx
Plane() = delete;
///
Plane(init::NoneType);
Plane(init::NoneTag);
///
constexpr Plane(init::ZeroType);
constexpr Plane(init::ZeroTag);
///
constexpr Plane(init::IdentityType);
constexpr Plane(init::IdentityTag);
///
constexpr Plane(Vec3 _normal, float _dist);
@@ -103,13 +103,13 @@ namespace bx
Quaternion() = delete;
///
Quaternion(init::NoneType);
Quaternion(init::NoneTag);
///
constexpr Quaternion(init::ZeroType);
constexpr Quaternion(init::ZeroTag);
///
constexpr Quaternion(init::IdentityType);
constexpr Quaternion(init::IdentityTag);
///
constexpr Quaternion(float _x, float _y, float _z, float _w);