Class for adding dependencies between different object properties.
It allows to associate updater functions to update properties and handles dirtiness of properties. Example:
#include <fires/fires.h>
{
public:
Point( )
{
this->registerProperty( "x", 0.0f );
this->registerProperty( "y", 0.0f );
}
};
{
public:
Edge( )
{
this->registerProperty( "length", 0.0f );
&points[0], "x" );
&points[0], "y" );
&points[1], "x" );
&points[1], "y" );
this, &Edge::computeLength );
}
{
this->setProperty(
"length", sqrtf(
powf( points[1].getProperty( "x" ).value< float >( ) -
points[0].getProperty( "x" ).value< float >( ), 2 ) +
powf( points[1].getProperty( "y" ).value< float >( ) -
points[0].getProperty( "y" ).value< float >( ), 2 )));
std::cout << "[ Edge: updated length ]" << std::endl;
}
Point points[2];
};
{
public:
Rectangle( )
{
this->registerProperty( "area", 0.0f );
&edges[0], "length" );
&edges[1], "length" );
this, &Rectangle::computeAreaAndLength );
this->registerProperty( "total length", 0.0f );
&edges[0], "length" );
&edges[1], "length" );
this, &Rectangle::computeAreaAndLength );
}
void computeAreaAndLength(
fires::Object*,
const std::string& prop )
{
if ( prop == "area" )
this->setProperty( "area",
edges[0].getProperty( "length" ).value< float >( ) *
edges[1].getProperty( "length" ).value< float >( ));
else if ( prop == "total length" )
this->setProperty(
"total length",
2.0f * ( edges[0].getProperty( "length" ).value< float >( ) +
edges[1].getProperty( "length" ).value< float >( )));
std::cout << "[ Rectangle: updated " << prop << " ]" << std::endl;
}
Edge edges[2];
};
int main( void )
{
Rectangle rect;
rect.edges[0].points[0].setProperty( "x", 1.0f );
rect.edges[0].points[0].setProperty( "y", 1.0f );
rect.edges[0].points[1].setProperty( "x", 1.0f );
rect.edges[0].points[1].setProperty( "y", 2.0f );
rect.edges[1].points[0].setProperty( "x", 1.0f );
rect.edges[1].points[0].setProperty( "y", 1.0f );
rect.edges[1].points[1].setProperty( "x", 3.0f );
rect.edges[1].points[1].setProperty( "y", 1.0f );
std::cout << "Requesting area for the first time" << std::endl;
std::cout << rect.getProperty( "area" ).value< float >( ) << std::endl;
std::cout << std::endl << "Requesting for total length for the first time"
<< std::endl;
std::cout << rect.getProperty( "total length" ).value< float >( ) << std::endl;
std::cout << std::endl << "Setting one dependency" << std::endl;
rect.edges[0].points[1].setProperty( "y", 4.0f );
std::cout << std::endl << "Requesting for total length needs updating"
<< std::endl;
std::cout << rect.getProperty( "total length" ).value< float >( ) << std::endl;
std::cout << std::endl << "Requesting for area needs updating"
<< std::endl;
std::cout << rect.getProperty( "area" ).value< float >( ) << std::endl;
std::cout << std::endl << "Requesting for area with no update needed"
<< std::endl;
std::cout << rect.getProperty( "area" ).value< float >( ) << std::endl;
std::cout << std::endl << "Requesting for total length needs updating"
<< std::endl;
std::cout << rect.getProperty( "total length" ).value< float >( ) << std::endl;
return 0;
}