osgEarth 2.1.1
Public Member Functions | Private Types | Private Member Functions | Private Attributes

osgEarth::Json::Path Class Reference

Experimental and untested: represents a "path" to access a node. More...

List of all members.

Public Member Functions

 Path (const std::string &path, const PathArgument &a1=PathArgument(), const PathArgument &a2=PathArgument(), const PathArgument &a3=PathArgument(), const PathArgument &a4=PathArgument(), const PathArgument &a5=PathArgument())
const Valueresolve (const Value &root) const
Value resolve (const Value &root, const Value &defaultValue) const
Valuemake (Value &root) const
 Creates the "path" to access the specified node and returns a reference on the node.

Private Types

typedef std::vector< const
PathArgument * > 
InArgs
typedef std::vector< PathArgumentArgs

Private Member Functions

void makePath (const std::string &path, const InArgs &in)
void addPathInArg (const std::string &path, const InArgs &in, InArgs::const_iterator &itInArg, PathArgument::Kind kind)
void invalidPath (const std::string &path, int location)

Private Attributes

Args args_

Detailed Description

Experimental and untested: represents a "path" to access a node.

Syntax:

Definition at line 540 of file JsonUtils.


Member Typedef Documentation

typedef std::vector<PathArgument> osgEarth::Json::Path::Args [private]

Definition at line 558 of file JsonUtils.

typedef std::vector<const PathArgument *> osgEarth::Json::Path::InArgs [private]

Definition at line 557 of file JsonUtils.


Constructor & Destructor Documentation

Path::Path ( const std::string &  path,
const PathArgument a1 = PathArgument(),
const PathArgument a2 = PathArgument(),
const PathArgument a3 = PathArgument(),
const PathArgument a4 = PathArgument(),
const PathArgument a5 = PathArgument() 
)

Definition at line 1807 of file JsonUtils.cpp.

{
   InArgs in;
   in.push_back( &a1 );
   in.push_back( &a2 );
   in.push_back( &a3 );
   in.push_back( &a4 );
   in.push_back( &a5 );
   makePath( path, in );
}

Here is the call graph for this function:


Member Function Documentation

void Path::addPathInArg ( const std::string &  path,
const InArgs in,
InArgs::const_iterator &  itInArg,
PathArgument::Kind  kind 
) [private]

Definition at line 1869 of file JsonUtils.cpp.

{
   if ( itInArg == in.end() )
   {
      // Error: missing argument %d
   }
   else if ( (*itInArg)->kind_ != kind )
   {
      // Error: bad argument type
   }
   else
   {
      args_.push_back( **itInArg );
   }
}

Here is the caller graph for this function:

void Path::invalidPath ( const std::string &  path,
int  location 
) [private]

Definition at line 1890 of file JsonUtils.cpp.

{
   // Error: invalid path.
}

Here is the caller graph for this function:

Value & Path::make ( Value root) const

Creates the "path" to access the specified node and returns a reference on the node.

Definition at line 1957 of file JsonUtils.cpp.

{
   Value *node = &root;
   for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
   {
      const PathArgument &arg = *it;
      if ( arg.kind_ == PathArgument::kindIndex )
      {
         if ( !node->isArray() )
         {
            // Error: node is not an array at position ...
         }
         node = &((*node)[arg.index_]);
      }
      else if ( arg.kind_ == PathArgument::kindKey )
      {
         if ( !node->isObject() )
         {
            // Error: node is not an object at position...
         }
         node = &((*node)[arg.key_]);
      }
   }
   return *node;
}

Here is the call graph for this function:

void Path::makePath ( const std::string &  path,
const InArgs in 
) [private]

Definition at line 1825 of file JsonUtils.cpp.

{
   const char *current = path.c_str();
   const char *end = current + path.length();
   InArgs::const_iterator itInArg = in.begin();
   while ( current != end )
   {
      if ( *current == '[' )
      {
         ++current;
         if ( *current == '%' )
            addPathInArg( path, in, itInArg, PathArgument::kindIndex );
         else
         {
            Value::UInt index = 0;
            for ( ; current != end && *current >= '0'  &&  *current <= '9'; ++current )
               index = index * 10 + Value::UInt(*current - '0');
            args_.push_back( index );
         }
         if ( current == end  ||  *current++ != ']' )
            invalidPath( path, int(current - path.c_str()) );
      }
      else if ( *current == '%' )
      {
         addPathInArg( path, in, itInArg, PathArgument::kindKey );
         ++current;
      }
      else if ( *current == '.' )
      {
         ++current;
      }
      else
      {
         const char *beginName = current;
         while ( current != end  &&  !strchr( "[.", *current ) )
            ++current;
         args_.push_back( std::string( beginName, current ) );
      }
   }
}

Here is the call graph for this function:

Here is the caller graph for this function:

const Value & Path::resolve ( const Value root) const

Definition at line 1898 of file JsonUtils.cpp.

{
   const Value *node = &root;
   for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
   {
      const PathArgument &arg = *it;
      if ( arg.kind_ == PathArgument::kindIndex )
      {
         if ( !node->isArray()  ||  node->isValidIndex( arg.index_ ) )
         {
            // Error: unable to resolve path (array value expected at position...
         }
         node = &((*node)[arg.index_]);
      }
      else if ( arg.kind_ == PathArgument::kindKey )
      {
         if ( !node->isObject() )
         {
            // Error: unable to resolve path (object value expected at position...)
         }
         node = &((*node)[arg.key_]);
         if ( node == &Value::null )
         {
            // Error: unable to resolve path (object has no member named '' at position...)
         }
      }
   }
   return *node;
}

Here is the call graph for this function:

Value Path::resolve ( const Value root,
const Value defaultValue 
) const

Definition at line 1930 of file JsonUtils.cpp.

{
   const Value *node = &root;
   for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
   {
      const PathArgument &arg = *it;
      if ( arg.kind_ == PathArgument::kindIndex )
      {
         if ( !node->isArray()  ||  node->isValidIndex( arg.index_ ) )
            return defaultValue;
         node = &((*node)[arg.index_]);
      }
      else if ( arg.kind_ == PathArgument::kindKey )
      {
         if ( !node->isObject() )
            return defaultValue;
         node = &((*node)[arg.key_]);
         if ( node == &Value::null )
            return defaultValue;
      }
   }
   return *node;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 569 of file JsonUtils.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines