没有理清的一段代码

 

看了半天,没看明白是怎么实现的!

 

ReaderWriter::ReadResult readNode(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true)
{
    ReaderWriter::ReadResult result;
    if (options && options->getReadFileCallback()) result = options->getReadFileCallback()->readNode(fileName,options);
    else if (_readFileCallback.valid()) result = _readFileCallback->readNode(fileName,options);
    else result = readNodeImplementation(fileName,options);

    if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);

    return result;
}

 

 

/* -*-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();

        virtual osg::Object*         readObject(const std::string& fileName);
        virtual osg::Image*          readImage(const std::string& fileName);
        virtual osg::Node*           readNode(const std::string& fileName);
        virtual 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:

        typedef std::map< std::string, osg::ref_ptr<osg::Object> > UniqueIDToObjectMapping;
        UniqueIDToObjectMapping _uniqueIDToObjectMap;

        osg::ref_ptr<const Options> _options;

};

}

#endif                                            // __SG_INPUT_H

 

 

/* -*-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.
*/
#include <osg/Object>

#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/Input>

using namespace osgDB;

Input::Input()
{
}


Input::~Input()
{
}


osg::Object* Input::getObjectForUniqueID(const std::string& uniqueID)
{
    UniqueIDToObjectMapping::iterator fitr = _uniqueIDToObjectMap.find(uniqueID);
    if (fitr != _uniqueIDToObjectMap.end()) return (*fitr).second.get();
    else return NULL;
}


void Input::registerUniqueIDForObject(const std::string& uniqueID,osg::Object* obj)
{
    _uniqueIDToObjectMap[uniqueID] = obj;
}


osg::Object* Input::readObjectOfType(const osg::Object& compObj)
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readObjectOfType(compObj,*this);
}

osg::Object* Input::readObjectOfType(const basic_type_wrapper &btw)
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readObjectOfType(btw,*this);
}

osg::Object* Input::readObject()
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readObject(*this);
}


osg::Image*  Input::readImage()
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readImage(*this);
}

osg::Drawable* Input::readDrawable()
{
    osg::Drawable* drawable = Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readDrawable(*this);
    osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0;
    if (geometry && geometry->containsDeprecatedData()) geometry->fixDeprecatedData();
    return drawable;
}

osg::StateAttribute* Input::readStateAttribute()
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readStateAttribute(*this);
}
osg::Uniform* Input::readUniform()
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readUniform(*this);
}

osg::Node* Input::readNode()
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readNode(*this);
}

osg::Object* Input::readObject(const std::string& fileName)
{
    return readObjectFile(fileName,_options.get());
}

osg::Shader*  Input::readShader()
{
    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->readShader(*this);
}

osg::Image*  Input::readImage(const std::string& fileName)
{
    return readImageFile(fileName,_options.get());
}

osg::Node* Input::readNode(const std::string& fileName)
{
    return readNodeFile(fileName,_options.get());
}

osg::Shader*  Input::readShader(const std::string& fileName)
{
    return readShaderFile(fileName,_options.get());
}

bool Input::read(Parameter value1)
{
    if (value1.valid((*this)[0].getStr()))
    {
        value1.assign((*this)[0].getStr());
        (*this) += 1;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        (*this) += 2;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2, Parameter value3)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()) &&
        value3.valid((*this)[2].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        value3.assign((*this)[2].getStr());
        (*this) += 3;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2, Parameter value3, Parameter value4)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()) &&
        value3.valid((*this)[2].getStr()) &&
        value4.valid((*this)[3].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        value3.assign((*this)[2].getStr());
        value4.assign((*this)[3].getStr());
        (*this) += 4;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()) &&
        value3.valid((*this)[2].getStr()) &&
        value4.valid((*this)[3].getStr()) &&
        value5.valid((*this)[4].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        value3.assign((*this)[2].getStr());
        value4.assign((*this)[3].getStr());
        value5.assign((*this)[4].getStr());
        (*this) += 5;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()) &&
        value3.valid((*this)[2].getStr()) &&
        value4.valid((*this)[3].getStr()) &&
        value5.valid((*this)[4].getStr()) &&
        value6.valid((*this)[5].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        value3.assign((*this)[2].getStr());
        value4.assign((*this)[3].getStr());
        value5.assign((*this)[4].getStr());
        value6.assign((*this)[5].getStr());
        (*this) += 6;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()) &&
        value3.valid((*this)[2].getStr()) &&
        value4.valid((*this)[3].getStr()) &&
        value5.valid((*this)[4].getStr()) &&
        value6.valid((*this)[5].getStr()) &&
        value7.valid((*this)[6].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        value3.assign((*this)[2].getStr());
        value4.assign((*this)[3].getStr());
        value5.assign((*this)[4].getStr());
        value6.assign((*this)[5].getStr());
        value7.assign((*this)[6].getStr());
        (*this) += 7;
        return true;
    }
    else return false;
}

bool Input::read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8)
{
    if (value1.valid((*this)[0].getStr()) &&
        value2.valid((*this)[1].getStr()) &&
        value3.valid((*this)[2].getStr()) &&
        value4.valid((*this)[3].getStr()) &&
        value5.valid((*this)[4].getStr()) &&
        value6.valid((*this)[5].getStr()) &&
        value7.valid((*this)[6].getStr()) &&
        value8.valid((*this)[7].getStr()))
    {
        value1.assign((*this)[0].getStr());
        value2.assign((*this)[1].getStr());
        value3.assign((*this)[2].getStr());
        value4.assign((*this)[3].getStr());
        value5.assign((*this)[4].getStr());
        value6.assign((*this)[5].getStr());
        value7.assign((*this)[6].getStr());
        value8.assign((*this)[7].getStr());
        (*this) += 8;
        return true;
    }
    else return false;
}

bool Input::read(const char* str)
{
    if ((*this)[0].matchWord(str))
    {
        (*this) += 1;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1)
{
    if ((*this)[0].matchWord(str) && value1.valid((*this)[1].getStr()))
    {
        value1.assign((*this)[1].getStr());
        (*this) += 2;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        (*this) += 3;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()) &&
        value3.valid((*this)[3].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        value3.assign((*this)[3].getStr());
        (*this) += 4;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()) &&
        value3.valid((*this)[3].getStr()) &&
        value4.valid((*this)[4].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        value3.assign((*this)[3].getStr());
        value4.assign((*this)[4].getStr());
        (*this) += 5;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()) &&
        value3.valid((*this)[3].getStr()) &&
        value4.valid((*this)[4].getStr()) &&
        value5.valid((*this)[5].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        value3.assign((*this)[3].getStr());
        value4.assign((*this)[4].getStr());
        value5.assign((*this)[5].getStr());
        (*this) += 6;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()) &&
        value3.valid((*this)[3].getStr()) &&
        value4.valid((*this)[4].getStr()) &&
        value5.valid((*this)[5].getStr()) &&
        value6.valid((*this)[6].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        value3.assign((*this)[3].getStr());
        value4.assign((*this)[4].getStr());
        value5.assign((*this)[5].getStr());
        value6.assign((*this)[6].getStr());
        (*this) += 7;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()) &&
        value3.valid((*this)[3].getStr()) &&
        value4.valid((*this)[4].getStr()) &&
        value5.valid((*this)[5].getStr()) &&
        value6.valid((*this)[6].getStr()) &&
        value7.valid((*this)[7].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        value3.assign((*this)[3].getStr());
        value4.assign((*this)[4].getStr());
        value5.assign((*this)[5].getStr());
        value6.assign((*this)[6].getStr());
        value7.assign((*this)[7].getStr());
        (*this) += 8;
        return true;
    }
    else return false;
}

bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8)
{
    if ((*this)[0].matchWord(str) &&
        value1.valid((*this)[1].getStr()) &&
        value2.valid((*this)[2].getStr()) &&
        value3.valid((*this)[3].getStr()) &&
        value4.valid((*this)[4].getStr()) &&
        value5.valid((*this)[5].getStr()) &&
        value6.valid((*this)[6].getStr()) &&
        value7.valid((*this)[7].getStr()) &&
        value8.valid((*this)[8].getStr()))
    {
        value1.assign((*this)[1].getStr());
        value2.assign((*this)[2].getStr());
        value3.assign((*this)[3].getStr());
        value4.assign((*this)[4].getStr());
        value5.assign((*this)[5].getStr());
        value6.assign((*this)[6].getStr());
        value7.assign((*this)[7].getStr());
        value8.assign((*this)[8].getStr());
        (*this) += 9;
        return true;
    }
    else return false;
}

 

 

#################################3

posted @ 2021-09-02 21:00  西北逍遥  阅读(66)  评论(0编辑  收藏  举报