OpenSceneGraph/include/osg/ProxyNode
Robert Osfield dd996a3289 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 13:42:19 +00:00

148 lines
6.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 OSG_ProxyNode
#define OSG_ProxyNode 1
#include <osg/Group>
namespace osg {
/** ProxyNode.
*/
class OSG_EXPORT ProxyNode : public Group
{
public :
ProxyNode();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ProxyNode(const ProxyNode&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
META_Node(osg, ProxyNode);
typedef osg::BoundingSphere::vec_type vec_type;
typedef osg::BoundingSphere::value_type value_type;
virtual void traverse(NodeVisitor& nv);
using osg::Group::addChild;
virtual bool addChild(Node *child);
virtual bool addChild(Node *child, const std::string& filename);
template<class T> bool addChild( const ref_ptr<T>& child, const std::string& filename) { return addChild(child.get(), filename); }
virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
/** Set the optional database osgDB::Options object to use when loading children.*/
void setDatabaseOptions(osg::Referenced* options) { _databaseOptions = options; }
/** Get the optional database osgDB::Options object used when loading children.*/
osg::Referenced* getDatabaseOptions() { return _databaseOptions.get(); }
/** Get the optional database osgDB::Options object used when loading children.*/
const osg::Referenced* getDatabaseOptions() const { return _databaseOptions.get(); }
/** Set the database path to prepend to children's filenames.*/
void setDatabasePath(const std::string& path);
/** Get the database path used to prepend to children's filenames.*/
inline const std::string& getDatabasePath() const { return _databasePath; }
void setFileName(unsigned int childNo, const std::string& filename) { expandFileNameListTo(childNo); _filenameList[childNo].first=filename; }
const std::string& getFileName(unsigned int childNo) const { return _filenameList[childNo].first; }
unsigned int getNumFileNames() const { return _filenameList.size(); }
/** Return the DatabaseRequest object used by the DatabasePager to keep track of file load requests
* being carried out on behalf of the DatabasePager.
* Note, in normal OSG usage you should not set this value yourself, as this will be managed by
* the osgDB::DatabasePager.*/
osg::ref_ptr<osg::Referenced>& getDatabaseRequest(unsigned int childNo) { return _filenameList[childNo].second; }
/** Return the const DatabaseRequest object.*/
const osg::ref_ptr<osg::Referenced>& getDatabaseRequest(unsigned int childNo) const { return _filenameList[childNo].second; }
/** Modes which control how the center of object should be determined when computing which child is active.*/
enum CenterMode
{
USE_BOUNDING_SPHERE_CENTER,
USER_DEFINED_CENTER,
UNION_OF_BOUNDING_SPHERE_AND_USER_DEFINED
};
/** Set how the center of object should be determined when computing which child is active.*/
void setCenterMode(CenterMode mode) { _centerMode=mode; }
/** Get how the center of object should be determined when computing which child is active.*/
CenterMode getCenterMode() const { return _centerMode; }
/** Modes which control how the proxynode external reference are loaded.*/
enum LoadingExternalReferenceMode
{
LOAD_IMMEDIATELY,
DEFER_LOADING_TO_DATABASE_PAGER,
NO_AUTOMATIC_LOADING
};
/** Set how the child loading is done.*/
void setLoadingExternalReferenceMode(LoadingExternalReferenceMode mode) { _loadingExtReference=mode; }
/** Get the loading mode.*/
LoadingExternalReferenceMode getLoadingExternalReferenceMode() const { return _loadingExtReference; }
/** Sets the object-space point which defines the center of the osg::ProxyNode.
Center is affected by any transforms in the hierarchy above the osg::ProxyNode.*/
inline void setCenter(const vec_type& center) { if (_centerMode!=UNION_OF_BOUNDING_SPHERE_AND_USER_DEFINED) { _centerMode=USER_DEFINED_CENTER; } _userDefinedCenter = center; }
/** Return the ProxyNode center point. */
inline const vec_type& getCenter() const { if ((_centerMode==USER_DEFINED_CENTER)||(_centerMode==UNION_OF_BOUNDING_SPHERE_AND_USER_DEFINED)) return _userDefinedCenter; else return getBound().center(); }
/** Set the object-space reference radius of the volume enclosed by the ProxyNode.
* Used to determine the bounding sphere of the ProxyNode in the absence of any children.*/
inline void setRadius(value_type radius) { _radius = radius; }
/** Get the object-space radius of the volume enclosed by the ProxyNode.*/
inline value_type getRadius() const { return _radius; }
virtual BoundingSphere computeBound() const;
protected :
virtual ~ProxyNode() {}
void expandFileNameListTo(unsigned int pos);
typedef std::pair< std::string, osg::ref_ptr<osg::Referenced> > FileNameDatabaseRequestPair;
typedef std::vector<FileNameDatabaseRequestPair> FileNameDatabaseRequestList;
FileNameDatabaseRequestList _filenameList;
ref_ptr<Referenced> _databaseOptions;
std::string _databasePath;
LoadingExternalReferenceMode _loadingExtReference;
CenterMode _centerMode;
vec_type _userDefinedCenter;
value_type _radius;
};
}
#endif