Browse Source

Refactoring

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
bc7d07d77b

+ 24 - 25
include/anki/Util.h

@@ -3,31 +3,6 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
-#ifndef ANKI_UTIL_H
-#define ANKI_UTIL_H
-
-#include "anki/util/Allocator.h"
-#include "anki/util/Array.h"
-#include "anki/util/Assert.h"
-#include "anki/util/Thread.h"
-#include "anki/util/Bitset.h"
-#include "anki/util/Dictionary.h"
-#include "anki/util/Exception.h"
-#include "anki/util/File.h"
-#include "anki/util/Functions.h"
-#include "anki/util/HighRezTimer.h"
-#include "anki/util/LinuxMalinfo.h"
-#include "anki/util/Memory.h"
-#include "anki/util/NonCopyable.h"
-#include "anki/util/Object.h"
-#include "anki/util/Singleton.h"
-#include "anki/util/StdTypes.h"
-#include "anki/util/StringList.h"
-#include "anki/util/System.h"
-//#include "anki/util/Variant.h"
-#include "anki/util/Vector.h"
-#include "anki/util/Visitor.h"
-
 /// @defgroup util Utilities (like STL)
 /// @ingroup anki_public
 
@@ -61,4 +36,28 @@
 /// @defgroup util_private Private interfaces
 /// @ingroup util
 
+#ifndef ANKI_UTIL_H
+#define ANKI_UTIL_H
+
+#include "anki/util/Allocator.h"
+#include "anki/util/Array.h"
+#include "anki/util/Assert.h"
+#include "anki/util/Thread.h"
+#include "anki/util/Bitset.h"
+#include "anki/util/Dictionary.h"
+#include "anki/util/Exception.h"
+#include "anki/util/File.h"
+#include "anki/util/Functions.h"
+#include "anki/util/HighRezTimer.h"
+#include "anki/util/LinuxMalinfo.h"
+#include "anki/util/Memory.h"
+#include "anki/util/NonCopyable.h"
+#include "anki/util/Object.h"
+#include "anki/util/Singleton.h"
+#include "anki/util/StdTypes.h"
+#include "anki/util/StringList.h"
+#include "anki/util/System.h"
+#include "anki/util/Vector.h"
+#include "anki/util/Visitor.h"
+
 #endif

+ 15 - 15
include/anki/util/StdTypes.h

@@ -16,60 +16,60 @@ namespace anki {
 /// @addtogroup util_other
 /// @{
 
-typedef int8_t I8; ///< Integer 8bit
+using I8 = int8_t; ///< Integer 8bit
 const I8 MAX_I8 = std::numeric_limits<I8>::max();
 const I8 MIN_I8 = std::numeric_limits<I8>::min();
 
-typedef int16_t I16; ///< Integer 16bit
+using I16 = int16_t; ///< Integer 16bit
 const I16 MAX_I16 = std::numeric_limits<I16>::max();
 const I16 MIN_I16 = std::numeric_limits<I16>::min();
 
-typedef int32_t I32; ///< Integer 32bit
+using I32 = int32_t; ///< Integer 32bit
 const I32 MAX_I32 = std::numeric_limits<I32>::max();
 const I32 MIN_I32 = std::numeric_limits<I32>::min();
 
-typedef int64_t I64; ///< Integer 64bit
+using I64 = int64_t; ///< Integer 64bit
 const I64 MAX_I64 = std::numeric_limits<I64>::max();
 const I64 MIN_I64 = std::numeric_limits<I64>::min();
 
-typedef int_fast32_t I; ///< Fast signed integer at least 32bit
+using I = int_fast32_t; ///< Fast signed integer at least 32bit
 const I MAX_I = std::numeric_limits<I>::max();
 const I MIN_I = std::numeric_limits<I>::min();
 
-typedef uint8_t U8; ///< Unsigned integer 8bit
+using U8 = uint8_t; ///< Unsigned integer 8bit
 const U8 MAX_U8 = std::numeric_limits<U8>::max();
 const U8 MIN_U8 = std::numeric_limits<U8>::min();
 
-typedef uint16_t U16; ///< Unsigned integer 16bit
+using U16 = uint16_t; ///< Unsigned integer 16bit
 const U16 MAX_U16 = std::numeric_limits<U16>::max();
 const U16 MIN_U16 = std::numeric_limits<U16>::min();
 
-typedef uint32_t U32; ///< Unsigned integer 32bit
+using U32 = uint32_t; ///< Unsigned integer 32bit
 const U32 MAX_U32 = std::numeric_limits<U32>::max();
 const U32 MIN_U32 = std::numeric_limits<U32>::min();
 
-typedef uint64_t U64; ///< Unsigned integer 64bit
+using U64 = uint64_t; ///< Unsigned integer 64bit
 const U64 MAX_U64 = std::numeric_limits<U64>::max();
 const U64 MIN_U64 = std::numeric_limits<U64>::min();
 
-typedef uint_fast32_t U; ///< Fast unsigned integer at least 32bit
+using U = uint_fast32_t; ///< Fast unsigned integer at least 32bit
 const U MAX_U = std::numeric_limits<U>::max();
 const U MIN_U = std::numeric_limits<U>::min();
 
-typedef size_t PtrSize; ///< Like size_t
+using PtrSize = size_t; ///< Like size_t
 const PtrSize MAX_PTR_SIZE = std::numeric_limits<PtrSize>::max();
 const PtrSize MIN_PTR_SIZE = std::numeric_limits<PtrSize>::min();
 
-typedef float F32; ///< Floating point 32bit
+using F32 = float; ///< Floating point 32bit
 const F32 MAX_F32 = std::numeric_limits<F32>::max();
 const F32 MIN_F32 = -std::numeric_limits<F32>::max();
 
-typedef double F64; ///< Floating point 64bit
+using F64 = double; ///< Floating point 64bit
 const F64 MAX_F64 = std::numeric_limits<F64>::max();
 const F64 MIN_F64 = -std::numeric_limits<F64>::max();
 
-typedef bool Bool; ///< Fast boolean type
-typedef U8 Bool8; ///< Small 8bit boolean type
+using Bool = bool; ///< Fast boolean type
+using Bool8 = U8; ///< Small 8bit boolean type
 
 /// @}
 

+ 2 - 2
include/anki/util/String.h

@@ -93,9 +93,9 @@ inline void toString(TNumber number, TString& out)
 {
 	Array<typename TString::value_type, 512> buff;
 	I ret = std::snprintf(
-		&buff, buff.size(), detail::toStringFormat<TNumber>(), number);
+		&buff[0], buff.size(), detail::toStringFormat<TNumber>(), number);
 
-	if(ret < 0 || ret > buff.size())
+	if(ret < 0 || ret > static_cast<I>(buff.size()))
 	{
 		throw ANKI_EXCEPTION("To small intermediate buffer");
 	}

+ 12 - 67
include/anki/util/StringList.h

@@ -9,7 +9,6 @@
 #include "anki/util/Vector.h"
 #include "anki/util/StdTypes.h"
 #include "anki/util/String.h"
-#include <sstream>
 #include <algorithm>
 
 namespace anki {
@@ -30,53 +29,22 @@ class BasicStringList: public Vector<BasicString<TChar, TAlloc>, TAlloc>
 {
 public:
 	using Self = BasicStringList; ///< Self type
+	using Char = TChar; ///< Char type
 	using Allocator = TAlloc;
-	/// Its the vector of strings
-	using Base = Vector<BasicString<TChar, Allocator>, Allocator>;
-	using String = typename Base::value_type; ///< Its string
-	using Char = typename String::value_type; ///< Char type
+	using String = BasicString<Char, Allocator>; ///< String type
+	using Base = Vector<String, Allocator>; ///< Base
 
 	// Use the base constructors
 	using Base::Base;
 
 	/// Join all the elements into a single big string using a the
 	/// seperator @a separator
-	String join(const Char* separator) const
-	{
-		Allocator alloc = Base::get_allocator();
-		String out(alloc);
-
-		typename Base::const_iterator it = Base::begin();
-		for(; it != Base::end(); it++)
-		{
-			out += *it;
-			if(it != Base::end() - 1)
-			{
-				out += separator;
-			}
-		}
-
-		return out;
-	}
+	String join(const Char* separator) const;
 
 	/// Returns the index position of the last occurrence of @a value in
 	/// the list
 	/// @return -1 of not found
-	I getIndexOf(const Char* value) const
-	{
-		U32 pos = 0;
-
-		for(auto it = Base::begin(); it != Base::end(); ++it)
-		{
-			if(*it == value)
-			{
-				break;
-			}
-			++ pos;
-		}
-
-		return (pos == Base::size()) ? -1 : pos;
-	}
+	I getIndexOf(const Char* value) const;
 
 	/// Sort the string list
 	void sortAll(const StringListSort method = StringListSort::ASCENDING)
@@ -92,36 +60,10 @@ public:
 		}
 	}
 
-	/// Split a string using a list of separators (@a sep) and return these
+	/// Split a string using a separator (@a separator) and return these
 	/// strings in a string list
-	static Self splitString(const Char* s, const Char separator = ' ',
-		Bool keepEmpties = false, Allocator alloc = Allocator())
-	{
-		Self out(alloc);
-		std::basic_istringstream<Char, std::char_traits<Char>, Allocator> 
-			ss(s, alloc);
-
-		while(!ss.eof())
-		{
-			String field;
-			getline(ss, field, separator);
-			if(!keepEmpties && field.empty())
-			{
-				continue;
-			}
-			out.push_back(field);
-		}
-
-		return out;
-	}
-
-	/// Mainly in the unit tests
-	friend std::ostream& operator<<(std::ostream& s, const Self& a)
-	{
-		auto alloc = a.get_allocator();
-		s << a.join(", ", a);
-		return s;
-	}
+	static Self splitString(const Char* s, const Char separator,
+		Allocator alloc = Allocator());
 
 private:
 	static Bool compareStringsAsc(const String& a, const String& b)
@@ -136,9 +78,12 @@ private:
 };
 
 /// A common string list allocated in heap
-typedef BasicStringList<char, std::allocator<char>> StringList;
+using StringList = BasicStringList<char, HeapAllocator<char>>;
+
 /// @}
 
 } // end namespace anki
 
+#include "anki/util/StringList.inl.h"
+
 #endif

+ 106 - 0
include/anki/util/StringList.inl.h

@@ -0,0 +1,106 @@
+// Copyright (C) 2014, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include "anki/util/StringList.h"
+#include <cstring>
+
+namespace anki {
+
+//==============================================================================
+template<typename TChar, typename TAlloc>
+typename BasicStringList<TChar, TAlloc>::String 
+	BasicStringList<TChar, TAlloc>::join(const Char* separator) const
+{
+	// Count the characters
+	I sepLen = std::strlen(separator);
+	I charCount = 0;
+	for(const String& str : *this)
+	{
+		charCount += str.length() + sepLen;
+	}
+
+	charCount -= sepLen; // Remove last separator
+	ANKI_ASSERT(charCount > 0);
+
+	Allocator alloc = Base::get_allocator();
+	String out(alloc);
+	out.reserve(charCount + 1);
+
+	typename Base::const_iterator it = Base::begin();
+	for(; it != Base::end(); it++)
+	{
+		out += *it;
+		if(it != Base::end() - 1)
+		{
+			out += separator;
+		}
+	}
+
+	return out;
+}
+
+//==============================================================================
+template<typename TChar, typename TAlloc>
+I BasicStringList<TChar, TAlloc>::getIndexOf(const Char* value) const
+{
+	U pos = 0;
+
+	for(auto it = Base::begin(); it != Base::end(); ++it)
+	{
+		if(*it == value)
+		{
+			break;
+		}
+		++ pos;
+	}
+
+	return (pos == Base::size()) ? -1 : pos;
+}
+
+//==============================================================================
+template<typename TChar, typename TAlloc>
+BasicStringList<TChar, TAlloc> 
+	BasicStringList<TChar, TAlloc>::splitString(
+	const Char* s, 
+	const Char separator,
+	Allocator alloc)
+{
+	ANKI_ASSERT(s != nullptr);
+
+	Self out(alloc);
+	const Char* begin = s;
+	const Char* end = begin;
+
+	while(true)
+	{
+		if(*end == '\0')
+		{
+			if(begin < end)
+			{
+				out.push_back(String(begin, end, alloc));
+			}
+
+			break;
+		}
+		else if(*end == separator)
+		{
+			if(begin < end)
+			{
+				out.push_back(String(begin, end, alloc));
+				begin = end + 1;
+			}
+			else
+			{
+				++begin;
+			}
+		}
+
+		++end;
+	}
+
+	return out;
+}
+
+} // end namespace anki

+ 2 - 1
src/resource/MaterialProgramCreator.cpp

@@ -248,7 +248,7 @@ void MaterialProgramCreator::parseInputsTag(const XmlElement& programEl)
 		if(valueEl.getText())
 		{
 			inpvar.m_value = MPStringList::splitString(
-				valueEl.getText(), ' ', false, m_alloc);
+				valueEl.getText(), ' ', m_alloc);
 		}
 
 		// <const>
@@ -486,6 +486,7 @@ void MaterialProgramCreator::parseOperationTag(
 
 	// Now write everything
 	MPString lines(m_alloc);
+	lines.reserve(256);
 	lines += "#if defined(" + funcName + "_DEFINED)";
 
 	// Write the defines for the operationOuts