/*****************************************************************************
 * $LastChangedDate: 2009-11-22 22:39:11 -0500 (Sun, 22 Nov 2009) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Visitor classes.
 * @remarks
 * apply() OSG's counterpart to the Visit() method of the Visitor pattern.
 * apply(osg::Node&node) generally should call traverse(node) to keep traversing.
 *//*
 * LEGAL:   COPYRIGHT (C) 2007 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#ifndef GRAPH_VISITORS_HH
#define GRAPH_VISITORS_HH 1

#include <osg/NodeVisitor>

namespace graph {

////////////////////////////////////////////////////////////////////////////////
/// @brief Visitor to find a Node by its name.
///
class VisitorFindNamedNode : public osg::NodeVisitor 
{
public: 
    VisitorFindNamedNode( const string& nodeName )
    :   osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
        mFoundNode(NULL),
        mNodeName(nodeName)
    {
    }

    virtual void apply( osg::Node& node )
    {
        // Compare name of node.
        if ( node.getName() == mNodeName )
        {
            // Found it, stop.
            mFoundNode = &node;  // RefPtr<Node> = &Node
        }
        else
        {
            // Keep searching.
            traverse( node );
        }
    }

    /// @return matching node or NULL.
    RefPtr0<osg::Node>
    GetNode( void )
    {
        return mFoundNode;
    }

private:
    RefPtr0<osg::Node>  mFoundNode;     ///< hold ref
    const string        mNodeName;
};

} // namespace graph

#endif // GRAPH_VISITORS_HH
