Branimir Karadžić 9 år sedan
förälder
incheckning
09b849a416
6 ändrade filer med 446 tillägg och 289 borttagningar
  1. 29 58
      include/bx/error.h
  2. 73 0
      include/bx/error.inl
  3. 81 186
      include/bx/string.h
  4. 196 0
      include/bx/string.inl
  5. 6 45
      include/bx/timer.h
  6. 61 0
      include/bx/timer.inl

+ 29 - 58
include/bx/error.h

@@ -44,55 +44,29 @@ namespace bx
 			);
 
 	public:
-		Error()
-			: m_code(0)
-		{
-		}
-
-		void reset()
-		{
-			m_code = 0;
-			m_msg.clear();
-		}
-
-		void setError(ErrorResult _errorResult, const StringView& _msg)
-		{
-			BX_CHECK(0 != _errorResult.code, "Invalid ErrorResult passed to setError!");
-
-			if (!isOk() )
-			{
-				return;
-			}
-
-			m_code = _errorResult.code;
-			m_msg  = _msg;
-		}
-
-		bool isOk() const
-		{
-			return 0 == m_code;
-		}
-
-		ErrorResult get() const
-		{
-			ErrorResult result = { m_code };
-			return result;
-		}
-
-		const StringView& getMessage() const
-		{
-			return m_msg;
-		}
-
-		bool operator==(const ErrorResult& _rhs) const
-		{
-			return _rhs.code == m_code;
-		}
-
-		bool operator!=(const ErrorResult& _rhs) const
-		{
-			return _rhs.code != m_code;
-		}
+		///
+		Error();
+
+		///
+		void reset();
+
+		///
+		void setError(ErrorResult _errorResult, const StringView& _msg);
+
+		///
+		bool isOk() const;
+
+		///
+		ErrorResult get() const;
+
+		///
+		const StringView& getMessage() const;
+
+		///
+		bool operator==(const ErrorResult& _rhs) const;
+
+		///
+		bool operator!=(const ErrorResult& _rhs) const;
 
 	private:
 		StringView m_msg;
@@ -108,16 +82,11 @@ namespace bx
 			);
 
 	public:
-		ErrorScope(Error* _err)
-			: m_err(_err)
-		{
-			BX_CHECK(NULL != _err, "_err can't be NULL");
-		}
+		///
+		ErrorScope(Error* _err);
 
-		~ErrorScope()
-		{
-			BX_CHECK(m_err->isOk(), "Error: %d", m_err->get().code);
-		}
+		///
+		~ErrorScope();
 
 	private:
 		Error* m_err;
@@ -125,4 +94,6 @@ namespace bx
 
 } // namespace bx
 
+#include "error.inl"
+
 #endif // BX_ERROR_H_HEADER_GUARD

+ 73 - 0
include/bx/error.inl

@@ -0,0 +1,73 @@
+/*
+ * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
+ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
+ */
+
+#ifndef BX_ERROR_H_HEADER_GUARD
+#	error "Must be included from bx/error!"
+#endif // BX_ERROR_H_HEADER_GUARD
+
+namespace bx
+{
+	inline Error::Error()
+		: m_code(0)
+	{
+	}
+
+	inline void Error::reset()
+	{
+		m_code = 0;
+		m_msg.clear();
+	}
+
+	inline void Error::setError(ErrorResult _errorResult, const StringView& _msg)
+	{
+		BX_CHECK(0 != _errorResult.code, "Invalid ErrorResult passed to setError!");
+
+		if (!isOk() )
+		{
+			return;
+		}
+
+		m_code = _errorResult.code;
+		m_msg  = _msg;
+	}
+
+	inline bool Error::isOk() const
+	{
+		return 0 == m_code;
+	}
+
+	inline ErrorResult Error::get() const
+	{
+		ErrorResult result = { m_code };
+		return result;
+	}
+
+	inline const StringView& Error::getMessage() const
+	{
+		return m_msg;
+	}
+
+	inline bool Error::operator==(const ErrorResult& _rhs) const
+	{
+		return _rhs.code == m_code;
+	}
+
+	inline bool Error::operator!=(const ErrorResult& _rhs) const
+	{
+		return _rhs.code != m_code;
+	}
+
+	inline ErrorScope::ErrorScope(Error* _err)
+		: m_err(_err)
+	{
+		BX_CHECK(NULL != _err, "_err can't be NULL");
+	}
+
+	inline ErrorScope::~ErrorScope()
+	{
+		BX_CHECK(m_err->isOk(), "Error: %d", m_err->get().code);
+	}
+
+} // namespace bx

+ 81 - 186
include/bx/string.h

@@ -14,12 +14,80 @@
 #include <bx/allocator.h>
 #include <bx/hash.h>
 
-#ifndef va_copy
-#	define va_copy(_a, _b) (_a) = (_b)
-#endif // va_copy
-
 namespace bx
 {
+	/// Non-zero-terminated string view.
+	class StringView
+	{
+	public:
+		///
+		StringView();
+
+		///
+		StringView(const StringView& _rhs);
+
+		///
+		StringView& operator=(const StringView& _rhs);
+
+		///
+		StringView(const char* _ptr, uint32_t _len = UINT16_MAX);
+
+		///
+		void set(const char* _ptr, uint32_t _len = UINT16_MAX);
+
+		///
+		void clear();
+
+		///
+		const char* getPtr() const;
+
+		///
+		const char* getTerm() const;
+
+		///
+		bool isEmpty() const;
+
+		///
+		uint32_t getLength() const;
+
+	protected:
+		const char* m_ptr;
+		uint32_t    m_len;
+	};
+
+	/// ASCII string
+	template<bx::AllocatorI** AllocatorT>
+	class StringT : public StringView
+	{
+	public:
+		///
+		StringT();
+
+		///
+		StringT(const StringT<AllocatorT>& _rhs);
+
+		///
+		StringT<AllocatorT>& operator=(const StringT<AllocatorT>& _rhs);
+
+		///
+		StringT(const char* _ptr, uint32_t _len = UINT32_MAX);
+
+		///
+		StringT(const StringView& _rhs);
+
+		///
+		~StringT();
+
+		///
+		void set(const char* _ptr, uint32_t _len = UINT32_MAX);
+
+		///
+		void append(const char* _ptr, uint32_t _len = UINT32_MAX);
+
+		///
+		void clear();
+	};
+
 	///
 	bool toBool(const char* _str);
 
@@ -87,47 +155,15 @@ namespace bx
 
 	///
 	template <typename Ty>
-	inline void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList)
-	{
-		char temp[2048];
-
-		char* out = temp;
-		int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
-		if ( (int32_t)sizeof(temp) < len)
-		{
-			out = (char*)alloca(len+1);
-			len = bx::vsnprintf(out, len, _format, _argList);
-		}
-		out[len] = '\0';
-		_out.append(out);
-	}
+	void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList);
 
 	///
 	template <typename Ty>
-	inline void stringPrintf(Ty& _out, const char* _format, ...)
-	{
-		va_list argList;
-		va_start(argList, _format);
-		stringPrintfVargs(_out, _format, argList);
-		va_end(argList);
-	}
+	void stringPrintf(Ty& _out, const char* _format, ...);
 
 	/// Replace all instances of substring.
 	template <typename Ty>
-	inline Ty replaceAll(const Ty& _str, const char* _from, const char* _to)
-	{
-		Ty str = _str;
-		size_t startPos = 0;
-		const size_t fromLen = strlen(_from);
-		const size_t toLen   = strlen(_to);
-		while ( (startPos = str.find(_from, startPos) ) != Ty::npos)
-		{
-			str.replace(startPos, fromLen, _to);
-			startPos += toLen;
-		}
-
-		return str;
-	}
+	Ty replaceAll(const Ty& _str, const char* _from, const char* _to);
 
 	/// Extract base file name from file path.
 	const char* baseName(const char* _filePath);
@@ -147,155 +183,14 @@ namespace bx
 	/// If retval >= siz, truncation occurred.
 	size_t strlcat(char* _dst, const char* _src, size_t _siz);
 
-	/// Non-zero-terminated string view.
-	class StringView
-	{
-	public:
-		StringView()
-		{
-			clear();
-		}
-
-		StringView(const StringView& _rhs)
-		{
-			set(_rhs.m_ptr, _rhs.m_len);
-		}
-
-		StringView& operator=(const StringView& _rhs)
-		{
-			set(_rhs.m_ptr, _rhs.m_len);
-			return *this;
-		}
-
-		StringView(const char* _ptr, uint32_t _len = UINT16_MAX)
-		{
-			set(_ptr, _len);
-		}
-
-		void set(const char* _ptr, uint32_t _len = UINT16_MAX)
-		{
-			clear();
-
-			if (NULL != _ptr)
-			{
-				uint32_t len = uint32_t(strnlen(_ptr, _len) );
-				if (0 != len)
-				{
-					m_len = len;
-					m_ptr = _ptr;
-				}
-			}
-		}
-
-		void clear()
-		{
-			m_ptr = "";
-			m_len = 0;
-		}
-
-		const char* getPtr() const
-		{
-			return m_ptr;
-		}
-
-		const char* getTerm() const
-		{
-			return m_ptr + m_len;
-		}
-
-		bool isEmpty() const
-		{
-			return 0 == m_len;
-		}
-
-		uint32_t getLength() const
-		{
-			return m_len;
-		}
-
-	protected:
-		const char* m_ptr;
-		uint32_t    m_len;
-	};
-
-	inline uint32_t hashMurmur2A(const StringView& _data)
-	{
-		return hashMurmur2A(_data.getPtr(), _data.getLength() );
-	}
-
-	inline uint32_t hashMurmur2A(const char* _data)
-	{
-		return hashMurmur2A(StringView(_data) );
-	}
+	///
+	uint32_t hashMurmur2A(const StringView& _data);
 
-	/// ASCII string
-	template<bx::AllocatorI** AllocatorT>
-	class StringT : public StringView
-	{
-	public:
-		StringT()
-			: StringView()
-		{
-		}
-
-		StringT(const StringT<AllocatorT>& _rhs)
-			: StringView()
-		{
-			set(_rhs.m_ptr, _rhs.m_len);
-		}
-
-		StringT<AllocatorT>& operator=(const StringT<AllocatorT>& _rhs)
-		{
-			set(_rhs.m_ptr, _rhs.m_len);
-			return *this;
-		}
-
-		StringT(const char* _ptr, uint32_t _len = UINT32_MAX)
-		{
-			set(_ptr, _len);
-		}
-
-		StringT(const StringView& _rhs)
-		{
-			set(_rhs.getPtr(), _rhs.getLength() );
-		}
-
-		~StringT()
-		{
-			clear();
-		}
-
-		void set(const char* _ptr, uint32_t _len = UINT32_MAX)
-		{
-			clear();
-			append(_ptr, _len);
-		}
-
-		void append(const char* _ptr, uint32_t _len = UINT32_MAX)
-		{
-			if (0 != _len)
-			{
-				uint32_t old = m_len;
-				uint32_t len = m_len + uint32_t(strnlen(_ptr, _len) );
-				char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast<char*>(m_ptr) : NULL, len+1);
-				m_len = len;
-				strlncpy(ptr + old, len-old+1, _ptr, _len);
-
-				*const_cast<char**>(&m_ptr) = ptr;
-			}
-		}
-
-		void clear()
-		{
-			if (0 != m_len)
-			{
-				BX_FREE(*AllocatorT, const_cast<char*>(m_ptr) );
-
-				StringView::clear();
-			}
-		}
-	};
+	///
+	uint32_t hashMurmur2A(const char* _data);
 
 } // namespace bx
 
+#include "string.inl"
+
 #endif // BX_STRING_H_HEADER_GUARD

+ 196 - 0
include/bx/string.inl

@@ -0,0 +1,196 @@
+/*
+ * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
+ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
+ */
+
+#ifndef BX_STRING_H_HEADER_GUARD
+#	error "Must be included from bx/string.h!"
+#endif // BX_STRING_H_HEADER_GUARD
+
+namespace bx
+{
+	template <typename Ty>
+	inline void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList)
+	{
+		char temp[2048];
+
+		char* out = temp;
+		int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
+		if ( (int32_t)sizeof(temp) < len)
+		{
+			out = (char*)alloca(len+1);
+			len = bx::vsnprintf(out, len, _format, _argList);
+		}
+		out[len] = '\0';
+		_out.append(out);
+	}
+
+	template <typename Ty>
+	inline void stringPrintf(Ty& _out, const char* _format, ...)
+	{
+		va_list argList;
+		va_start(argList, _format);
+		stringPrintfVargs(_out, _format, argList);
+		va_end(argList);
+	}
+
+	template <typename Ty>
+	inline Ty replaceAll(const Ty& _str, const char* _from, const char* _to)
+	{
+		Ty str = _str;
+		size_t startPos = 0;
+		const size_t fromLen = strlen(_from);
+		const size_t toLen   = strlen(_to);
+		while ( (startPos = str.find(_from, startPos) ) != Ty::npos)
+		{
+			str.replace(startPos, fromLen, _to);
+			startPos += toLen;
+		}
+
+		return str;
+	}
+
+	inline StringView::StringView()
+	{
+		clear();
+	}
+
+	inline StringView::StringView(const StringView& _rhs)
+	{
+		set(_rhs.m_ptr, _rhs.m_len);
+	}
+
+	inline StringView& StringView::operator=(const StringView& _rhs)
+	{
+		set(_rhs.m_ptr, _rhs.m_len);
+		return *this;
+	}
+
+	inline StringView::StringView(const char* _ptr, uint32_t _len)
+	{
+		set(_ptr, _len);
+	}
+
+	inline void StringView::set(const char* _ptr, uint32_t _len)
+	{
+		clear();
+
+		if (NULL != _ptr)
+		{
+			uint32_t len = uint32_t(strnlen(_ptr, _len) );
+			if (0 != len)
+			{
+				m_len = len;
+				m_ptr = _ptr;
+			}
+		}
+	}
+
+	inline void StringView::clear()
+	{
+		m_ptr = "";
+		m_len = 0;
+	}
+
+	inline const char* StringView::getPtr() const
+	{
+		return m_ptr;
+	}
+
+	inline const char* StringView::getTerm() const
+	{
+		return m_ptr + m_len;
+	}
+
+	inline bool StringView::isEmpty() const
+	{
+		return 0 == m_len;
+	}
+
+	inline uint32_t StringView::getLength() const
+	{
+		return m_len;
+	}
+
+	inline uint32_t hashMurmur2A(const StringView& _data)
+	{
+		return hashMurmur2A(_data.getPtr(), _data.getLength() );
+	}
+
+	inline uint32_t hashMurmur2A(const char* _data)
+	{
+		return hashMurmur2A(StringView(_data) );
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline StringT<AllocatorT>::StringT()
+		: StringView()
+	{
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline StringT<AllocatorT>::StringT(const StringT<AllocatorT>& _rhs)
+		: StringView()
+	{
+		set(_rhs.m_ptr, _rhs.m_len);
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline StringT<AllocatorT>& StringT<AllocatorT>::operator=(const StringT<AllocatorT>& _rhs)
+	{
+		set(_rhs.m_ptr, _rhs.m_len);
+		return *this;
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline StringT<AllocatorT>::StringT(const char* _ptr, uint32_t _len)
+	{
+		set(_ptr, _len);
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline StringT<AllocatorT>::StringT(const StringView& _rhs)
+	{
+		set(_rhs.getPtr(), _rhs.getLength() );
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline StringT<AllocatorT>::~StringT()
+	{
+		clear();
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline void StringT<AllocatorT>::set(const char* _ptr, uint32_t _len)
+	{
+		clear();
+		append(_ptr, _len);
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline void StringT<AllocatorT>::append(const char* _ptr, uint32_t _len)
+	{
+		if (0 != _len)
+		{
+			uint32_t old = m_len;
+			uint32_t len = m_len + uint32_t(strnlen(_ptr, _len) );
+			char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast<char*>(m_ptr) : NULL, len+1);
+			m_len = len;
+			strlncpy(ptr + old, len-old+1, _ptr, _len);
+
+			*const_cast<char**>(&m_ptr) = ptr;
+		}
+	}
+
+	template<bx::AllocatorI** AllocatorT>
+	inline void StringT<AllocatorT>::clear()
+	{
+		if (0 != m_len)
+		{
+			BX_FREE(*AllocatorT, const_cast<char*>(m_ptr) );
+
+			StringView::clear();
+		}
+	}
+
+} // namespace bx

+ 6 - 45
include/bx/timer.h

@@ -8,55 +8,16 @@
 
 #include "bx.h"
 
-#if BX_PLATFORM_ANDROID
-#	include <time.h> // clock, clock_gettime
-#elif BX_PLATFORM_EMSCRIPTEN
-#	include <emscripten.h>
-#elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
-#	include <windows.h>
-#else
-#	include <sys/time.h> // gettimeofday
-#endif // BX_PLATFORM_
-
 namespace bx
 {
-	inline int64_t getHPCounter()
-	{
-#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
-		LARGE_INTEGER li;
-		// Performance counter value may unexpectedly leap forward
-		// http://support.microsoft.com/kb/274323
-		QueryPerformanceCounter(&li);
-		int64_t i64 = li.QuadPart;
-#elif BX_PLATFORM_ANDROID
-		struct timespec now;
-		clock_gettime(CLOCK_MONOTONIC, &now);
-		int64_t i64 = now.tv_sec*INT64_C(1000000000) + now.tv_nsec;
-#elif BX_PLATFORM_EMSCRIPTEN
-		int64_t i64 = int64_t(1000.0f * emscripten_get_now() );
-#else
-		struct timeval now;
-		gettimeofday(&now, 0);
-		int64_t i64 = now.tv_sec*INT64_C(1000000) + now.tv_usec;
-#endif // BX_PLATFORM_
-		return i64;
-	}
+	///
+	int64_t getHPCounter();
 
-	inline int64_t getHPFrequency()
-	{
-#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
-		LARGE_INTEGER li;
-		QueryPerformanceFrequency(&li);
-		return li.QuadPart;
-#elif BX_PLATFORM_ANDROID
-		return INT64_C(1000000000);
-#elif BX_PLATFORM_EMSCRIPTEN
-		return INT64_C(1000000);
-#else
-		return INT64_C(1000000);
-#endif // BX_PLATFORM_
-	}
+	///
+	int64_t getHPFrequency();
 
 } // namespace bx
 
+#include "timer.inl"
+
 #endif // BX_TIMER_H_HEADER_GUARD

+ 61 - 0
include/bx/timer.inl

@@ -0,0 +1,61 @@
+/*
+ * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
+ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
+ */
+
+#ifndef BX_TIMER_H_HEADER_GUARD
+#	error "Must be included from bx/timer.h!"
+#endif // BX_TIMER_H_HEADER_GUARD
+
+#include "bx.h"
+
+#if BX_PLATFORM_ANDROID
+#	include <time.h> // clock, clock_gettime
+#elif BX_PLATFORM_EMSCRIPTEN
+#	include <emscripten.h>
+#elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
+#	include <windows.h>
+#else
+#	include <sys/time.h> // gettimeofday
+#endif // BX_PLATFORM_
+
+namespace bx
+{
+	inline int64_t getHPCounter()
+	{
+#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
+		LARGE_INTEGER li;
+		// Performance counter value may unexpectedly leap forward
+		// http://support.microsoft.com/kb/274323
+		QueryPerformanceCounter(&li);
+		int64_t i64 = li.QuadPart;
+#elif BX_PLATFORM_ANDROID
+		struct timespec now;
+		clock_gettime(CLOCK_MONOTONIC, &now);
+		int64_t i64 = now.tv_sec*INT64_C(1000000000) + now.tv_nsec;
+#elif BX_PLATFORM_EMSCRIPTEN
+		int64_t i64 = int64_t(1000.0f * emscripten_get_now() );
+#else
+		struct timeval now;
+		gettimeofday(&now, 0);
+		int64_t i64 = now.tv_sec*INT64_C(1000000) + now.tv_usec;
+#endif // BX_PLATFORM_
+		return i64;
+	}
+
+	inline int64_t getHPFrequency()
+	{
+#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
+		LARGE_INTEGER li;
+		QueryPerformanceFrequency(&li);
+		return li.QuadPart;
+#elif BX_PLATFORM_ANDROID
+		return INT64_C(1000000000);
+#elif BX_PLATFORM_EMSCRIPTEN
+		return INT64_C(1000000);
+#else
+		return INT64_C(1000000);
+#endif // BX_PLATFORM_
+	}
+
+} // namespace bx