Re: [osg-users] Safe downcast

2012-08-22 Thread Jean-Sébastien Guay

Hello,

For the answer, I suggest you revise the C++ docs about dynamic_cast 
and also check the class hierarchy of OSG's node types.


dynamic_castA* succeeds for any pointer to an object of class A or any 
pointer to an object of a derived class of A. So even if you don't have 
a proper Group at the root, if you have any object of a derived class of 
Group (like osg::MatrixTransform, osg::Switch, etc.) it will succeed.


Check out Node::asGroup() (reimplemented in osg::Group) for an 
alternative to dynamic_cast (trades a dynamic_cast which is pretty 
costly for a virtual method call which is much less costly).


Hope this helps,

J-S

On 21/08/2012 3:49 PM, Peterakos wrote:

Hello.

I forgot to mention that i check it. That's why i posted it. Cause it 
is very strange to run even though there is no group as root..


osg::ref_ptrosg::Group node = 
dynamic_castosg::Group*(osgDB::readNodeFile( astroboy_walk.dae ));

if(!node) {
   std::cout  Model could not be loaded   std::endl;
   return 1;
}

thank you.


___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



--
__
Jean-Sebastien Guay  jean_...@videotron.ca
http://whitestar02.dyndns-web.com/

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Safe downcast

2012-08-21 Thread Robert Osfield
Hi Peterakos,

You should always check that the node set by the dynamic_cast is not
NULL before doing any operation on it, this is a very basic
programming practice, you shouldn't assume that the root node will
always be a Group or a subclass from Group.

Robert.

On 20 August 2012 18:15, Peterakos hay...@gmail.com wrote:
 Hello.
 Thank you for your answer.
 Using an InfoVisitorNode, i can see there is not any group node as a parent.
 Even though that happens, i can still use addChild method in something that
 is not Group and has been passed through dynamic_cast.

 osg::ref_ptrosg::Group node =
 dynamic_castosg::Group*(osgDB::readNodeFile(astroboy_walk.dae));
 node-addChild(createAxis());


 I send the dae file too.

 thank you for your time.

 ___
 osg-users mailing list
 osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Safe downcast

2012-08-21 Thread Peterakos
Hello.

I forgot to mention that i check it. That's why i posted it. Cause it is
very strange to run even though there is no group as root..

osg::ref_ptrosg::Group node =
dynamic_castosg::Group*(osgDB::readNodeFile( astroboy_walk.dae ));
if(!node) {
   std::cout  Model could not be loaded   std::endl;
   return 1;
}

thank you.
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


[osg-users] Safe downcast

2012-08-20 Thread Peterakos
Hello.

In the example osganimationviewer in main there is the following line:
osg::Group* node =
dynamic_castosg::Group*(osgDB::readNodeFiles(arguments));

How safe is this if i use only 1 model in arguments and it is not Group ?

Thank you for your time.
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Safe downcast

2012-08-20 Thread Jason Daly

On 08/20/2012 09:00 AM, Peterakos wrote:

Hello.

In the example osganimationviewer in main there is the following line:
osg::Group* node = 
dynamic_castosg::Group*(osgDB::readNodeFiles(arguments));


How safe is this if i use only 1 model in arguments and it is not Group ?


osgDB::readNodeFiles inserts a group at the root if there's more than 
one model loaded, but not when only a single model is loaded.  There's a 
(rather slim) chance that your one model might not contain a Group (or 
Group descendant) at the root and that the dynamic_cast would result in 
a NULL.  If you wanted to be absolutely sure, you could put in a check 
for this.


Practically, almost every useful model (especially models with 
animations) will have some kind of group at its root.


--J
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org