Бранимир Караџић 3 years ago
parent
commit
0ee20c6c83
6 changed files with 89 additions and 64 deletions
  1. 7 2
      include/bx/allocator.h
  2. 22 2
      include/bx/bx.h
  3. 14 14
      include/bx/inline/bx.inl
  4. 9 9
      include/bx/inline/math.inl
  5. 15 15
      include/bx/math.h
  6. 22 22
      tests/math_test.cpp

+ 7 - 2
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();

+ 22 - 2
include/bx/bx.h

@@ -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>

+ 14 - 14
include/bx/inline/bx.inl

@@ -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>

+ 9 - 9
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)

+ 15 - 15
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);

+ 22 - 22
tests/math_test.cpp

@@ -339,36 +339,36 @@ TEST_CASE("quaternion", "")
 
 TEST_CASE("limits", "")
 {
-	STATIC_REQUIRE(bx::Limits<int8_t>::min == INT8_MIN);
-	STATIC_REQUIRE(bx::Limits<int8_t>::max == INT8_MAX);
+	STATIC_REQUIRE(bx::LimitsT<int8_t>::min == INT8_MIN);
+	STATIC_REQUIRE(bx::LimitsT<int8_t>::max == INT8_MAX);
 
-	STATIC_REQUIRE(bx::Limits<signed char>::min == CHAR_MIN);
-	STATIC_REQUIRE(bx::Limits<signed char>::max == CHAR_MAX);
+	STATIC_REQUIRE(bx::LimitsT<signed char>::min == CHAR_MIN);
+	STATIC_REQUIRE(bx::LimitsT<signed char>::max == CHAR_MAX);
 
-	STATIC_REQUIRE(bx::Limits<unsigned char>::min == 0);
-	STATIC_REQUIRE(bx::Limits<unsigned char>::max == UCHAR_MAX);
+	STATIC_REQUIRE(bx::LimitsT<unsigned char>::min == 0);
+	STATIC_REQUIRE(bx::LimitsT<unsigned char>::max == UCHAR_MAX);
 
-	STATIC_REQUIRE(bx::Limits<int16_t>::min == INT16_MIN);
-	STATIC_REQUIRE(bx::Limits<int16_t>::max == INT16_MAX);
+	STATIC_REQUIRE(bx::LimitsT<int16_t>::min == INT16_MIN);
+	STATIC_REQUIRE(bx::LimitsT<int16_t>::max == INT16_MAX);
 
-	STATIC_REQUIRE(bx::Limits<uint16_t>::min == 0);
-	STATIC_REQUIRE(bx::Limits<uint16_t>::max == UINT16_MAX);
+	STATIC_REQUIRE(bx::LimitsT<uint16_t>::min == 0);
+	STATIC_REQUIRE(bx::LimitsT<uint16_t>::max == UINT16_MAX);
 
-	STATIC_REQUIRE(bx::Limits<int32_t>::min == INT32_MIN);
-	STATIC_REQUIRE(bx::Limits<int32_t>::max == INT32_MAX);
+	STATIC_REQUIRE(bx::LimitsT<int32_t>::min == INT32_MIN);
+	STATIC_REQUIRE(bx::LimitsT<int32_t>::max == INT32_MAX);
 
-	STATIC_REQUIRE(bx::Limits<uint32_t>::min == 0);
-	STATIC_REQUIRE(bx::Limits<uint32_t>::max == UINT32_MAX);
+	STATIC_REQUIRE(bx::LimitsT<uint32_t>::min == 0);
+	STATIC_REQUIRE(bx::LimitsT<uint32_t>::max == UINT32_MAX);
 
-	STATIC_REQUIRE(bx::Limits<int64_t>::min == INT64_MIN);
-	STATIC_REQUIRE(bx::Limits<int64_t>::max == INT64_MAX);
+	STATIC_REQUIRE(bx::LimitsT<int64_t>::min == INT64_MIN);
+	STATIC_REQUIRE(bx::LimitsT<int64_t>::max == INT64_MAX);
 
-	STATIC_REQUIRE(bx::Limits<uint64_t>::min == 0);
-	STATIC_REQUIRE(bx::Limits<uint64_t>::max == UINT64_MAX);
+	STATIC_REQUIRE(bx::LimitsT<uint64_t>::min == 0);
+	STATIC_REQUIRE(bx::LimitsT<uint64_t>::max == UINT64_MAX);
 
-	STATIC_REQUIRE(bx::Limits<float>::min == std::numeric_limits<float>::lowest() );
-	STATIC_REQUIRE(bx::Limits<float>::max == std::numeric_limits<float>::max() );
+	STATIC_REQUIRE(bx::LimitsT<float>::min == std::numeric_limits<float>::lowest() );
+	STATIC_REQUIRE(bx::LimitsT<float>::max == std::numeric_limits<float>::max() );
 
-	STATIC_REQUIRE(bx::Limits<double>::min == std::numeric_limits<double>::lowest() );
-	STATIC_REQUIRE(bx::Limits<double>::max == std::numeric_limits<double>::max() );
+	STATIC_REQUIRE(bx::LimitsT<double>::min == std::numeric_limits<double>::lowest() );
+	STATIC_REQUIRE(bx::LimitsT<double>::max == std::numeric_limits<double>::max() );
 }