NAP
Public Member Functions | Static Public Member Functions | List of all members
Path Class Reference

#include <rtti/path.h>

Public Member Functions

void pushAttribute (const char *attributeName)
 
void pushAttribute (const std::string &attributeName)
 
void pushArrayElement (int index)
 
void popBack ()
 
int getLength () const
 
const PathElementgetElement (int index) const
 
bool operator== (const Path &lhs) const
 
bool operator!= (const Path &lhs) const
 
const std::string toString () const
 
bool resolve (const rtti::Object *object, ResolvedPath &resolvedPath) const
 
int length () const
 

Static Public Member Functions

static const Path fromString (const std::string &path)
 

Description

A Path represents a path to a specific property/value through a RTTI hierarchy. It's useful to be able to store a 'reference' to a property in a data structure, without actually having to have a reference. By itself, Path just stores a path and can't be used for any operations. In order to do that, the RTITPath must be 'resolved' against a root object. The resolve of a Path results in a ResolvedPath, which can then be used to get/set the value of the property represented by the Path.

As a simple example, consider the following RTTI types (assume they're registered in RTTI):

struct DataStruct
{
    float               mFloatProperty = 0.0f;
    rtti::RTTIObject*   mPointerProperty = nullptr;
};

class SomeRTTIClass : public rtti::RTTIObject
{
    RTTI_ENABLE(rtti::RTTIObject)

public:
    DataStruct                      mNestedCompound;
    std::vector<int>                mArrayOfInts;
    std::vector<DataStruct>         mArrayOfCompounds;
    std::vector<rtti::RTTIObject*>  mArrayOfPointers;
};

Suppose we want to store a reference to the 'PointerProperty' of the 'DataStruct' embedded in the first element of the 'ArrayOfCompounds' property in 'SomeRTTIClass'. We could do this as follows:

Path path;
path.PushAttribute("ArrayOfCompounds");     // Push name of the RTTI property on SomeRTTIClass
path.PushArrayElement(0);                   // Push index into the array 'ArrayOfCompounds' on SomeRTTIClass
path.PushAttribute("PointerProperty");      // Push name of the RTTI property on the compound contained in ArrayOfCompounds, namely 'DataStruct'

So we now have a 'path' to the pointer property, which we can store wherever we want.

At some later point in time, let's suppose we now want to get or set the value that this Path points to. In order to do this, we first need to resolve the path:

SomeRTTIClass* object = ... // Assume we have a pointer to an instance of SomeRTTIClass that we can resolve against
ResolvedPath resolved_path = path.resolve(object);
if (!resolved_path.isValid())
    return; // Failed to resolve the path; either it's incorrect or does not match with the provided instance of SomeRTTIClass

Once the path has been successfully resolved, we can use it to get/set the value of the property:

// Retrieve the value of the pointer
rtti::RTTIObject* pointer_value = resolved_path.getValue().get_value<rtti::RTTIObject*>();

// Set the value of the pointer
rtti::RTTIObject* some_other_object = ... // Get the pointer we want to set
resolved_path.setValue(some_other_object);

After the setValue, object->mArrayOfCompounds[0].mPointerProperty now points to 'some_other_object'

Member Function Documentation

◆ fromString()

static const Path fromString ( const std::string &  path)
static

Convert a string representation of a Path in the format "Attribute/ArrayIndex/Attribute" to an actual Path

Returns
The Path

◆ getElement()

const PathElement& getElement ( int  index) const

Get the element at the specified index on the path

Parameters
indexThe index of the element to get
Returns
The element at the specified index

◆ getLength()

int getLength ( ) const

Get the length of the path

Returns
The length of the path

◆ length()

int length ( ) const

◆ operator!=()

bool operator!= ( const Path lhs) const

Inequality comparison

◆ operator==()

bool operator== ( const Path lhs) const

Equality comparison

◆ popBack()

void popBack ( )

Remove last element from the path

◆ pushArrayElement()

void pushArrayElement ( int  index)

Push an array element on the path

Parameters
indexThe index of the array element to push

◆ pushAttribute() [1/2]

void pushAttribute ( const char *  attributeName)

Push an attribute on the path. The specified attribute name string is expected to have a lifetime longer than that of the Path.

Parameters
attributeNameThe name of the attribute to push

◆ pushAttribute() [2/2]

void pushAttribute ( const std::string &  attributeName)

Push an attribute on the path. The specified attribute name string is copied locally; use the const char* overload for better performance, if you know that the lifetime of your string is longer than that of the Path.

Parameters
attributeNameThe name of the attribute to push

◆ resolve()

bool resolve ( const rtti::Object object,
ResolvedPath resolvedPath 
) const

Resolve a Path against an Object

Parameters
objectThe object to resolve against
resolvedPathThe resolved RTTI path
Returns
Whether the resolve succeeded or not

◆ toString()

const std::string toString ( ) const

Convert this Path to a string representation of the format "Attribute/ArrayIndex/Attribute"

Returns
The string representation of this Path