Permalink
Browse files

Refactored osgDB::Input::readObjectOfType to use a template, and upda…

…ted associated wrappers to avoid using local static's
  • Loading branch information...
1 parent aadc406 commit e0985fdeead28f7c746c2c017edbfcccd8b9c878 @osg-training-guest osg-training-guest committed Nov 21, 2012
View
@@ -29,7 +29,25 @@
namespace osgDB {
-struct basic_type_wrapper;
+/** 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
@@ -255,6 +273,13 @@ class OSGDB_EXPORT Input : public FieldReaderIterator
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();
@@ -40,25 +40,6 @@ extern "C"
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;
- }
-};
/**
Registry is a singleton factory which stores
@@ -198,12 +198,10 @@ bool AnimationPathCallback_readLocalData(osg::Object &obj, osgDB::Input &fr)
iteratorAdvanced = true;
}
- static osg::ref_ptr<osg::AnimationPath> s_path = new osg::AnimationPath;
- ref_ptr<osg::Object> object = fr.readObjectOfType(*s_path);
- if (object.valid())
+ osg::ref_ptr<osg::AnimationPath> animpath = fr.readObjectOfType<osg::AnimationPath>();
+ if (animpath.valid())
{
- osg::AnimationPath* animpath = dynamic_cast<osg::AnimationPath*>(object.get());
- if (animpath) apc->setAnimationPath(animpath);
+ apc->setAnimationPath(animpath.get());
iteratorAdvanced = true;
}
@@ -45,9 +45,7 @@ bool CoordinateSystemNode_readLocalData(Object& obj, Input& fr)
fr+=2;
}
- static ref_ptr<EllipsoidModel> s_ellipsoidModel = new EllipsoidModel;
-
- EllipsoidModel* em = static_cast<EllipsoidModel*>(fr.readObjectOfType(*s_ellipsoidModel));
+ EllipsoidModel* em = fr.readObjectOfType<osg::EllipsoidModel>();
if (em) csn.setEllipsoidModel(em);
return iteratorAdvanced;
@@ -27,35 +27,35 @@ bool Drawable_readLocalData(Object& obj, Input& fr)
Drawable& drawable = static_cast<Drawable&>(obj);
- static ref_ptr<StateSet> s_drawstate = new osg::StateSet;
- if (StateSet* readState = static_cast<StateSet*>(fr.readObjectOfType(*s_drawstate)))
+ osg::StateSet* readState = fr.readObjectOfType<osg::StateSet>();
+ if (readState)
{
drawable.setStateSet(readState);
iteratorAdvanced = true;
}
- Shape* shape = static_cast<Shape *>(fr.readObjectOfType(type_wrapper<Shape>()));
+ Shape* shape = fr.readObjectOfType<Shape>();
if (shape)
{
drawable.setShape(shape);
iteratorAdvanced = true;
}
- Drawable::UpdateCallback* uc = dynamic_cast<Drawable::UpdateCallback *>(fr.readObjectOfType(type_wrapper<Drawable::UpdateCallback>()));
+ Drawable::UpdateCallback* uc = fr.readObjectOfType<Drawable::UpdateCallback>();
if (uc)
{
drawable.setUpdateCallback(uc);
iteratorAdvanced = true;
}
- Drawable::CullCallback* cc = dynamic_cast<Drawable::CullCallback *>(fr.readObjectOfType(type_wrapper<Drawable::CullCallback>()));
+ Drawable::CullCallback* cc = fr.readObjectOfType<Drawable::CullCallback>();
if (cc)
{
drawable.setCullCallback(cc);
iteratorAdvanced = true;
}
- Drawable::DrawCallback* dc = dynamic_cast<Drawable::DrawCallback *>(fr.readObjectOfType(type_wrapper<Drawable::DrawCallback>()));
+ Drawable::DrawCallback* dc = fr.readObjectOfType<Drawable::DrawCallback>();
if (dc)
{
drawable.setDrawCallback(dc);
@@ -76,7 +76,7 @@ bool Drawable_readLocalData(Object& obj, Input& fr)
iteratorAdvanced = true;
}
- Drawable::ComputeBoundingBoxCallback* cbc = dynamic_cast<Drawable::ComputeBoundingBoxCallback *>(fr.readObjectOfType(type_wrapper<Drawable::ComputeBoundingBoxCallback>()));
+ Drawable::ComputeBoundingBoxCallback* cbc = fr.readObjectOfType<Drawable::ComputeBoundingBoxCallback>();
if (cbc)
{
drawable.setComputeBoundingBoxCallback(cbc);
@@ -74,23 +74,22 @@ bool Node_readLocalData(Object& obj, Input& fr)
iteratorAdvanced = true;
}
- static ref_ptr<StateSet> s_drawstate = new osg::StateSet;
- if (StateSet* readState = static_cast<StateSet*>(fr.readObjectOfType(*s_drawstate)))
+ StateSet* readState = fr.readObjectOfType<StateSet>();
+ if (readState)
{
node.setStateSet(readState);
iteratorAdvanced = true;
}
- static ref_ptr<NodeCallback> s_nodecallback = new osg::NodeCallback;
while (fr.matchSequence("UpdateCallback {"))
{
int entry = fr[0].getNoNestedBrackets();
fr += 2;
while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
{
- NodeCallback* nodecallback = dynamic_cast<NodeCallback*>(fr.readObjectOfType(*s_nodecallback));
+ NodeCallback* nodecallback = fr.readObjectOfType<NodeCallback>();
if (nodecallback) {
if (node.getUpdateCallback() == NULL) {
node.setUpdateCallback(nodecallback);
@@ -111,7 +110,7 @@ bool Node_readLocalData(Object& obj, Input& fr)
while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
{
- NodeCallback* nodecallback = dynamic_cast<NodeCallback*>(fr.readObjectOfType(*s_nodecallback));
+ NodeCallback* nodecallback = fr.readObjectOfType<NodeCallback>();
if (nodecallback) {
if (node.getEventCallback() == NULL) {
node.setEventCallback(nodecallback);
@@ -132,7 +131,7 @@ bool Node_readLocalData(Object& obj, Input& fr)
while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
{
- NodeCallback* nodecallback = dynamic_cast<NodeCallback*>(fr.readObjectOfType(*s_nodecallback));
+ NodeCallback* nodecallback = fr.readObjectOfType<NodeCallback>();
if (nodecallback) {
if (node.getCullCallback() == NULL) {
node.setCullCallback(nodecallback);
@@ -165,7 +164,7 @@ bool Node_readLocalData(Object& obj, Input& fr)
while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
{
- Node::ComputeBoundingSphereCallback* callback = dynamic_cast<Node::ComputeBoundingSphereCallback*>(fr.readObjectOfType(type_wrapper<Node::ComputeBoundingSphereCallback>()));
+ Node::ComputeBoundingSphereCallback* callback = fr.readObjectOfType<Node::ComputeBoundingSphereCallback>();
if (callback) {
node.setComputeBoundingSphereCallback(callback);
}
@@ -28,12 +28,10 @@ bool NodeCallback_readLocalData(osg::Object &obj, osgDB::Input &fr)
bool itrAdvanced = false;
- static osg::ref_ptr<NodeCallback> s_nc = new NodeCallback;
- osg::ref_ptr<osg::Object> object = fr.readObjectOfType(*s_nc);
- if (object.valid())
+ NodeCallback* ncc = fr.readObjectOfType<NodeCallback>();
+ if (ncc)
{
- NodeCallback* ncc = dynamic_cast<NodeCallback*>(object.get());
- if (ncc) nc.setNestedCallback(ncc);
+ nc.setNestedCallback(ncc);
itrAdvanced = true;
}
@@ -27,10 +27,7 @@ bool OccluderNode_readLocalData(Object& obj, Input& fr)
OccluderNode& occludernode = static_cast<OccluderNode&>(obj);
- static ref_ptr<ConvexPlanarOccluder> s_occluder = new ConvexPlanarOccluder;
-
- ConvexPlanarOccluder* tmpOccluder = static_cast<ConvexPlanarOccluder*>(fr.readObjectOfType(*s_occluder));
-
+ ConvexPlanarOccluder* tmpOccluder = fr.readObjectOfType<ConvexPlanarOccluder>();
if (tmpOccluder)
{
occludernode.setOccluder(tmpOccluder);
@@ -29,12 +29,11 @@ bool StateAttribute_readLocalData(Object& obj, Input& fr)
bool iteratorAdvanced = false;
StateAttribute& stateAttribute = static_cast<StateAttribute&>(obj);
- static ref_ptr<StateAttributeCallback> s_callback = new osg::StateAttributeCallback;
while (fr.matchSequence("UpdateCallback {"))
{
//int entry = fr[0].getNoNestedBrackets();
fr += 2;
- StateAttributeCallback* callback = dynamic_cast<StateAttributeCallback*>(fr.readObjectOfType(*s_callback));
+ StateAttributeCallback* callback = fr.readObjectOfType<StateAttributeCallback>();
if (callback) {
stateAttribute.setUpdateCallback(callback);
}
@@ -45,7 +44,7 @@ bool StateAttribute_readLocalData(Object& obj, Input& fr)
{
//int entry = fr[0].getNoNestedBrackets();
fr += 2;
- StateAttributeCallback* callback = dynamic_cast<StateAttributeCallback*>(fr.readObjectOfType(*s_callback));
+ StateAttributeCallback* callback = fr.readObjectOfType<StateAttributeCallback>();
if (callback) {
stateAttribute.setEventCallback(callback);
}
Oops, something went wrong.

0 comments on commit e0985fd

Please sign in to comment.