void OutXmlSerializer::process(const char* name, Pointer* ptr, u32 elementSize)
{
	XmlElement* data = new XmlElement;
	data->setValue("data");

	if (name)
		data->setAttribute("name",name);

	if ((&m_allocation.allocator()) != (&HeapAllocator::allocator()))
	{
		BufferStream temp;
		temp << hex << setfill('0') << setw(8) << ptr->m_allocator->handle();

		data->setAttribute("allocator",temp.string());
	}

	if (m_allocation.alignment() != SerializableAllocation::DefaultAlignment)
	{
		BufferStream temp;
		temp << m_allocation.alignment();
		data->setAttribute("align",temp.string());
	}

	if (ptr->m_count > 0)
	{
		BufferStream temp;
		temp << ptr->m_count;
		data->setAttribute("count",temp.string());
	}

	{
		BufferStream temp;
		temp << elementSize;
		data->setAttribute("size",temp.string());
	}

	{
		BufferStream temp;
		Base64::encode(temp,ptr->m_objects,elementSize * ptr->m_count);

		if (temp.buffer().count() > 0)
		{
			XmlText* text = zenic_new XmlText;
			text->setValue(temp.string());
			data->addChild(text);
		}
	}	

	ZENIC_ASSERT(m_current);
	m_current->addChild(data);

	m_allocation = SerializableAllocation();
}
void OutXmlSerializer::pushStructure(const char* name, SerializableStructure* structure, Pointer* ptr)
{
	ZENIC_ASSERT(structure);

	XmlElement* st = new XmlElement;
	st->setValue("struct");

	if (name)
		st->setAttribute("name",name);

	SerializableFactory* factory = structure->factory();
	{
		BufferStream temp;
		temp << hex << setw(8) << setfill('0') << factory->host();
		st->setAttribute("host",temp.string());
	}
	{
		BufferStream temp;
		temp << hex << setw(8) << setfill('0') << factory->type();
		st->setAttribute("type",temp.string());
	}
	{
		BufferStream temp;
		temp << hex << setw(8) << setfill('0') << structure->identifier();
		st->setAttribute("id",temp.string());
	}

	if (ptr->m_allocator && (ptr->m_allocator != &HeapAllocator::allocator()))
	{
		BufferStream temp;
		temp << hex << setfill('0') << setw(8) << ptr->m_allocator->handle();
		st->setAttribute("allocator",temp.string());
	}
	
	{
		BufferStream temp;
		temp << ptr->m_count;
		st->setAttribute("count",temp.string());
	}

	m_stack.pushBack(m_current);
	m_current->addChild(st);
	m_current = st;
}
void OutXmlSerializer::process(SerializableVersion& version)
{
	if (!m_current)
	{
		ZENIC_ASSERT(version.factory());

		XmlElement* current = zenic_new XmlElement;
		current->setValue("object");
		
		{
			BufferStream temp;
			temp << m_currentIndex;
			current->setAttribute("id",temp.string());
		}

		{
			BufferStream temp;
			temp << hex << setw(8) << setfill('0') << version.factory()->host();
			current->setAttribute("host",temp.string());
		}

		{
			BufferStream temp;
			temp << hex << setw(8) << setfill('0') << version.factory()->type();
			current->setAttribute("type",temp.string());
		}

		m_current = current;
	}

	XmlElement* v = zenic_new XmlElement;
	v->setValue("version");

	XmlText* text = zenic_new XmlText;
	BufferStream temp;
	temp << version.version();
	text->setValue(temp.string());
	v->addChild(text);

	m_current->addChild(v);
}
void OutXmlSerializer::process(const char* name, const char* valueType, BufferStream& value)
{
	XmlElement* variable = zenic_new XmlElement;
	variable->setValue(valueType);

	if (name)
		variable->setAttribute("name",name);

	XmlText* text = zenic_new XmlText;
	text->setValue(value.string());
	variable->addChild(text);

	ZENIC_ASSERT(m_current);
	m_current->addChild(variable);
}
void OutXmlSerializer::process(const char* name, Serializable*& object)
{
	XmlElement* reference = new XmlElement;
	reference->setValue("ref");

	if (name)
		reference->setAttribute("name",name);

	XmlText* text = new XmlText;

	if (object)
	{
		// insert into list if needed

		uint i;

		for (i = 0; i < m_objects.count(); ++i)
		{
			if (m_objects[i] == object)
				break;
		}

		if (i == m_objects.count())
			m_objects.pushBack(object);

		BufferStream temp;
		temp << i;
		text->setValue(temp.string());
	}
	else
		text->setValue("null");

	reference->addChild(text);

	ZENIC_ASSERT(m_current);
	m_current->addChild(reference);
}