Bladeren bron

Fix code style and remove some bad code

Daniele Bartolini 13 jaren geleden
bovenliggende
commit
dbcedfbda0
2 gewijzigde bestanden met toevoegingen van 144 en 401 verwijderingen
  1. 104 337
      src/core/containers/Generic.cpp
  2. 40 64
      src/core/containers/Generic.h

+ 104 - 337
src/core/containers/Generic.cpp

@@ -24,18 +24,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Generic.h"
-#include "Str.h"
-#include "List.h"
 #include "Types.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Generic::Generic():
-	mType(GT_Undefined)
+Generic::Generic() : m_type(GT_UNDEFINED)
 {
-	mData.intValue = 0;
 }
 
 //-----------------------------------------------------------------------------
@@ -47,218 +43,156 @@ Generic::Generic(const Generic& other)
 //-----------------------------------------------------------------------------
 Generic::~Generic()
 {
-	if (mType == GT_Str)
-	{
-		delete mData.stringValue;
-	}
-//	else if (mType == GT_List)
-//	{
-//		delete mData.listValue;
-//	}
-}
-
-//-----------------------------------------------------------------------------
-Generic::Generic(int value):
-	mType(GT_Int)
-{
-	mData.intValue = value;
-}
-
-//-----------------------------------------------------------------------------
-Generic::Generic(uint value):
-	mType(GT_UInt)
-{
-	mData.uintValue = value;
 }
 
 //-----------------------------------------------------------------------------
-Generic::Generic(ushort value):
-	mType(GT_UShort)
+Generic::Generic(int value) : m_type(GT_INT)
 {
-	mData.ushortValue = value;
+	m_data.int_value = value;
 }
 
 //-----------------------------------------------------------------------------
-Generic::Generic(float value):
-	mType(GT_Float)
+Generic::Generic(uint value) : m_type(GT_UINT)
 {
-	mData.floatValue = value;
+	m_data.uint_value = value;
 }
 
 //-----------------------------------------------------------------------------
-Generic::Generic(bool value):
-	mType(GT_Bool)
+Generic::Generic(ushort value) : m_type(GT_USHORT)
 {
-	mData.boolValue = value;
+	m_data.ushort_value = value;
 }
 
 //-----------------------------------------------------------------------------
-Generic::Generic(const Str& value):
-	mType(GT_Str)
+Generic::Generic(float value) : m_type(GT_FLOAT)
 {
-	mData.stringValue = new Str(value);
+	m_data.float_value = value;
 }
 
 //-----------------------------------------------------------------------------
-Generic::Generic(const char* value):
-	mType(GT_Str)
+Generic::Generic(bool value) : m_type(GT_BOOL)
 {
-	mData.stringValue = new Str(value);
+	m_data.bool_value = value;
 }
 
-////-----------------------------------------------------------------------------
-//Generic::Generic(IList<Generic>* value):
-//	mType(GT_List)
-//{
-//	mData.listValue = new Shared<IList<Generic> >(value);
-//}
-
-////-----------------------------------------------------------------------------
-//Generic::Generic(Object* value):
-//	mType(GT_Object)
-//{
-//	mData.objectValue = value;
-//}
-
 //-----------------------------------------------------------------------------
-bool Generic::asInt(int& out) const
+bool Generic::as_int(int& out) const
 {
-	if (mType == GT_Int)
+	if (m_type == GT_INT)
 	{
-		out = mData.intValue;
+		out = m_data.int_value;
 		return true;
 	}
 
-	if (mType == GT_UInt)
+	if (m_type == GT_UINT)
 	{
-		out = (int)mData.uintValue;
+		out = (int)m_data.uint_value;
 		return true;
 	}
 
-	if (mType == GT_UShort)
+	if (m_type == GT_USHORT)
 	{
-		out = (int)mData.ushortValue;
+		out = (int)m_data.ushort_value;
 		return true;
 	}
 
-	if (mType == GT_Float)
+	if (m_type == GT_FLOAT)
 	{
-		out = (int)mData.floatValue;
+		out = (int)m_data.float_value;
 		return true;
 	}
 
-	if (mType == GT_Bool)
+	if (m_type == GT_BOOL)
 	{
-		out = mData.boolValue?1:0;
+		out = m_data.bool_value ? 1 : 0;
 		return true;
 	}
 
-	if (mType == GT_Str)
-	{
-		return mData.stringValue->ParseInt(&out);
-	}
-
 	return false;
 }
 
 //-----------------------------------------------------------------------------
-int Generic::asInt() const
+int Generic::as_int() const
 {
 	int value = 0;
-	asInt(value);
+	as_int(value);
 	return value;
 }
 
 //-----------------------------------------------------------------------------
-bool Generic::asUInt(uint& out) const
+bool Generic::as_uint(uint& out) const
 {
-	if (mType == GT_Int)
+	if (m_type == GT_INT)
 	{
-		out = (uint)mData.intValue;
+		out = (uint)m_data.int_value;
 		return true;
 	}
 
-	if (mType == GT_UInt)
+	if (m_type == GT_UINT)
 	{
-		out = mData.uintValue;
+		out = m_data.uint_value;
 		return true;
 	}
 
-	if (mType == GT_UShort)
+	if (m_type == GT_USHORT)
 	{
-		out = (uint)mData.ushortValue;
+		out = (uint)m_data.ushort_value;
 		return true;
 	}
 
-	if (mType == GT_Float)
+	if (m_type == GT_FLOAT)
 	{
-		out = (uint)mData.floatValue;
+		out = (uint)m_data.float_value;
 		return true;
 	}
 
-	if (mType == GT_Bool)
+	if (m_type == GT_BOOL)
 	{
-		out = mData.boolValue?1:0;
+		out = m_data.bool_value?1:0;
 		return true;
 	}
 
-	if (mType == GT_Str)
-	{
-		return mData.stringValue->ParseUInt(&out);
-	}
-
 	return false;
 }
 
 //-----------------------------------------------------------------------------
-uint Generic::asUInt() const
+uint Generic::as_uint() const
 {
 	uint value = 0;
-	asUInt(value);
+	as_uint(value);
 	return value;
 }
 
 //-----------------------------------------------------------------------------
-bool Generic::asUShort(ushort& out) const
+bool Generic::as_ushort(ushort& out) const
 {
-	if (mType == GT_Int)
+	if (m_type == GT_INT)
 	{
-		out = (ushort)mData.intValue;
+		out = (ushort)m_data.int_value;
 		return true;
 	}
 
-	if (mType == GT_UInt)
+	if (m_type == GT_UINT)
 	{
-		out = (ushort)mData.uintValue;
+		out = (ushort)m_data.uint_value;
 		return true;
 	}
 
-	if (mType == GT_UShort)
+	if (m_type == GT_USHORT)
 	{
-		out = mData.ushortValue;
+		out = m_data.ushort_value;
 		return true;
 	}
 
-	if (mType == GT_Float)
+	if (m_type == GT_FLOAT)
 	{
-		out = (ushort)mData.floatValue;
+		out = (ushort)m_data.float_value;
 		return true;
 	}
 
-	if (mType == GT_Bool)
+	if (m_type == GT_BOOL)
 	{
-		out = mData.boolValue?1:0;
-		return true;
-	}
-
-	if (mType == GT_Str)
-	{
-		uint tmp;
-		if (!mData.stringValue->ParseUInt(&tmp))
-		{
-			return false;
-		}
-		out = (ushort)tmp;
+		out = m_data.bool_value?1:0;
 		return true;
 	}
 
@@ -266,265 +200,128 @@ bool Generic::asUShort(ushort& out) const
 }
 
 //-----------------------------------------------------------------------------
-ushort Generic::asUShort() const
+ushort Generic::as_ushort() const
 {
 	ushort value = 0;
-	asUShort(value);
+	as_ushort(value);
 	return value;
 }
 
 //-----------------------------------------------------------------------------
-bool Generic::asFloat(float& out) const
+bool Generic::as_float(float& out) const
 {
-	if (mType == GT_Int)
+	if (m_type == GT_INT)
 	{
-		out = (float)mData.intValue;
+		out = (float)m_data.int_value;
 		return true;
 	}
 
-	if (mType == GT_UInt)
+	if (m_type == GT_UINT)
 	{
-		out = (float)mData.uintValue;
+		out = (float)m_data.uint_value;
 		return true;
 	}
 
-	if (mType == GT_UShort)
+	if (m_type == GT_USHORT)
 	{
-		out = (float)mData.ushortValue;
+		out = (float)m_data.ushort_value;
 		return true;
 	}
 
-	if (mType == GT_Float)
+	if (m_type == GT_FLOAT)
 	{
-		out = mData.floatValue;
+		out = m_data.float_value;
 		return true;
 	}
 
-	if (mType == GT_Bool)
+	if (m_type == GT_BOOL)
 	{
-		out = mData.boolValue?1.0f:0.0f;
+		out = m_data.bool_value?1.0f:0.0f;
 		return true;
 	}
 
-	if (mType == GT_Str)
-	{
-		return mData.stringValue->ParseFloat(&out);
-	}
-
 	return false;
 }
 
 //-----------------------------------------------------------------------------
-float Generic::asFloat() const
+float Generic::as_float() const
 {
 	float value = 0.0f;
-	asFloat(value);
+	as_float(value);
 	return value;
 }
 
 //-----------------------------------------------------------------------------
-bool Generic::asBool(bool& out) const
+bool Generic::as_bool(bool& out) const
 {
-	if (mType == GT_Int)
+	if (m_type == GT_INT)
 	{
-		out = mData.intValue==0?false:true;
+		out = m_data.int_value==0?false:true;
 		return true;
 	}
 
-	if (mType == GT_UInt)
+	if (m_type == GT_UINT)
 	{
-		out = mData.uintValue==0?false:true;
+		out = m_data.uint_value==0?false:true;
 		return true;
 	}
 
-	if (mType == GT_UShort)
+	if (m_type == GT_USHORT)
 	{
-		out = mData.ushortValue==0?false:true;
+		out = m_data.ushort_value==0?false:true;
 		return true;
 	}
 
-	if (mType == GT_Float)
+	if (m_type == GT_FLOAT)
 	{
-		//Does it make sense? xD
-		out = mData.floatValue==0.0f?false:true;
+		//Does it make sense?
+		out = m_data.float_value==0.0f?false:true;
 		return true;
 	}
 
-	if (mType == GT_Bool)
+	if (m_type == GT_BOOL)
 	{
-		out = mData.boolValue;
+		out = m_data.bool_value;
 		return true;
 	}
 
-	if (mType == GT_Str)
-	{
-		if (*mData.stringValue == "True" || *mData.stringValue == "true")
-		{
-			out = true;
-			return true;
-		}
-
-		if (*mData.stringValue == "False" || *mData.stringValue == "false")
-		{
-			out = false;
-			return true;
-		}
-
-		return false;
-	}
-
 	return false;
 }
 
 //-----------------------------------------------------------------------------
-bool Generic::asBool() const
+bool Generic::as_bool() const
 {
 	bool value = false;
-	asBool(value);
+	as_bool(value);
 	return value;
 }
 
-//-----------------------------------------------------------------------------
-bool Generic::asStr(Str& out) const
-{
-	if (mType == GT_Int)
-	{
-		out = mData.intValue;
-		return true;
-	}
-
-	if (mType == GT_UInt)
-	{
-		out = mData.uintValue;
-		return true;
-	}
-
-	if (mType == GT_UShort)
-	{
-		out = mData.ushortValue;
-		return true;
-	}
-
-	if (mType == GT_Float)
-	{
-		out = mData.floatValue;
-		return true;
-	}
-
-	if (mType == GT_Bool)
-	{
-		if (mData.boolValue)
-		{
-			out = "True";
-		}
-		else
-		{
-			out = "False";
-		}
-
-		return true;
-	}
-
-	if (mType == GT_Str)
-	{
-		out = *mData.stringValue;
-		return true;
-	}
-
-//	if (mType == GT_List)
-//	{
-//		out = (*mData.listValue)->ToStr();
-//		return true;
-//	}
-
-//	if (mType == GT_Object)
-//	{
-//		out = mData.objectValue->ToStr();
-//		return true;
-//	}
-
-	return false;
-}
-
-//-----------------------------------------------------------------------------
-Str Generic::asStr() const
-{
-	Str value;
-	asStr(value);
-	return value;
-}
-
-////-----------------------------------------------------------------------------
-//bool Generic::asList(Shared<IList<Generic> >& out) const
-//{
-//	if (mType == GT_List)
-//	{
-//		out = *mData.listValue;
-//		return true;
-//	}
-
-//	return false;
-//}
-
-////-----------------------------------------------------------------------------
-//bool Generic::asObject(Object** out) const
-//{
-//	if (mType == GT_Object)
-//	{
-//		*out = mData.objectValue;
-//		return true;
-//	}
-
-//	*out = NULL;
-//	return false;
-//}
-
-////-----------------------------------------------------------------------------
-//Object* Generic::asObject() const
-//{
-//	if (mType == GT_Object)
-//	{
-//		return mData.objectValue;
-//	}
-
-//	return NULL;
-//}
-
 //-----------------------------------------------------------------------------
 bool Generic::operator==(const Generic& other)
 {
-	if (mType != other.mType)
+	if (m_type != other.m_type)
 	{
 		return false;
 	}
 
-	switch (mType)
+	switch (m_type)
 	{
-		case GT_Int:
-			return mData.intValue == other.mData.intValue;
+		case GT_INT:
+			return m_data.int_value == other.m_data.int_value;
 			break;
-		case GT_UInt:
-			return mData.uintValue == other.mData.uintValue;
+		case GT_UINT:
+			return m_data.uint_value == other.m_data.uint_value;
 			break;
-		case GT_UShort:
-			return mData.ushortValue == other.mData.ushortValue;
+		case GT_USHORT:
+			return m_data.ushort_value == other.m_data.ushort_value;
 			break;
-		case GT_Float:
-			return mData.floatValue == other.mData.floatValue;
+		case GT_FLOAT:
+			return m_data.float_value == other.m_data.float_value;
 			break;
-		case GT_Bool:
-			return mData.boolValue == other.mData.boolValue;
+		case GT_BOOL:
+			return m_data.bool_value == other.m_data.bool_value;
 			break;
-		case GT_Str:
-			return mData.stringValue == other.mData.stringValue;
-			break;
-//		case GT_List:
-//			return *mData.listValue == *other.mData.listValue;
-//			break;
-//		case GT_Object:
-//			return mData.objectValue == other.mData.objectValue;
-//			break;
-		case GT_Undefined:
+		case GT_UNDEFINED:
 			return true;
 			break;
 	}
@@ -535,35 +332,26 @@ bool Generic::operator==(const Generic& other)
 //-----------------------------------------------------------------------------
 const Generic& Generic::operator=(const Generic& other)
 {
-	mType = other.mType;
+	m_type = other.m_type;
 
-	switch (mType)
+	switch (m_type)
 	{
-		case GT_Int:
-			mData.intValue = other.mData.intValue;
-			break;
-		case GT_UInt:
-			mData.uintValue = other.mData.uintValue;
+		case GT_INT:
+			m_data.int_value = other.m_data.int_value;
 			break;
-		case GT_UShort:
-			mData.ushortValue = other.mData.ushortValue;
+		case GT_UINT:
+			m_data.uint_value = other.m_data.uint_value;
 			break;
-		case GT_Float:
-			mData.floatValue = other.mData.floatValue;
+		case GT_USHORT:
+			m_data.ushort_value = other.m_data.ushort_value;
 			break;
-		case GT_Bool:
-			mData.boolValue = other.mData.boolValue;
+		case GT_FLOAT:
+			m_data.float_value = other.m_data.float_value;
 			break;
-		case GT_Str:
-			mData.stringValue = new Str(*other.mData.stringValue);
+		case GT_BOOL:
+			m_data.bool_value = other.m_data.bool_value;
 			break;
-//		case GT_List:
-//			mData.listValue = new Shared<IList<Generic> >(*other.mData.listValue);
-//			break;
-//		case GT_Object:
-//			mData.objectValue = other.mData.objectValue;
-//			break;
-		case GT_Undefined:
+		case GT_UNDEFINED:
 			//Copy nothing
 			break;
 	}
@@ -571,26 +359,5 @@ const Generic& Generic::operator=(const Generic& other)
 	return other;
 }
 
-////-----------------------------------------------------------------------------
-//Str GenericList::ToStr() const
-//{
-//	Str out;
-//	out = "[";
-//	List<Generic>::Enumerator en = getBegin();
-//	bool first = true;
+} // namespace crown
 
-//	while (en.next())
-//	{
-//		if (!first)
-//		{
-//			out += ",";
-//		}
-
-//		first = false;
-//		out += en.current().asStr();
-//	}
-
-//	return out + "]";
-//}
-
-}

+ 40 - 64
src/core/containers/Generic.h

@@ -26,79 +26,58 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Types.h"
-#include "Shared.h"
 
 namespace crown
 {
 
-class Str;
-
 class Generic
 {
 public:
 
 	enum Type
 	{
-		GT_Undefined = 0,
-		/* -- Basic types -- */
-		GT_Int,
-		GT_UInt,
-		GT_UShort,
-		GT_Float,
-		GT_Bool,
-
-		/* -- Container types -- */
-		GT_Str
-//		GT_List,
-//		GT_Object
+		GT_UNDEFINED	= 0,
+
+		// Basic types
+		GT_INT			= 1,
+		GT_UINT			= 2,
+		GT_USHORT		= 3,
+		GT_FLOAT		= 4,
+		GT_BOOL			= 5
 	};
 
 					Generic();
 					Generic(const Generic& other);
-	virtual			~Generic();
-
-					Generic(int value);
-					Generic(uint value);
-					Generic(ushort value);
-					Generic(float value);
-					Generic(bool value);
-					Generic(const char* value);
-					Generic(const Str& value);
-//					Generic(IList<Generic>* value);
-//					Generic(Object* value);
-
-	inline Type		GetType() const
-	{
-		return mType;
-	}
-
-	bool			asInt(int& out) const;
-	int				asInt() const;
+					~Generic();
 
-	bool			asUInt(uint& out) const;
-	uint			asUInt() const;
+	explicit		Generic(int value);
+	explicit		Generic(uint value);
+	explicit		Generic(ushort value);
+	explicit		Generic(float value);
+	explicit		Generic(bool value);
+	explicit		Generic(const char* value);
 
-	bool			asUShort(ushort& out) const;
-	ushort			asUShort() const;
+	inline Type		get_type() const { return m_type; }
 
-	bool			asFloat(float& out) const;
-	float			asFloat() const;
+	bool			as_int(int& out) const;
+	int				as_int() const;
 
-	bool			asBool(bool& out) const;
-	bool			asBool() const;
+	bool			as_uint(uint& out) const;
+	uint			as_uint() const;
 
-	bool			asStr(Str& out) const;
-	Str				asStr() const;
+	bool			as_ushort(ushort& out) const;
+	ushort			as_ushort() const;
 
-//	bool			asList(Shared<IList<Generic> >& out) const;
+	bool			as_float(float& out) const;
+	float			as_float() const;
 
-//	bool			asObject(Object** out) const;
-//	Object*			asObject() const;
+	bool			as_bool(bool& out) const;
+	bool			as_bool() const;
 
 	template <typename T>
-	bool			asType(T* out) const;
+	bool			as_type(T* out) const;
 	template <typename T>
-	T*				asType() const;
+	T*				as_type() const;
 
 	const Generic&	operator=(const Generic& other);
 
@@ -108,38 +87,35 @@ private:
 
 	union Data
 	{
-		int			intValue;
-		uint		uintValue;
-		ushort		ushortValue;
-		float		floatValue;
-		bool		boolValue;
-		Str*		stringValue;
-//		Shared<IList<Generic> >* listValue;
-//		Object* objectValue;
+		int			int_value;
+		uint		uint_value;
+		ushort		ushort_value;
+		float		float_value;
+		bool		bool_value;
 	};
 
-	Type			mType;
-	Data			mData;
+	Type			m_type;
+	Data			m_data;
 };
 
 template <typename T>
-bool Generic::asType(T* out) const
+bool Generic::as_type(T* out) const
 {
 //	if (mType == GT_Object)
 //	{
-//		*out = dynamic_cast<T>(mData.objectValue);
-//		return *out != NULL || mData.objectValue == NULL;
+//		*out = dynamic_cas_t<T>(mData.object_value);
+//		return *out != NULL || mData.object_value == NULL;
 //	}
 
 	return false;
 }
 
 template <typename T>
-T* Generic::asType() const
+T* Generic::as_type() const
 {
 //	if (mType == GT_Object)
 //	{
-//		return dynamic_cast<T*>(mData.objectValue);
+//		return dynamic_cast<T*>(mData.object_value);
 //	}
 
 	return NULL;