Serialization of irr::core classes

Announce new projects or updates of Irrlicht Engine related tools, games, and applications.
Also check the Wiki
Post Reply
FlyHigh
Posts: 111
Joined: Wed Mar 23, 2005 12:05 am

Serialization of irr::core classes

Post by FlyHigh »

I was reading the development page on irrlicht and the one of the items is 'create scene serialization', which I may have something to contribute to... A while ago (version 9ish) I overloaded all the ostream << operators to take pretty much all of the irr::core classes (and the corrosponding istream >> operators as well)

(I cant host code so i'll post it here ok?)

Code: Select all

#ifndef IRR_OUT_H
#define IRR_OUT_H

#include <irrlicht.h>
#include <iostream>

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::vector3d<T> &pos )
{	return os << "(" << pos.X << "," << pos.Y << "," << pos.Z << ")"; }

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::rect<T> &rect )
{	return os << "(" << rect.UpperLeftCorner << "," << rect.LowerRightCorner << ")"; }

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::vector2d<T> &pos )
{	return os << "(" << pos.X << "," << pos.Y << ")"; }

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::aabbox3d<T> &box )
{	return os << "(" << box.MinEdge << "," << box.MaxEdge << ")"; }

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::array<T> &ls )
{	
	os	<< "[" << ls.size() << "/" << ls.allocated_size() << "](";
	for(int i = 0; i < ls.size(); ++i)
	{
		os << ls[i] << ((i == ls.size() - 1) ? ")" : ",");
	}
	return os;
}

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::dimension2d<T> &dim )
{	return os << "(" << dim.Height << "," << dim.Width << ")"; }

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::line2d<T> &ln )
{	return os << "(" << ln.start << "," << ln.start << ")";}

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::line3d<T> &ln )
{	return os << "(" << ln.start << "," << ln.start << ")";}

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::list<T> &ls )
{	
	os	<< "[" << ls.getSize() << "](";
	for(irr::core::list<T>::Iterator i = ls.begin(); i != ls.end(); ++i)
	{
		os << (*i) << ",";
	}
	return os;
}

inline std::ostream &operator<<(std::ostream &os, const irr::core::matrix4 &m )
{
	os << "(";
	for(int i = 0; i < 4; ++i)
		for(int j = 0; j < 4; ++j)
			os << m(i,j) << (i == 3 && j == 3) ? ")" : ","; 
	return os;
}

template <class T>
inline std::ostream & operator<<( std::ostream &os, const irr::core::plane3d<T> &p)
{	return os << "(" << p.D << p.Normal << ")";	}

template <class T>
inline std::ostream &operator<<( std::ostream &os, const irr::core::position2d<T> &pos)
{	return os << "(" << pos.X << "," << pos.Y << ")";	}

inline std::ostream &operator<<(std::ostream &os, const irr::core::quaternion &q)
{	return os << "(" << q.W << "," << q.X << "," << q.Y << "," << q.Z << ")"; }

inline std::ostream &operator<<(std::ostream &os, const irr::video::SColor &c)
{	return os << "(" << os.hex << c.color << os.dec << ")"; }

#endif
So if you wanted to serialize the ISceneNode class just write:

std::cout << AbsoluteTransformation << RelativeRotation << RelativeScale; etc. (derefercing pointers would pose problems I suppose)

Anyways I know irrlicht doesn't depend on std:: at all but thought this might help.
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Hey, thanks for posting. Personally, I don't use streams, but this can be definitively useful anyway. About the serialization: I've planned to take a slight unusual approach to this, just like most stuff in Irrlicht is done 'a little bit' different. But lets see :)
FlyHigh
Posts: 111
Joined: Wed Mar 23, 2005 12:05 am

Post by FlyHigh »

Thats ok, theres an istream counterpart but its pretty much identical except << become >> and the brackets become is.ignore().

A slighty unusual approach to serialization? I'm intrigued...
Post Reply