OpenSceneGraph/include/osgDB/DotOsgWrapper
2010-01-21 09:25:45 +00:00

123 lines
4.2 KiB
C++

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGDB_DOTOSGWRAPPER
#define OSGDB_DOTOSGWRAPPER 1
#include <osg/ref_ptr>
#include <osg/Object>
#include <osgDB/Input>
#include <osgDB/Output>
#include <string>
#include <vector>
namespace osgDB {
/** Wrapper class for specifying read and write functions for extending
* the .osg file format. */
class OSGDB_EXPORT DotOsgWrapper : public osg::Referenced
{
public:
typedef std::vector<std::string> Associates;
typedef bool (*ReadFunc)(osg::Object&,osgDB::Input&);
typedef bool (*WriteFunc)(const osg::Object&,osgDB::Output&);
enum ReadWriteMode
{
READ_AND_WRITE,
READ_ONLY
};
DotOsgWrapper(osg::Object* proto,
const std::string& name,
const std::string& associates,
ReadFunc readFunc,
WriteFunc writeFunc,
ReadWriteMode readWriteMode=READ_AND_WRITE);
inline const osg::Object* getPrototype() const { return _prototype.get(); }
inline const std::string& getName() const { return _name; }
inline const Associates& getAssociates() const { return _associates; }
inline ReadFunc getReadFunc() const { return _readFunc; }
inline WriteFunc getWriteFunc() const { return _writeFunc; }
inline ReadWriteMode getReadWriteMode() const { return _readWriteMode; }
protected:
/// protected to prevent inappropriate creation of wrappers.
DotOsgWrapper() {}
/// protected to prevent inappropriate creation of wrappers.
DotOsgWrapper(DotOsgWrapper&):osg::Referenced() {}
/// protected to prevent wrapper being created on stack.
virtual ~DotOsgWrapper() {}
osg::ref_ptr<osg::Object> _prototype;
std::string _name;
Associates _associates;
ReadFunc _readFunc;
WriteFunc _writeFunc;
ReadWriteMode _readWriteMode;
};
/** Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
class OSGDB_EXPORT RegisterDotOsgWrapperProxy
{
public:
RegisterDotOsgWrapperProxy(osg::Object* proto,
const std::string& name,
const std::string& associates,
DotOsgWrapper::ReadFunc readFunc,
DotOsgWrapper::WriteFunc writeFunc,
DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE);
~RegisterDotOsgWrapperProxy();
protected:
osg::ref_ptr<DotOsgWrapper> _wrapper;
};
template<class T>
class OSGDB_EXPORT TemplateRegisterDotOsgWrapperProxy : public RegisterDotOsgWrapperProxy, public T
{
public:
TemplateRegisterDotOsgWrapperProxy(osg::Object* proto,
const std::string& name,
const std::string& associates,
DotOsgWrapper::ReadFunc readFunc,
DotOsgWrapper::WriteFunc writeFunc,
DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE):
RegisterDotOsgWrapperProxy(proto, name, associates, readFunc, writeFunc, readWriteMode) {}
};
#define REGISTER_DOTOSGWRAPPER(classname) \
extern "C" void dotosgwrapper_##classname(void) {} \
static osgDB::RegisterDotOsgWrapperProxy dotosgwrapper_proxy_##classname
}
#endif