diff --git a/include/bx/allocator.h b/include/bx/allocator.h index aa0757e..c5555ea 100644 --- a/include/bx/allocator.h +++ b/include/bx/allocator.h @@ -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(); diff --git a/include/bx/bx.h b/include/bx/bx.h index 5e0f981..3555c7b 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -41,6 +41,14 @@ namespace bx template constexpr bool isTriviallyCopyable(); + /// Returns true if type `Ty` is signed type. + template + constexpr bool isSigned(); + + /// Arithmetic type `Ty` limits. + template()> + struct LimitsT; + /// Find the address of an object of a class that has an overloaded unary ampersand (&) operator. template Ty* addressOf(Ty& _a); @@ -49,13 +57,25 @@ namespace bx template 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 - 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 - const Ty* addressOf(const void* _ptr, ptrdiff_t _offset); + const Ty* addressOf(const void* _ptr, ptrdiff_t _offsetInBytes = 0); /// Swap two values. template diff --git a/include/bx/inline/bx.inl b/include/bx/inline/bx.inl index 52c9a58..af0e0ea 100644 --- a/include/bx/inline/bx.inl +++ b/include/bx/inline/bx.inl @@ -54,15 +54,15 @@ namespace bx } template - 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 - 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 @@ -72,34 +72,34 @@ namespace bx } template - struct IsSignedT { static constexpr bool value = Ty(-1) < Ty(0); }; - - template::value> - struct Limits; + constexpr bool isSigned() + { + return Ty(-1) < Ty(0); + } template - struct Limits + struct LimitsT { static constexpr Ty max = ( ( (Ty(1) << ( (sizeof(Ty) * 8) - 2) ) - Ty(1) ) << 1) | Ty(1); static constexpr Ty min = -max - Ty(1); }; template - struct Limits + struct LimitsT { static constexpr Ty min = 0; static constexpr Ty max = Ty(-1); }; template<> - struct Limits + struct LimitsT { static constexpr float min = -kFloatLargest; static constexpr float max = kFloatLargest; }; template<> - struct Limits + struct LimitsT { static constexpr double min = -kDoubleLargest; static constexpr double max = kDoubleLargest; @@ -108,13 +108,13 @@ namespace bx template inline constexpr Ty max() { - return bx::Limits::max; + return bx::LimitsT::max; } template inline constexpr Ty min() { - return bx::Limits::min; + return bx::LimitsT::min; } template diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index 37493f3..df433fc 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -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) diff --git a/include/bx/math.h b/include/bx/math.h index f0e2acb..a02549b 100644 --- a/include/bx/math.h +++ b/include/bx/math.h @@ -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); diff --git a/tests/math_test.cpp b/tests/math_test.cpp index 3ce37eb..5116559 100644 --- a/tests/math_test.cpp +++ b/tests/math_test.cpp @@ -339,36 +339,36 @@ TEST_CASE("quaternion", "") TEST_CASE("limits", "") { - STATIC_REQUIRE(bx::Limits::min == INT8_MIN); - STATIC_REQUIRE(bx::Limits::max == INT8_MAX); + STATIC_REQUIRE(bx::LimitsT::min == INT8_MIN); + STATIC_REQUIRE(bx::LimitsT::max == INT8_MAX); - STATIC_REQUIRE(bx::Limits::min == CHAR_MIN); - STATIC_REQUIRE(bx::Limits::max == CHAR_MAX); + STATIC_REQUIRE(bx::LimitsT::min == CHAR_MIN); + STATIC_REQUIRE(bx::LimitsT::max == CHAR_MAX); - STATIC_REQUIRE(bx::Limits::min == 0); - STATIC_REQUIRE(bx::Limits::max == UCHAR_MAX); + STATIC_REQUIRE(bx::LimitsT::min == 0); + STATIC_REQUIRE(bx::LimitsT::max == UCHAR_MAX); - STATIC_REQUIRE(bx::Limits::min == INT16_MIN); - STATIC_REQUIRE(bx::Limits::max == INT16_MAX); + STATIC_REQUIRE(bx::LimitsT::min == INT16_MIN); + STATIC_REQUIRE(bx::LimitsT::max == INT16_MAX); - STATIC_REQUIRE(bx::Limits::min == 0); - STATIC_REQUIRE(bx::Limits::max == UINT16_MAX); + STATIC_REQUIRE(bx::LimitsT::min == 0); + STATIC_REQUIRE(bx::LimitsT::max == UINT16_MAX); - STATIC_REQUIRE(bx::Limits::min == INT32_MIN); - STATIC_REQUIRE(bx::Limits::max == INT32_MAX); + STATIC_REQUIRE(bx::LimitsT::min == INT32_MIN); + STATIC_REQUIRE(bx::LimitsT::max == INT32_MAX); - STATIC_REQUIRE(bx::Limits::min == 0); - STATIC_REQUIRE(bx::Limits::max == UINT32_MAX); + STATIC_REQUIRE(bx::LimitsT::min == 0); + STATIC_REQUIRE(bx::LimitsT::max == UINT32_MAX); - STATIC_REQUIRE(bx::Limits::min == INT64_MIN); - STATIC_REQUIRE(bx::Limits::max == INT64_MAX); + STATIC_REQUIRE(bx::LimitsT::min == INT64_MIN); + STATIC_REQUIRE(bx::LimitsT::max == INT64_MAX); - STATIC_REQUIRE(bx::Limits::min == 0); - STATIC_REQUIRE(bx::Limits::max == UINT64_MAX); + STATIC_REQUIRE(bx::LimitsT::min == 0); + STATIC_REQUIRE(bx::LimitsT::max == UINT64_MAX); - STATIC_REQUIRE(bx::Limits::min == std::numeric_limits::lowest() ); - STATIC_REQUIRE(bx::Limits::max == std::numeric_limits::max() ); + STATIC_REQUIRE(bx::LimitsT::min == std::numeric_limits::lowest() ); + STATIC_REQUIRE(bx::LimitsT::max == std::numeric_limits::max() ); - STATIC_REQUIRE(bx::Limits::min == std::numeric_limits::lowest() ); - STATIC_REQUIRE(bx::Limits::max == std::numeric_limits::max() ); + STATIC_REQUIRE(bx::LimitsT::min == std::numeric_limits::lowest() ); + STATIC_REQUIRE(bx::LimitsT::max == std::numeric_limits::max() ); }