Browse Source

Refactoring of String

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
0b3f31a98e
3 changed files with 102 additions and 100 deletions
  1. 37 66
      include/anki/util/String.h
  2. 1 1
      src/util/CMakeLists.txt
  3. 64 33
      src/util/String.cpp

+ 37 - 66
include/anki/util/String.h

@@ -9,9 +9,6 @@
 #include "anki/util/Array.h"
 #include "anki/util/Array.h"
 #include "anki/util/NonCopyable.h"
 #include "anki/util/NonCopyable.h"
 #include <cstring>
 #include <cstring>
-#include <cmath> // For HUGE_VAL
-#include <climits> // For LLONG_MAX
-#include <cstdarg> // For var args
 
 
 namespace anki {
 namespace anki {
 
 
@@ -206,49 +203,13 @@ public:
 	}
 	}
 
 
 	/// Convert to F64.
 	/// Convert to F64.
-	ANKI_USE_RESULT Error toF64(F64& out) const
-	{
-		checkInit();
-		Error err = ErrorCode::NONE;
-		out = std::strtod(m_ptr, nullptr);
-
-		if(out == HUGE_VAL)
-		{
-			ANKI_LOGE("Conversion failed");
-			err = ErrorCode::USER_DATA;
-		}
-
-		return err;
-	}
+	ANKI_USE_RESULT Error toF64(F64& out) const;
 
 
 	/// Convert to F32.
 	/// Convert to F32.
-	ANKI_USE_RESULT Error toF32(F32& out) const
-	{
-		F64 d;
-		Error err = toF64(d);
-		if(!err)
-		{
-			out = d;
-		}
-
-		return err;
-	}
+	ANKI_USE_RESULT Error toF32(F32& out) const;
 
 
 	/// Convert to I64.
 	/// Convert to I64.
-	ANKI_USE_RESULT Error toI64(I64& out) const
-	{
-		checkInit();
-		Error err = ErrorCode::NONE;
-		out = std::strtoll(m_ptr, nullptr, 10);
-
-		if(out == LLONG_MAX || out == LLONG_MIN)
-		{
-			ANKI_LOGE("Conversion failed");
-			err = ErrorCode::USER_DATA;
-		}
-
-		return err;
-	}
+	ANKI_USE_RESULT Error toI64(I64& out) const;
 
 
 private:
 private:
 	const Char* m_ptr = nullptr;
 	const Char* m_ptr = nullptr;
@@ -267,6 +228,7 @@ public:
 	using CStringType = CString;
 	using CStringType = CString;
 	using Iterator = Char*;
 	using Iterator = Char*;
 	using ConstIterator = const Char*;
 	using ConstIterator = const Char*;
+	using Allocator = GenericMemoryPoolAllocator<Char>;
 
 
 	static const PtrSize NPOS = MAX_PTR_SIZE;
 	static const PtrSize NPOS = MAX_PTR_SIZE;
 
 
@@ -285,28 +247,23 @@ public:
 	{}
 	{}
 
 
 	/// Initialize using a const string.
 	/// Initialize using a const string.
-	template<typename TAllocator>
-	void create(TAllocator alloc, const CStringType& cstr);
+	void create(Allocator alloc, const CStringType& cstr);
 
 
 	/// Initialize using a range. Copies the range of [first, last)
 	/// Initialize using a range. Copies the range of [first, last)
-	template<typename TAllocator>
-	void create(TAllocator alloc,
+	void create(Allocator alloc,
 		ConstIterator first, ConstIterator last);
 		ConstIterator first, ConstIterator last);
 
 
 	/// Initialize using a character.
 	/// Initialize using a character.
-	template<typename TAllocator>
-	void create(TAllocator alloc, Char c, PtrSize length);
+	void create(Allocator alloc, Char c, PtrSize length);
 
 
 	/// Copy one string to this one.
 	/// Copy one string to this one.
-	template<typename TAllocator>
-	void create(TAllocator alloc, const String& b)
+	void create(Allocator alloc, const String& b)
 	{
 	{
 		create(alloc, b.toCString());
 		create(alloc, b.toCString());
 	}
 	}
 
 
 	/// Destroy the string.
 	/// Destroy the string.
-	template<typename TAllocator>
-	void destroy(TAllocator alloc)
+	void destroy(Allocator alloc)
 	{
 	{
 		m_data.destroy(alloc);
 		m_data.destroy(alloc);
 	}
 	}
@@ -460,8 +417,7 @@ public:
 	}
 	}
 
 
 	/// Append another string to this one.
 	/// Append another string to this one.
-	template<typename TAllocator>
-	void append(TAllocator alloc, const String& b)
+	void append(Allocator alloc, const String& b)
 	{
 	{
 		if(!b.isEmpty())
 		if(!b.isEmpty())
 		{
 		{
@@ -470,8 +426,7 @@ public:
 	}
 	}
 
 
 	/// Append a const string to this one.
 	/// Append a const string to this one.
-	template<typename TAllocator>
-	void append(TAllocator alloc, const CStringType& cstr)
+	void append(Allocator alloc, const CStringType& cstr)
 	{
 	{
 		if(!cstr.isEmpty())
 		if(!cstr.isEmpty())
 		{
 		{
@@ -480,8 +435,7 @@ public:
 	}
 	}
 
 
 	/// Create formated string.
 	/// Create formated string.
-	template<typename TAllocator>
-	void sprintf(TAllocator alloc, CString fmt, ...);
+	void sprintf(Allocator alloc, CString fmt, ...);
 
 
 	/// Return true if it's empty.
 	/// Return true if it's empty.
 	Bool isEmpty() const
 	Bool isEmpty() const
@@ -512,8 +466,8 @@ public:
 	}
 	}
 
 
 	/// Convert a number to a string.
 	/// Convert a number to a string.
-	template<typename TAllocator, typename TNumber>
-	void toString(TAllocator alloc, TNumber number);
+	template<typename TNumber>
+	void toString(Allocator alloc, TNumber number);
 
 
 	/// Convert to F64.
 	/// Convert to F64.
 	ANKI_USE_RESULT Error toF64(F64& out) const
 	ANKI_USE_RESULT Error toF64(F64& out) const
@@ -536,8 +490,7 @@ protected:
 	}
 	}
 
 
 	/// Append to this string.
 	/// Append to this string.
-	template<typename TAllocator>
-	void appendInternal(TAllocator alloc, const Char* str, PtrSize strSize);
+	void appendInternal(Allocator alloc, const Char* str, PtrSize strSize);
 
 
 	void move(String& b)
 	void move(String& b)
 	{
 	{
@@ -546,15 +499,35 @@ protected:
 	}
 	}
 };
 };
 
 
+//==============================================================================
+template<typename TNumber>
+inline void String::toString(Allocator alloc, TNumber number)
+{
+	destroy(alloc);
+
+	Array<Char, 512> buff;
+	I ret = std::snprintf(
+		&buff[0], buff.size(), detail::toStringFormat<TNumber>(), number);
+
+	if(ret < 0 || ret > static_cast<I>(buff.getSize()))
+	{
+		ANKI_LOGF("To small intermediate buffer");
+	}
+	else
+	{
+		create(alloc, &buff[0]);
+	}
+}
+
 /// String with automatic cleanup.
 /// String with automatic cleanup.
 class StringAuto: public String
 class StringAuto: public String
 {
 {
 public:
 public:
 	using Base = String;
 	using Base = String;
+	using Allocator = typename Base::Allocator;
 
 
 	/// Create with allocator.
 	/// Create with allocator.
-	template<typename TAllocator>
-	StringAuto(TAllocator alloc)
+	StringAuto(Allocator alloc)
 		: Base()
 		: Base()
 		, m_alloc(alloc)
 		, m_alloc(alloc)
 	{}
 	{}
@@ -642,5 +615,3 @@ private:
 
 
 } // end namespace anki
 } // end namespace anki
 
 
-#include "anki/util/String.inl.h"
-

+ 1 - 1
src/util/CMakeLists.txt

@@ -1,4 +1,4 @@
-set(ANKI_UTIL_SOURCES Assert.cpp Functions.cpp File.cpp Filesystem.cpp Memory.cpp System.cpp HighRezTimer.cpp Thread.cpp Hash.cpp Logger.cpp)
+set(ANKI_UTIL_SOURCES Assert.cpp Functions.cpp File.cpp Filesystem.cpp Memory.cpp System.cpp HighRezTimer.cpp Thread.cpp Hash.cpp Logger.cpp String.cpp)
 
 
 if(LINUX OR ANDROID OR MACOS)
 if(LINUX OR ANDROID OR MACOS)
 	set(ANKI_UTIL_SOURCES ${ANKI_UTIL_SOURCES} HighRezTimerPosix.cpp FilesystemPosix.cpp ThreadPosix.cpp)
 	set(ANKI_UTIL_SOURCES ${ANKI_UTIL_SOURCES} HighRezTimerPosix.cpp FilesystemPosix.cpp ThreadPosix.cpp)

+ 64 - 33
include/anki/util/String.inl.h → src/util/String.cpp

@@ -3,11 +3,68 @@
 // Code licensed under the BSD License.
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
+#include <anki/util/String.h>
+#include <cmath> // For HUGE_VAL
+#include <climits> // For LLONG_MAX
+#include <cstdarg> // For var args
+
 namespace anki {
 namespace anki {
 
 
 //==============================================================================
 //==============================================================================
-template<typename TAllocator>
-inline void String::create(TAllocator alloc, const CStringType& cstr)
+// CString                                                                     =
+//==============================================================================
+
+//==============================================================================
+Error CString::toF64(F64& out) const
+{
+	checkInit();
+	Error err = ErrorCode::NONE;
+	out = std::strtod(m_ptr, nullptr);
+
+	if(out == HUGE_VAL)
+	{
+		ANKI_LOGE("Conversion failed");
+		err = ErrorCode::USER_DATA;
+	}
+
+	return err;
+}
+
+//==============================================================================
+Error CString::toF32(F32& out) const
+{
+	F64 d;
+	Error err = toF64(d);
+	if(!err)
+	{
+		out = d;
+	}
+
+	return err;
+}
+
+//==============================================================================
+Error CString::toI64(I64& out) const
+{
+	checkInit();
+	Error err = ErrorCode::NONE;
+	out = std::strtoll(m_ptr, nullptr, 10);
+
+	if(out == LLONG_MAX || out == LLONG_MIN)
+	{
+		ANKI_LOGE("Conversion failed");
+		err = ErrorCode::USER_DATA;
+	}
+
+	return err;
+}
+
+//==============================================================================
+// String                                                                      =
+//==============================================================================
+
+//==============================================================================
+void String::create(Allocator alloc, const CStringType& cstr)
 {
 {
 	auto len = cstr.getLength();
 	auto len = cstr.getLength();
 	if(len > 0)
 	if(len > 0)
@@ -19,9 +76,7 @@ inline void String::create(TAllocator alloc, const CStringType& cstr)
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-template<typename TAllocator>
-inline void String::create(
-	TAllocator alloc, ConstIterator first, ConstIterator last)
+void String::create(Allocator alloc, ConstIterator first, ConstIterator last)
 {
 {
 	ANKI_ASSERT(first != 0 && last != 0);
 	ANKI_ASSERT(first != 0 && last != 0);
 	auto length = last - first;
 	auto length = last - first;
@@ -32,8 +87,7 @@ inline void String::create(
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-template<typename TAllocator>
-inline void String::create(TAllocator alloc, Char c, PtrSize length)
+void String::create(Allocator alloc, Char c, PtrSize length)
 {
 {
 	ANKI_ASSERT(c != '\0');
 	ANKI_ASSERT(c != '\0');
 	m_data.create(alloc, length + 1);
 	m_data.create(alloc, length + 1);
@@ -43,9 +97,7 @@ inline void String::create(TAllocator alloc, Char c, PtrSize length)
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-template<typename TAllocator>
-inline void String::appendInternal(
-	TAllocator alloc, const Char* str, PtrSize strSize)
+void String::appendInternal(Allocator alloc, const Char* str, PtrSize strSize)
 {
 {
 	ANKI_ASSERT(str != nullptr);
 	ANKI_ASSERT(str != nullptr);
 	ANKI_ASSERT(strSize > 1);
 	ANKI_ASSERT(strSize > 1);
@@ -58,7 +110,7 @@ inline void String::appendInternal(
 		size = 1;
 		size = 1;
 	}
 	}
 
 
-	DArray<Char> newData;	
+	DArray<Char> newData;
 	newData.create(alloc, size + strSize - 1);
 	newData.create(alloc, size + strSize - 1);
 
 
 	if(!m_data.isEmpty())
 	if(!m_data.isEmpty())
@@ -73,8 +125,7 @@ inline void String::appendInternal(
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-template<typename TAllocator>
-inline void String::sprintf(TAllocator alloc, CString fmt, ...)
+void String::sprintf(Allocator alloc, CString fmt, ...)
 {
 {
 	Array<Char, 512> buffer;
 	Array<Char, 512> buffer;
 	va_list args;
 	va_list args;
@@ -106,25 +157,5 @@ inline void String::sprintf(TAllocator alloc, CString fmt, ...)
 	}
 	}
 }
 }
 
 
-//==============================================================================
-template<typename TAllocator, typename TNumber>
-inline void String::toString(TAllocator alloc, TNumber number)
-{
-	destroy(alloc);
-
-	Array<Char, 512> buff;
-	I ret = std::snprintf(
-		&buff[0], buff.size(), detail::toStringFormat<TNumber>(), number);
-
-	if(ret < 0 || ret > static_cast<I>(buff.getSize()))
-	{
-		ANKI_LOGF("To small intermediate buffer");
-	}
-	else
-	{
-		create(alloc, &buff[0]);
-	}
-}
-
 } // end namespace anki
 } // end namespace anki