/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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_READERWRITER #define OSGDB_READERWRITER 1 #include #include #include #include #include #include #include namespace osgDB { class Archive; /** pure virtual base class for reading and writing of non native formats. */ class OSGDB_EXPORT ReaderWriter : public osg::Object { public: ReaderWriter() {} ReaderWriter(const ReaderWriter& rw,const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):Object(rw,copyop) {} virtual ~ReaderWriter(); META_Object(osgDB,ReaderWriter); virtual bool acceptsExtension(const std::string& /*extension*/) { return false; } /** Options base class used for passing options into plugins to control their operation.*/ class Options : public osg::Referenced { public: Options() {} Options(const std::string& str):_str(str) {} /** Set the general Options string.*/ void setOptionString(const std::string& str) { _str = str; } /** Get the general Options string.*/ const std::string& getOptionString() const { return _str; } /** Set the database path to use a hint of where to look when loading models.*/ void setDatabasePath(const std::string& str) { _databasePath = str; } /** Get the database path which is used a hint of where to look when loading models.*/ const std::string& getDatabasePath() const { return _databasePath; } protected: virtual ~Options() {} std::string _str; std::string _databasePath; }; class OSGDB_EXPORT ReadResult { public: enum ReadStatus { FILE_NOT_HANDLED, FILE_NOT_FOUND, FILE_LOADED, FILE_LOADED_FROM_CACHE, ERROR_IN_READING_FILE }; ReadResult(ReadStatus status=FILE_NOT_HANDLED):_status(status) {} ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {} ReadResult(osg::Object* obj, ReadStatus status=FILE_LOADED):_status(status),_object(obj) {} ReadResult(const ReadResult& rr):_status(rr._status),_message(rr._message),_object(rr._object) {} ReadResult& operator = (const ReadResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message;_object=rr._object; return *this; } osg::Object* getObject(); osg::Image* getImage(); osg::HeightField* getHeightField(); osg::Node* getNode(); osgDB::Archive* getArchive(); bool validObject() { return _object.valid(); } bool validImage() { return getImage()!=0; } bool validHeightField() { return getHeightField()!=0; } bool validNode() { return getNode()!=0; } bool validArchive() { return getArchive()!=0; } osg::Object* takeObject(); osg::Image* takeImage(); osg::HeightField* takeHeightField(); osg::Node* takeNode(); osgDB::Archive* takeArchive(); const std::string& message() const { return _message; } ReadStatus status() const { return _status; } bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; } bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; } bool error() const { return _status==ERROR_IN_READING_FILE; } bool notHandled() const { return _status==FILE_NOT_HANDLED; } bool notFound() const { return _status==FILE_NOT_FOUND; } protected: ReadStatus _status; std::string _message; osg::ref_ptr _object; }; class WriteResult { public: enum WriteStatus { FILE_NOT_HANDLED, FILE_SAVED, ERROR_IN_WRITING_FILE }; WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {} WriteResult(const std::string& m):_status(ERROR_IN_WRITING_FILE),_message(m) {} WriteResult(const WriteResult& rr):_status(rr._status),_message(rr._message) {} WriteResult& operator = (const WriteResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message; return *this; } const std::string& message() const { return _message; } WriteStatus status() const { return _status; } bool success() const { return _status==FILE_SAVED; } bool error() const { return _status==ERROR_IN_WRITING_FILE; } bool notHandled() const { return _status==FILE_NOT_HANDLED; } protected: WriteStatus _status; std::string _message; }; enum ArchiveStatus { READ, WRITE, CREATE }; #define SERIALIZER() OpenThreads::ScopedLock lock(_serializerMutex) /** open an archive for reading, writing or or to create an empty archive for writing to.*/ virtual ReadResult threadSafe_openArchive(const std::string& fileName,ArchiveStatus status, unsigned int indexBlockSize=4096, const Options* options=NULL) { SERIALIZER(); return openArchive(fileName,status, indexBlockSize, options); } /** open an archive for reading.*/ virtual ReadResult threadSafe_openArchive(std::istream& fin,const Options* options=NULL) { SERIALIZER(); return openArchive(fin, options); } virtual ReadResult threadSafe_readObject(const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return readObject(fileName,options); } virtual ReadResult threadSafe_readImage(const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return readImage(fileName,options); } virtual ReadResult threadSafe_readHeightField(const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return readHeightField(fileName,options); } virtual ReadResult threadSafe_readNode(const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return readNode(fileName,options); } virtual WriteResult threadSafe_writeObject(const osg::Object& obj,const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return writeObject(obj, fileName,options); } virtual WriteResult threadSafe_writeImage(const osg::Image& image,const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return writeObject(image, fileName,options); } virtual WriteResult threadSafe_writeHeightField(const osg::HeightField& heightField,const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return writeHeightField(heightField, fileName,options); } virtual WriteResult threadSafe_writeNode(const osg::Node& node,const std::string& fileName,const Options* options=NULL) { SERIALIZER(); return writeNode(node, fileName,options); } virtual ReadResult threadSafe_readObject(std::istream& fin,const Options* options=NULL) { SERIALIZER(); return readObject(fin,options); } virtual ReadResult threadSafe_readImage(std::istream& fin,const Options* options=NULL) { SERIALIZER(); return readImage(fin,options); } virtual ReadResult threadSafe_readHeightField(std::istream& fin,const Options* options=NULL) { SERIALIZER(); return readHeightField(fin,options); } virtual ReadResult threadSafe_readNode(std::istream& fin,const Options* options=NULL) { SERIALIZER(); return readNode(fin,options); } virtual WriteResult threadSafe_writeObject(const osg::Object& obj,std::ostream& fout,const Options* options=NULL) { SERIALIZER(); return writeObject(obj, fout, options); } virtual WriteResult threadSafe_writeImage(const osg::Image& image,std::ostream& fout,const Options* options=NULL) { SERIALIZER(); return writeImage(image, fout, options); } virtual WriteResult threadSafe_writeHeightField(const osg::HeightField& heightField,std::ostream& fout,const Options* options=NULL) { SERIALIZER(); return writeHeightField(heightField, fout, options); } virtual WriteResult threadSafe_writeNode(const osg::Node& node,std::ostream& fout,const Options* options=NULL) { SERIALIZER(); return writeNode(node, fout, options); } protected: /** open an archive for reading, writing or or to create an empty archive for writing to.*/ virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } /** open an archive for reading.*/ virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readHeightField(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readNode(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) { return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual ReadResult readObject(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readImage(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readHeightField(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readNode(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual WriteResult writeObject(const osg::Object& /*obj*/,std::ostream& /*fout*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeImage(const osg::Image& /*image*/,std::ostream& /*fout*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,std::ostream& /*fout*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeNode(const osg::Node& /*node*/,std::ostream& /*fout*/,const Options* =NULL) { return WriteResult(WriteResult::FILE_NOT_HANDLED); } protected: osgDB::ReentrantMutex _serializerMutex; }; } #endif // OSGDB_READERWRITER