Browse Source

Some improvements in some operator[]

Panagiotis Christopoulos Charitos 4 năm trước cách đây
mục cha
commit
32ecf9545b
2 tập tin đã thay đổi với 33 bổ sung10 xóa
  1. 26 6
      AnKi/Util/Array.h
  2. 7 4
      AnKi/Util/String.h

+ 26 - 6
AnKi/Util/Array.h

@@ -32,18 +32,38 @@ public:
 
 	Value m_data[N];
 
-	template<typename Y>
-	Reference operator[](const Y n)
+	/// Access an element using an integer.
+	template<typename TInt, ANKI_ENABLE(!std::is_enum<TInt>::value)>
+	Reference operator[](const TInt n)
 	{
+		ANKI_ASSERT(!(std::is_signed<TInt>::value && n < 0));
 		ANKI_ASSERT(PtrSize(n) < N);
-		return m_data[PtrSize(n)];
+		return m_data[n];
 	}
 
-	template<typename Y>
-	ConstReference operator[](const Y n) const
+	/// Access an element using an integer.
+	template<typename TInt, ANKI_ENABLE(!std::is_enum<TInt>::value)>
+	ConstReference operator[](const TInt n) const
 	{
+		ANKI_ASSERT(!(std::is_signed<TInt>::value && n < 0));
 		ANKI_ASSERT(PtrSize(n) < N);
-		return m_data[PtrSize(n)];
+		return m_data[n];
+	}
+
+	/// Access an element using an enumerant. It's a little bit special and separate from operator[] that accepts
+	/// integer. This to avoid any short of arbitrary integer type casting.
+	template<typename TEnum, ANKI_ENABLE(std::is_enum<TEnum>::value)>
+	Reference operator[](const TEnum n)
+	{
+		return operator[](typename std::underlying_type<TEnum>::type(n));
+	}
+
+	/// Access an element using an enumerant. It's a little bit special and separate from operator[] that accepts
+	/// integer. This to avoid any short of arbitrary integer type casting.
+	template<typename TEnum, ANKI_ENABLE(std::is_enum<TEnum>::value)>
+	ConstReference operator[](const TEnum n) const
+	{
+		return operator[](typename std::underlying_type<TEnum>::type(n));
 	}
 
 	Iterator getBegin()

+ 7 - 4
AnKi/Util/String.h

@@ -94,10 +94,11 @@ public:
 	}
 
 	/// Return char at the specified position.
-	const Char& operator[](U pos) const
+	template<typename T>
+	const Char& operator[](T pos) const
 	{
 		checkInit();
-		ANKI_ASSERT(pos <= getLength());
+		ANKI_ASSERT(pos >= 0 && U32(pos) <= getLength());
 		return m_ptr[pos];
 	}
 
@@ -330,14 +331,16 @@ public:
 	String& operator=(StringAuto&& b);
 
 	/// Return char at the specified position.
-	const Char& operator[](U pos) const
+	template<typename TInt>
+	const Char& operator[](TInt pos) const
 	{
 		checkInit();
 		return m_data[pos];
 	}
 
 	/// Return char at the specified position as a modifiable reference.
-	Char& operator[](U pos)
+	template<typename TInt>
+	Char& operator[](TInt pos)
 	{
 		checkInit();
 		return m_data[pos];