Browse Source

Removing Exceptions

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
15b7bac3be

+ 4 - 3
include/anki/core/Counters.h

@@ -5,6 +5,7 @@
 
 #include "anki/util/StdTypes.h"
 #include "anki/util/Singleton.h"
+#include "anki/util/DArray.h"
 #include "anki/util/File.h"
 #include "anki/util/HighRezTimer.h"
 #include "anki/util/Atomic.h"
@@ -61,9 +62,9 @@ public:
 private:
 	File m_perframeFile;
 	File m_perrunFile;
-	Vector<U64> m_perframeValues;
-	Vector<U64> m_perrunValues;
-	Vector<HighRezTimer::Scalar> m_counterTimes;
+	DArray<U64> m_perframeValues;
+	DArray<U64> m_perrunValues;
+	DArray<HighRezTimer::Scalar> m_counterTimes;
 };
 
 /// The singleton of the counters manager

+ 2 - 0
include/anki/gl/GlProgram.h

@@ -90,6 +90,8 @@ private:
 		char*& namesPtr, U& namesLen);
 
 	ANKI_USE_RESULT Error populateVariablesAndBlock(GlAllocator<U8>& alloc);
+
+	ANKI_USE_RESULT Error handleError(GlAllocator<U8>& alloc, String& src);
 };
 
 /// @}

+ 1 - 1
include/anki/gl/GlProgramPipeline.h

@@ -29,7 +29,7 @@ public:
 
 	ANKI_USE_RESULT Error create(
 		const GlProgramHandle* progsBegin, const GlProgramHandle* progsEnd,
-		GlAllocator<U8>& alloc);
+		GlAllocator<U8> alloc);
 
 	GlProgramHandle getAttachedProgram(GLenum type) const;
 

+ 5 - 0
include/anki/util/DArray.h

@@ -120,6 +120,11 @@ public:
 		return m_size;
 	}
 
+	Bool isEmpty() const
+	{
+		return m_size == 0;
+	}
+
 	PtrSize getSizeInBytes() const
 	{
 		return m_size * sizeof(Value);

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

@@ -496,7 +496,7 @@ public:
 	/// Return true if it's empty.
 	Bool isEmpty() const 
 	{
-		return m_data.empty();
+		return m_data.isEmpty();
 	}
 
 	/// Find a substring of this string.

+ 9 - 29
include/anki/util/StringList.h

@@ -7,6 +7,7 @@
 #define ANKI_UTIL_STRING_LIST_H
 
 #include "anki/util/String.h"
+#include "anki/util/List.h"
 #include <algorithm>
 
 namespace anki {
@@ -16,14 +17,14 @@ namespace anki {
 
 /// A simple convenience class for string lists
 template<typename TAlloc>
-class StringListBase: public Vector<StringBase<TAlloc>, TAlloc>
+class StringListBase: public List<StringBase<TAlloc>, TAlloc>
 {
 public:
 	using Self = StringListBase; ///< Self type
 	using Char = char; ///< Char type
 	using Allocator = TAlloc;
-	using String = StringBase<TAlloc>; ///< String type
-	using Base = Vector<String, Allocator>; ///< Base
+	using String = StringBase<Allocator>; ///< String type
+	using Base = List<String, Allocator>; ///< Base
 
 	/// Sort method for sortAll().
 	enum class Sort
@@ -37,7 +38,8 @@ public:
 
 	/// Join all the elements into a single big string using a the
 	/// seperator @a separator
-	String join(const CString& separator) const;
+	ANKI_USE_RESULT Error join(
+		Allocator alloc, const CString& separator, String& out) const;
 
 	/// Returns the index position of the last occurrence of @a value in
 	/// the list
@@ -45,34 +47,12 @@ public:
 	I getIndexOf(const CString& value) const;
 
 	/// Sort the string list
-	void sortAll(const Sort method = Sort::ASCENDING)
-	{
-		if(method == Sort::ASCENDING)
-		{
-			std::sort(Base::begin(), Base::end(), compareStringsAsc);
-		}
-		else
-		{
-			ANKI_ASSERT(method == Sort::DESCENDING);
-			std::sort(Base::begin(), Base::end(), compareStringsDesc);
-		}
-	}
+	void sortAll(const Sort method = Sort::ASCENDING);
 
 	/// Split a string using a separator (@a separator) and return these
 	/// strings in a string list
-	static Self splitString(const CString& s, const Char separator,
-		Allocator alloc);
-
-private:
-	static Bool compareStringsAsc(const String& a, const String& b)
-	{
-		return a < b;
-	}
-
-	static Bool compareStringsDesc(const String& a,	const String& b)
-	{
-		return a > b;
-	}
+	static ANKI_USE_RESULT Error splitString(
+		Allocator alloc, const CString& s, const Char separator, Self& out);
 };
 
 /// A common string list allocated in heap.

+ 79 - 23
include/anki/util/StringList.inl.h

@@ -10,16 +10,18 @@ namespace anki {
 
 //==============================================================================
 template<typename TAlloc>
-typename StringListBase<TAlloc>::String 
-	StringListBase<TAlloc>::join(const CString& separator) const
+Error StringListBase<TAlloc>::join(
+	Allocator alloc,
+	const CString& separator,
+	String& out) const
 {
-	if(Base::size() == 0)
+	if(Base::isEmpty())
 	{
-		return String(Base::get_allocator());
+		return ErrorCode::NONE;
 	}
 
 	// Count the characters
-	I sepLen = separator.getLength();
+	const I sepLen = separator.getLength();
 	I charCount = 0;
 	for(const String& str : *this)
 	{
@@ -29,21 +31,30 @@ typename StringListBase<TAlloc>::String
 	charCount -= sepLen; // Remove last separator
 	ANKI_ASSERT(charCount > 0);
 
-	Allocator alloc = Base::get_allocator();
-	String out(alloc);
-	out.reserve(charCount + 1);
+	// Allocate
+	Error err = out.create(alloc, '?', charCount + 1);
+	if(err)
+	{
+		return err;
+	}
 
-	typename Base::const_iterator it = Base::begin();
-	for(; it != Base::end(); it++)
+	// Append to output
+	Char* to = &out[0];
+	typename Base::ConstIterator it = Base::getBegin();
+	for(; it != Base::getEnd(); it++)
 	{
-		out += *it;
+		const String& from = *it;
+		std::memcpy(out, &from[0], from.getLength() * sizeof(Char));
+		to += from.getLength();
+
 		if(it != Base::end() - 1)
 		{
-			out += separator;
+			std::memcpy(to, &separator[0], sepLen * sizeof(Char));
+			to += sepLen;
 		}
 	}
 
-	return out;
+	return err;
 }
 
 //==============================================================================
@@ -52,7 +63,7 @@ I StringListBase<TAlloc>::getIndexOf(const CString& value) const
 {
 	U pos = 0;
 
-	for(auto it = Base::begin(); it != Base::end(); ++it)
+	for(auto it = Base::getBegin(); it != Base::getEnd(); ++it)
 	{
 		if(*it == value)
 		{
@@ -66,23 +77,36 @@ I StringListBase<TAlloc>::getIndexOf(const CString& value) const
 
 //==============================================================================
 template<typename TAlloc>
-StringListBase<TAlloc> 
-	StringListBase<TAlloc>::splitString(
+Error StringListBase<TAlloc>::splitString(
+	Allocator alloc,
 	const CString& s, 
 	const Char separator,
-	Allocator alloc)
+	Self& out)
 {
-	Self out(alloc);
+	ANKI_ASSERT(out.isEmpty());
+
+	Error err = ErrorCode::NONE;
 	const Char* begin = &s[0];
 	const Char* end = begin;
 
-	while(true)
+	while(!err)
 	{
 		if(*end == '\0')
 		{
 			if(begin < end)
 			{
-				out.emplace_back(begin, end, alloc);
+				err = out.emplaceBack(alloc);
+
+				String str;
+				if(!err)
+				{
+					err = str.create(alloc, begin, end);
+				}
+
+				if(!err)
+				{
+					out.getBack() = std::move(str);
+				}
 			}
 
 			break;
@@ -91,8 +115,19 @@ StringListBase<TAlloc>
 		{
 			if(begin < end)
 			{
-				out.emplace_back(begin, end, alloc);
-				begin = end + 1;
+				err = out.emplaceBack(alloc);
+
+				String str;
+				if(!err)
+				{
+					err = str.create(alloc, begin, end);
+				}
+
+				if(!err)
+				{
+					out.getBack() = std::move(str);
+					begin = end + 1;
+				}
 			}
 			else
 			{
@@ -103,7 +138,28 @@ StringListBase<TAlloc>
 		++end;
 	}
 
-	return out;
+	return err;
+}
+
+//==============================================================================
+template<typename TAlloc>
+void StringListBase<TAlloc>::sortAll(const Sort method)
+{
+	if(method == Sort::ASCENDING)
+	{
+		Base::sort([](const String& a, const String& b)
+		{
+			return a < b;
+		});
+	}
+	else
+	{
+		ANKI_ASSERT(method == Sort::DESCENDING);
+		Base::sort([](const String& a, const String& b)
+		{
+			return a < b;
+		});
+	}
 }
 
 } // end namespace anki

+ 75 - 51
src/gl/GlProgram.cpp

@@ -290,24 +290,28 @@ Error GlProgram::create(GLenum type, const CString& source,
 		version = major * 100 + minor * 10;
 	}
 
-	String fullSrc(alloc);
+	String fullSrc;
 #if ANKI_GL == ANKI_GL_DESKTOP
-	fullSrc.sprintf("#version %d core\n%s\n", version, &source[0]); 
+	err = fullSrc.sprintf(alloc, "#version %d core\n%s\n", version, &source[0]); 
 #else
-	fullSrc.sprintf("#version %d es\n%s\n", version, &source[0]);
+	err = fullSrc.sprintf(alloc, "#version %d es\n%s\n", version, &source[0]);
 #endif
 
 	// 2) Gen name, create, compile and link
 	//
-	const char* sourceStrs[1] = {nullptr};
-	sourceStrs[0] = &fullSrc[0];
-	m_glName = glCreateShaderProgramv(m_type, 1, sourceStrs);
-	if(m_glName == 0)
+	if(!err)
 	{
-		return ErrorCode::FUNCTION_FAILED;
+		const char* sourceStrs[1] = {nullptr};
+		sourceStrs[0] = &fullSrc[0];
+		m_glName = glCreateShaderProgramv(m_type, 1, sourceStrs);
+		if(m_glName == 0)
+		{
+			err = ErrorCode::FUNCTION_FAILED;
+		}
 	}
 
 #if ANKI_DUMP_SHADERS
+	if(!err)
 	{
 		const char* ext;
 
@@ -336,68 +340,88 @@ Error GlProgram::create(GLenum type, const CString& source,
 			ANKI_ASSERT(0);
 		}
 
-		String fname(alloc);
-		fname.sprintf(
+		String fname;
+		err = fname.sprintf(alloc,
 			"%s/%05u.%s", &cacheDir[0], static_cast<U32>(m_glName), ext);
 
-		File file;
-		err = file.open(fname.toCString(), File::OpenFlag::WRITE);
-		
 		if(!err)
 		{
-			err = file.writeText("%s", &fullSrc[0]);
+			File file;
+			err = file.open(fname.toCString(), File::OpenFlag::WRITE);
 		}
 
-		if(err)
-		{
-			ANKI_LOGW("Failed to open file %s", &fname[0]);
-		}
+		fname.destroy(alloc);
 	}
 #endif
 	
-	GLint status = GL_FALSE;
-	glGetProgramiv(m_glName, GL_LINK_STATUS, &status);
+	if(!err)
+	{
+		GLint status = GL_FALSE;
+		glGetProgramiv(m_glName, GL_LINK_STATUS, &status);
+
+		if(status == GL_FALSE)
+		{
+			err = handleError(alloc, fullSrc);
+		}
+	}
 
-	if(status == GL_FALSE)
+	// 3) Populate with vars and blocks
+	//
+	if(!err)
 	{
-		GLint infoLen = 0;
-		GLint charsWritten = 0;
-		String infoLog(alloc);
+		err = populateVariablesAndBlock(alloc);
+	}
 
-		static const char* padding = 
-			"======================================="
-			"=======================================";
+	fullSrc.destroy(alloc);
 
-		glGetProgramiv(m_glName, GL_INFO_LOG_LENGTH, &infoLen);
+	return err;
+}
 
-		infoLog.resize(infoLen + 1);
-		glGetProgramInfoLog(m_glName, infoLen, &charsWritten, &infoLog[0]);
-		
-		String errstr(alloc);
-		errstr.sprintf("Shader compile failed (type %x):\n%s\n%s\n%s\n",
-			m_type, padding, &infoLog[0], padding);
-
-		// Prettyfy source
-		StringList lines(
-			StringList::splitString(fullSrc.toCString(), '\n', alloc));
-		I lineno = 0;
-		for(const String& line : lines)
-		{
-			String tmp(alloc);
-			tmp.sprintf("%4d: %s\n", ++lineno, &line[0]);
+//==============================================================================
+Error GlProgram::handleError(GlAllocator<U8>& alloc, String& src)
+{
+	Error err = ErrorCode::NONE;
 
-			errstr += tmp;
-		}
+	GLint compilerLogLen = 0;
+	GLint charsWritten = 0;
+	String compilerLog;
+	String prettySrc;
+
+	static const char* padding = 
+		"======================================="
+		"=======================================";
+
+	glGetProgramiv(m_glName, GL_INFO_LOG_LENGTH, &compilerLogLen);
 
-		errstr += padding;
+	err = compilerLog.create(alloc, '?', compilerLogLen + 1);
 
-		ANKI_LOGE("%s", &errstr[0]);
-		return ErrorCode::USER_DATA;
+	StringList lines;
+	if(!err)
+	{
+		glGetProgramInfoLog(
+			m_glName, compilerLogLen, &charsWritten, &compilerLog[0]);
+		
+		err = StringList::splitString(alloc, src.toCString(), '\n', lines);
 	}
 
-	// 3) Populate with vars and blocks
-	//
-	err = populateVariablesAndBlock(alloc);
+	I lineno = 0;
+	for(auto it = lines.getBegin(); it != lines.getEnd() && !err; ++it)
+	{
+		String tmp;
+
+		err = tmp.sprintf(alloc, "%4d: %s\n", ++lineno, &(*it)[0]);
+		
+		if(!err)
+		{
+			err = prettySrc.append(alloc, tmp);
+		}
+	}
+
+	if(!err)
+	{
+		ANKI_LOGE("Shader compilation failed (type %x):\n%s\n%s\n%s\n%s",
+			m_type, padding, &compilerLog[0], padding, &prettySrc[0]);
+	}
 
 	return err;
 }

+ 1 - 1
src/gl/GlProgramPipeline.cpp

@@ -12,7 +12,7 @@ namespace anki {
 //==============================================================================
 Error GlProgramPipeline::create(
 	const GlProgramHandle* progsBegin, const GlProgramHandle* progsEnd,
-	GlAllocator<U8>& alloc)
+	GlAllocator<U8> alloc)
 {
 	ANKI_ASSERT(progsBegin != nullptr && progsEnd != nullptr);
 	ANKI_ASSERT(progsBegin != progsEnd);

+ 3 - 2
src/gl/GlProgramPipelineHandle.cpp

@@ -57,10 +57,11 @@ Error GlProgramPipelineHandle::commonConstructor(
 			} while(++prog != progsEnd);
 		}
 
-		Error operator()(GlCommandBuffer*)
+		Error operator()(GlCommandBuffer* cmdb)
 		{
 			Error err = m_ppline._get().create(
-				&m_progs[0], &m_progs[0] + m_progsCount);
+				&m_progs[0], &m_progs[0] + m_progsCount,
+				cmdb->getGlobalAllocator());
 
 			GlHandleState oldState = m_ppline._setState(
 				err ? GlHandleState::ERROR : GlHandleState::CREATED);

+ 5 - 1
src/gl/GlTexture.cpp

@@ -154,7 +154,11 @@ Error GlTexture::create(const Initializer& init,
 				{
 					PtrSize layerSize = init.m_data[level][0].m_size;
 					ANKI_ASSERT(layerSize > 0);
-					data.create(alloc, layerSize * m_depth);
+					Error err = data.create(alloc, layerSize * m_depth);
+					if(err)
+					{
+						return err;
+					}
 
 					for(U d = 0; d < m_depth; d++)
 					{