Просмотр исходного кода

Add array_value() to access JSON arrays much much faster than by calling operator[]

Daniele Bartolini 12 лет назад
Родитель
Сommit
c25194eb88
2 измененных файлов с 107 добавлено и 0 удалено
  1. 84 0
      engine/core/json/JSONParser.cpp
  2. 23 0
      engine/core/json/JSONParser.h

+ 84 - 0
engine/core/json/JSONParser.cpp

@@ -369,6 +369,90 @@ const char* JSONElement::string_value()
 	return string.begin();
 }
 
+//--------------------------------------------------------------------------
+void JSONElement::array_value(List<bool>& array)
+{
+	TempAllocator1024 alloc;
+	List<const char*> temp(alloc);
+
+	JSONParser::parse_array(m_begin, temp);
+
+	for (uint32_t i = 0; i < temp.size(); i++)
+	{
+		array.push_back(JSONParser::parse_bool(temp[i]));
+	}
+}
+
+//--------------------------------------------------------------------------
+void JSONElement::array_value(List<int16_t>& array)
+{
+	TempAllocator1024 alloc;
+	List<const char*> temp(alloc);
+
+	JSONParser::parse_array(m_begin, temp);
+
+	for (uint32_t i = 0; i < temp.size(); i++)
+	{
+		array.push_back((int16_t)JSONParser::parse_int(temp[i]));
+	}
+}
+
+//--------------------------------------------------------------------------
+void JSONElement::array_value(List<uint16_t>& array)
+{
+	TempAllocator1024 alloc;
+	List<const char*> temp(alloc);
+
+	JSONParser::parse_array(m_begin, temp);
+
+	for (uint32_t i = 0; i < temp.size(); i++)
+	{
+		array.push_back((uint16_t)JSONParser::parse_int(temp[i]));
+	}
+}
+
+//--------------------------------------------------------------------------
+void JSONElement::array_value(List<int32_t>& array)
+{
+	TempAllocator1024 alloc;
+	List<const char*> temp(alloc);
+
+	JSONParser::parse_array(m_begin, temp);
+
+	for (uint32_t i = 0; i < temp.size(); i++)
+	{
+		array.push_back((int32_t)JSONParser::parse_int(temp[i]));
+	}
+}
+
+//--------------------------------------------------------------------------
+void JSONElement::array_value(List<uint32_t>& array)
+{
+	TempAllocator1024 alloc;
+	List<const char*> temp(alloc);
+
+	JSONParser::parse_array(m_begin, temp);
+
+	for (uint32_t i = 0; i < temp.size(); i++)
+	{
+		array.push_back((uint32_t)JSONParser::parse_int(temp[i]));
+	}
+}
+
+//--------------------------------------------------------------------------
+void JSONElement::array_value(List<float>& array)
+{
+	TempAllocator1024 alloc;
+	List<const char*> temp(alloc);
+
+	JSONParser::parse_array(m_begin, temp);
+
+	for (uint32_t i = 0; i < temp.size(); i++)
+	{
+		array.push_back(JSONParser::parse_float(temp[i]));
+	}
+}
+
 //--------------------------------------------------------------------------
 bool JSONElement::is_nil() const
 {

+ 23 - 0
engine/core/json/JSONParser.h

@@ -129,6 +129,29 @@ public:
 	/// instead of copying its content somewhere else.
 	const char*			string_value();
 
+	/// Returns the array value of the element.
+	/// @note
+	/// Calling this function is way faster than accessing individual
+	/// array elements by JSONElement::operator[] and it is the very preferred way
+	/// for retrieving array elemets. However, you have to be sure that the array
+	/// contains only items of the given @array type.
+	void				array_value(List<bool>& array);
+
+	/// @copydoc JSONElement::array_value(List<bool>&)
+	void				array_value(List<int16_t>& array);
+
+	/// @copydoc JSONElement::array_value(List<bool>&)
+	void				array_value(List<uint16_t>& array);
+
+	/// @copydoc JSONElement::array_value(List<bool>&)
+	void				array_value(List<int32_t>& array);
+
+	/// @copydoc JSONElement::array_value(List<bool>&)
+	void				array_value(List<uint32_t>& array);
+
+	/// @copydoc JSONElement::array_value(List<bool>&)
+	void				array_value(List<float>& array);
+
 private:
 
 	explicit			JSONElement(JSONParser& parser, const char* at);