Jean-Yves Didier

Serialisation en chaîne au point.

......@@ -45,5 +45,10 @@ ARCSAbstractComponent* InventorFamily::instanciate(QString type)
void InventorFamily::destroy(ARCSAbstractComponent* cmp)
{
if (cmp->getFamily() == name())
{
InventorComponent* ic = dynamic_cast<InventorComponent*>(cmp);
if (ic)
delete ic;
}
}
......
......@@ -9,6 +9,7 @@
#include <iostream>
#include <cstring>
// black magic is fun !
/////////////////////////////////////////////////////////////////////////
......
......@@ -15,43 +15,60 @@
#include <osg/Matrix>
#include <osg/Matrixf>
#include <osg/Matrixd>
#include <osgDB/ObjectWrapper>
#include <string>
#include <iostream>
#include "osgstringserializer.h"
class OSGSlotWrapper : public QObject
{
Q_OBJECT
public:
explicit OSGSlotWrapper(QObject *parent = 0);
explicit OSGSlotWrapper(QString s,osgDB::ObjectWrapper* wrapper,osg::Object* osgObject, QObject *parent = 0)
: QObject(parent), container(osgObject), osgWrapper(wrapper), slotName(s) {}
public slots:
void call(osg::Object* data);
void call(osg::Image* data);
void call(bool data);
void call(char data);
void call(unsigned char data);
void call(short data);
void call(unsigned short data);
void call(int data);
void call(unsigned int data);
void call(float data);
void call(double data);
void call(osg::Vec2f data);
void call(osg::Vec2d data);
void call(osg::Vec3d data);
void call(osg::Vec3f data);
void call(osg::Vec4f data);
void call(osg::Vec4d data);
void call(osg::Quat data);
void call(osg::Plane data);
void call(osg::Matrixf data);
void call(osg::Matrixd data);
void call(osg::Object* data) { call<osg::Object*>(data); }
void call(osg::Image* data) { call<osg::Image*>(data); }
void call(bool data) { call<bool>(data); }
void call(char data) { call<char>(data); }
void call(unsigned char data) { call<unsigned char>(data); }
void call(short data) { call<short>(data); }
void call(unsigned short data) { call<unsigned short>(data); }
void call(int data) { call<int>(data); }
void call(unsigned int data) { call<unsigned int>(data); }
void call(float data) { call<float>(data); }
void call(double data) { call<double>(data); }
void call(osg::Vec2f data) { call<osg::Vec2f>(data); }
void call(osg::Vec2d data) { call<osg::Vec2d>(data); }
void call(osg::Vec3d data) { call<osg::Vec3d>(data); }
void call(osg::Vec3f data) { call<osg::Vec3f>(data); }
void call(osg::Vec4f data) { call<osg::Vec4f>(data); }
void call(osg::Vec4d data) { call<osg::Vec4d>(data); }
void call(osg::Quat data) { call<osg::Quat>(data); }
void call(osg::Plane data) { call<osg::Plane>(data); }
void call(osg::Matrixf data) { call<osg::Matrixf>(data); }
void call(osg::Matrixd data) { call<osg::Matrixd>(data); }
//void call(osg::Matrix data);
//void call(GLenum data);
void call(std::string data);
void call(std::string data) { call<std::string>(data); }
private:
template<typename T> void call(T&);
osg::Object* container;
osgDB::ObjectWrapper* osgWrapper;
QString slotName;
};
template<typename T> void OSGSlotWrapper::call(T& value)
{
OSGStringSerializer::getInstance()->updateObject<T>(container,osgWrapper->getSerializer(qPrintable(slotName)),value );
}
#endif // __OSGSLOTWRAPPER_H__
......
#include "osgstringserializer.h"
#include <QMutexLocker>
#include <osgDB/Registry>
OSGStringSerializer* OSGStringSerializer::instance = 0;
OSGStringSerializer *OSGStringSerializer::getInstance()
{
if (!instance)
instance = new OSGStringSerializer();
return instance;
}
osg::ref_ptr<osg::Object> OSGStringSerializer::parseString(QString s)
{
QMutexLocker locker(&mutex);
source.str(qPrintable(s));
osgDB::ReaderWriter::ReadResult res = osgRW->readObject(source);
source.clear();
if (res.error())
{
std::cout << res.message() << std::endl;
return osg::ref_ptr<osg::Object>();
}
return res.getObject();
}
QString OSGStringSerializer::toString(osg::ref_ptr<osg::Object> target)
{
QMutexLocker locker(&mutex);
source.str("");
osgRW->writeObject(*target, source);
QString s = source.str().data();
source.clear();
return s;
}
OSGStringSerializer::OSGStringSerializer() :
binaryInputStream(0),binaryOutputStream(0),
inputStream(0),outputStream(0)
{
bip = new BinaryInputIterator(&source);
bop = new BinaryOutputIterator(&source);
binaryInputStream.setInputIterator(bip);
binaryOutputStream.setOutputIterator(bop);
osgRW = osgDB::Registry::instance()->getReaderWriterForExtension("osg");
}
OSGStringSerializer::~OSGStringSerializer()
{
delete bip;
delete bop;
}
#ifndef __OSGSTRINGSERIALIZER_H__
#define __OSGSTRINGSERIALIZER_H__
#include <QString>
#include <osg/Object>
#include <osgDB/ObjectWrapper>
#include <string>
#include <iostream>
#include <sstream>
#include <osgDB/InputStream>
#include <osgDB/OutputStream>
#include <osgDB/ReaderWriter>
#include "BinaryStreamOperator.h"
#include <QMutex>
class OSGStringSerializer
{
public:
static OSGStringSerializer* getInstance();
osg::ref_ptr<osg::Object> parseString(QString s);
QString toString(osg::ref_ptr<osg::Object> target);
template<typename T> bool updateObject(osg::ref_ptr<osg::Object> target, osgDB::BaseSerializer* serializer, T& value);
// QString
private:
static OSGStringSerializer* instance;
OSGStringSerializer();
~OSGStringSerializer();
std::stringstream source;
osgDB::InputStream binaryInputStream;
osgDB::OutputStream binaryOutputStream;
osgDB::InputStream inputStream;
osgDB::OutputStream outputStream;
BinaryInputIterator* bip;
BinaryOutputIterator* bop;
osgDB::ReaderWriter* osgRW;
QMutex mutex;
};
template <typename T> bool OSGStringSerializer::updateObject(osg::ref_ptr<osg::Object> target,
osgDB::BaseSerializer *serializer, T& value)
{
QMutexLocker locker(&mutex);
source.str("");
binaryOutputStream << value;
bool res = serializer->read(binaryInputStream,*target);
source.clear();
return res;
}
#endif // __OSGSTRINGSERIALIZER_H__
#ifndef __OSGTYPES_H__
#define __OSGTYPES_H__
#include <arcs/arcslibtoolkit.h>
#include <osg/Image>
#include <osg/Vec2d>
#include <osg/Vec2f>
#include <osg/Vec3d>
#include <osg/Vec3f>
#include <osg/Vec4f>
#include <osg/Vec4d>
#include <osg/Quat>
#include <osg/Plane>
#include <osg/Matrix>
#include <osg/Matrixf>
#include <osg/Matrixd>
#include <typeinfo>
template<typename T> class OSGType : public ARCSTypeFactoryTemplate<T>
{
public:
virtual QString getTypeName() const; // const { return QString; };
virtual bool isInternal() const { return false; }
protected:
virtual T parse(QString s);
virtual QString serialize(T obj);
};
template<typename T> QString OSGType<T>::getTypeName() const
{
return QString(typeid(T).name());
}
template<typename T> T OSGType<T>::parse(QString s)
{
}
#endif // __OSGTYPES_H__
......