OpenSceneGraph/include/osgDB/Input

332 lines
10 KiB
Plaintext
Raw Normal View History

/* -*-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_INPUT
#define OSGDB_INPUT 1
#include <osg/Image>
#include <osg/Shader>
#include <osg/Node>
#include <osg/Drawable>
#include <osg/StateAttribute>
#include <osg/ArgumentParser>
#include <osgDB/ReaderWriter>
#include <osgDB/Options>
#include <map>
#include <string>
namespace osgDB {
/** basic structure for custom runtime inheritance checking */
struct basic_type_wrapper {
virtual ~basic_type_wrapper() {}
virtual bool matches(const osg::Object *proto) const = 0;
};
/** a class template that checks inheritance between a given
Object's class and a class defined at compile time through
the template parameter T.
This is used in conjunction with readObjectOfType() to
specify an abstract class as reference type.
**/
template<class T>
struct type_wrapper: basic_type_wrapper {
bool matches(const osg::Object *proto) const
{
return dynamic_cast<const T*>(proto) != 0;
}
};
/** deprecated. */
class OSGDB_EXPORT Field
{
public:
enum {
MIN_CACHE_SIZE = 256
};
Field();
Field(const Field& field);
virtual ~Field();
virtual Field& operator = (const Field& ic);
void reset();
void addChar(char c);
int getNoCharacters() const { return _fieldCacheSize; }
void setWithinQuotes(bool withinQuotes=true);
bool getWithinQuotes();
void setNoNestedBrackets(int no);
int getNoNestedBrackets();
enum FieldType
{
OPEN_BRACKET,
CLOSE_BRACKET,
STRING,
WORD,
REAL,
INTEGER,
BLANK,
UNINITIALISED
};
FieldType getFieldType() const;
bool isValid() const;
bool isOpenBracket() const;
bool isCloseBracket() const;
bool isWord() const;
bool matchWord(const char* str) const;
bool matchWord(const char* str,int noCharacters) const;
bool isString() const;
bool matchString(const char* str) const;
bool matchString(const char* str,int noCharacters) const;
bool isQuotedString() const;
const char* getStr() const;
char* takeStr();
bool isInt() const;
bool matchInt(int i) const;
bool getInt(int& i) const;
bool isUInt() const;
bool matchUInt(unsigned int i) const;
bool getUInt(unsigned int& i) const;
bool isFloat() const;
bool matchFloat(float f) const;
bool getFloat(float& f) const;
bool getFloat(double& f) const;
static FieldType calculateFieldType(const char* str,bool withinQuotes=false);
protected:
void _init();
void _free();
void _copy(const Field& ic);
int _fieldCacheCapacity;
int _fieldCacheSize;
char* _fieldCache;
mutable FieldType _fieldType;
bool _withinQuotes;
int _noNestedBrackets;
};
/** deprecated. */
class OSGDB_EXPORT FieldReader
{
public:
FieldReader();
FieldReader(const FieldReader& ic);
virtual ~FieldReader();
virtual FieldReader& operator = (const FieldReader& ic);
void attach(std::istream* input);
void detach();
virtual bool eof() const;
bool readField(Field& fieldPtr);
void ignoreField();
/** no of unmatched `{' encountered so far in file*/
int getNoNestedBrackets() const;
private:
bool _readField(Field* fieldPtr);
void _init();
void _free();
void _copy(const FieldReader& ic);
std::istream* _fin;
bool _eof;
bool findStartOfNextField();
int _noNestedBrackets;
bool _delimiterEatLookUp[256];
bool _delimiterKeepLookUp[256];
};
/** deprecated. */
class OSGDB_EXPORT FieldReaderIterator
{
public:
enum {
MINIMUM_FIELD_READER_QUEUE_SIZE = 10
};
FieldReaderIterator();
FieldReaderIterator(const FieldReaderIterator& ic);
virtual ~FieldReaderIterator();
FieldReaderIterator& operator = (const FieldReaderIterator& ic);
void attach(std::istream* input);
void detach();
virtual bool eof() const;
FieldReader& getFieldReader() { return _reader; }
void insert(int pos,Field* field);
void insert(int pos,const char* str);
Field& operator [] (int pos);
Field& field (int pos);
FieldReaderIterator& operator ++ ();
FieldReaderIterator& operator += (int no);
/** increments the iterator of the next simple field or
* whole block if the current field[0] is an open bracket */
void advanceOverCurrentFieldOrBlock();
void advanceToEndOfCurrentBlock();
void advanceToEndOfBlock(int noNestBrackets);
bool matchSequence(const char* str);
bool readSequence(const char* keyword,std::string& value);
bool readSequence(const char* keyword,unsigned int& value);
bool readSequence(const char* keyword,int& value);
bool readSequence(const char* keyword,float& value);
bool readSequence(const char* keyword,osg::Vec2f& value);
bool readSequence(const char* keyword,osg::Vec3f& value);
bool readSequence(const char* keyword,osg::Vec4f& value);
bool readSequence(const char* keyword,osg::Vec2d& value);
bool readSequence(const char* keyword,osg::Vec3d& value);
bool readSequence(const char* keyword,osg::Vec4d& value);
bool readSequence(std::string& value);
bool readSequence(unsigned int& value);
bool readSequence(int& value);
bool readSequence(float& value);
bool readSequence(osg::Vec2f& value);
bool readSequence(osg::Vec3f& value);
bool readSequence(osg::Vec4f& value);
bool readSequence(osg::Vec2d& value);
bool readSequence(osg::Vec3d& value);
bool readSequence(osg::Vec4d& value);
private:
void _init();
void _free();
void _copy(const FieldReaderIterator& ic);
FieldReader _reader;
Field _blank;
Field* _previousField;
Field** _fieldQueue;
int _fieldQueueSize;
int _fieldQueueCapacity;
};
/** deprecated. */
class OSGDB_EXPORT Input : public FieldReaderIterator
{
public:
Input();
virtual ~Input();
void setOptions(const Options* options) { _options = options; }
const Options* getOptions() const { return _options.get(); }
virtual osg::Object* readObjectOfType(const osg::Object& compObj);
virtual osg::Object* readObjectOfType(const basic_type_wrapper &btw);
template<typename T>
inline T* readObjectOfType()
{
return dynamic_cast<T*>(readObjectOfType(osgDB::type_wrapper<T>()));
}
virtual osg::Object* readObject();
virtual osg::Image* readImage();
virtual osg::Drawable* readDrawable();
virtual osg::StateAttribute* readStateAttribute();
virtual osg::Uniform* readUniform();
virtual osg::Node* readNode();
virtual osg::Shader* readShader();
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
virtual osg::ref_ptr<osg::Object> readObject(const std::string& fileName);
virtual osg::ref_ptr<osg::Image> readImage(const std::string& fileName);
virtual osg::ref_ptr<osg::Node> readNode(const std::string& fileName);
virtual osg::ref_ptr<osg::Shader> readShader(const std::string& fileName);
virtual osg::Object* getObjectForUniqueID(const std::string& uniqueID);
virtual void registerUniqueIDForObject(const std::string& uniqueID,osg::Object* obj);
typedef osg::ArgumentParser::Parameter Parameter;
bool read(Parameter value1);
bool read(Parameter value1, Parameter value2);
bool read(Parameter value1, Parameter value2, Parameter value3);
bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4);
bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
bool read(const char* str);
bool read(const char* str, Parameter value1);
bool read(const char* str, Parameter value1, Parameter value2);
bool read(const char* str, Parameter value1, Parameter value2, Parameter value3);
bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
private:
2003-03-11 00:40:26 +08:00
typedef std::map< std::string, osg::ref_ptr<osg::Object> > UniqueIDToObjectMapping;
UniqueIDToObjectMapping _uniqueIDToObjectMap;
osg::ref_ptr<const Options> _options;
};
}
#endif // __SG_INPUT_H