FiReS  0.2.0
FiReS - Filter, Retrieval and Search
fires::DependenciesManager Class Reference

Class for adding dependencies between different object properties. More...

#include <DependenciesManager.h>

+ Collaboration diagram for fires::DependenciesManager:

Public Types

typedef std::function< void(Object *, const std::string &) > UpdatePropertyCallback
 Type for the callback functions for updating properties.
 
typedef std::unordered_set< std::pair< Object *, PropertyGID >, boost::hash< std::pair< Object *, PropertyGID > > > ObjectPropertyPairSet
 Type for holding the dependency or dependent pair of object and property.
 
typedef std::unordered_map< Object *, std::unordered_map< PropertyGID, std::pair< UpdatePropertyCallback, ObjectPropertyPairSet > > > DependsOn
 Type for holding the links from dependent to dependencies and also the upater callbacks.
 
typedef std::unordered_map< Object *, std::unordered_map< PropertyGID, ObjectPropertyPairSet > > IsDependecyOf
 Type for holding the links from dependencies to dependents.
 
typedef std::unordered_map< Object *, std::unordered_set< PropertyGID > > DirtyProperties
 Type for storing the dirtiness of properties. If the entery [object][propertyGID] exist in the container the property in that object is dirty.
 

Static Public Member Functions

template<class UPDATER >
static void setUpdater (Object *obj, const std::string &propLabel, UPDATER *updater, void(UPDATER::*memberFunc)(Object *, const std::string &))
 Establishes a member function used as updater for a specific object and property. More...
 
static void addDependency (Object *dependent, const std::string &dependentPropLabel, Object *dependency, const std::string &dependencyPropLabel)
 Establish a dependency between a property of an object and another property of the same or another object. More...
 
static void removeDependency (Object *dependent, const std::string &dependentPropLabel, Object *dependency, const std::string &dependencyPropLabel)
 Removes a dependency between a property of an object and another property of the same or another object. More...
 
static void setDependentsDirty (Object *obj, const std::string &propLabel, bool includeSelf=false)
 Sets a property of an object to be dirty. More...
 
static bool getDirtiness (Object *obj, const std::string &propLabel)
 Returns of a property of an object is dirty. More...
 
static void updateProperty (Object *obj, const std::string &propLabel)
 Method to update a specific property. More...
 
static void removeObject (Object *obj)
 Removes an object both from dependencies and dependents. More...
 

Static Protected Attributes

static DependsOn _dependsOn
 Holds the links from dependents to depdendencies and also the updaters.
 
static IsDependecyOf _isDependencyOf
 Holds the links from depdendenciesgg to dependents.
 
static DirtyProperties _dirtyProps
 Hold the properties that are dirty.
 

Detailed Description

Class for adding dependencies between different object properties.

It allows to associate updater functions to update properties and handles dirtiness of properties. Example:

/*
* Copyright (c) 2014-2016 VG-Lab/URJC/UPM.
*
* Authors: Pablo Toharia <pablo.toharia@upm.es>
*
* This file is part of FiReS <https://github.com/vg-lab/FiReS>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <fires/fires.h>
class Point : public fires::Object
{
public:
Point( )
{
this->registerProperty( "x", 0.0f );
this->registerProperty( "y", 0.0f );
}
};
class Edge : public fires::Object
{
public:
Edge( )
{
this->registerProperty( "length", 0.0f );
&points[0], "x" );
&points[0], "y" );
&points[1], "x" );
&points[1], "y" );
this, &Edge::computeLength );
}
void computeLength( fires::Object*, const std::string& )
{
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];
};
class Rectangle : public fires::Object
{
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;
// output:
// [ Rectangle: updating area ]
// [ Edge: updating length ]
// [ Edge: updating length ]
// 2
std::cout << std::endl << "Requesting for total length for the first time"
<< std::endl;
std::cout << rect.getProperty( "total length" ).value< float >( ) << std::endl;
// output:
// [ Rectangle: updated total length ]
// 6
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;
// [ Rectangle: updating area ]
// [ Edge: updating length ]
// 6
std::cout << std::endl << "Requesting for area with no update needed"
<< std::endl;
std::cout << rect.getProperty( "area" ).value< float >( ) << std::endl;
// output:
// 6
std::cout << std::endl << "Requesting for total length needs updating"
<< std::endl;
std::cout << rect.getProperty( "total length" ).value< float >( ) << std::endl;
// output:
// 10
return 0;
}

Definition at line 47 of file DependenciesManager.h.

Member Function Documentation

static void fires::DependenciesManager::addDependency ( Object dependent,
const std::string &  dependentPropLabel,
Object dependency,
const std::string &  dependencyPropLabel 
)
static

Establish a dependency between a property of an object and another property of the same or another object.

Parameters
dependentobject that will depend on ( dependency, dependencyPropLabel )
dependentPropLabelproperty that will depend on ( dependent, propLabel )
dependencyobject which will be a dependency of ( dependent, propLabel )
dependencyPropLabelproperty that will be a dependency of ( dependent, propLabel )

Referenced by setUpdater().

+ Here is the caller graph for this function:

static bool fires::DependenciesManager::getDirtiness ( Object obj,
const std::string &  propLabel 
)
static

Returns of a property of an object is dirty.

Parameters
objthe object that holds the property to test if dirty
propLabelthe name of the property to test if dirty
Returns
true if the property is dirty

Referenced by setUpdater().

+ Here is the caller graph for this function:

static void fires::DependenciesManager::removeDependency ( Object dependent,
const std::string &  dependentPropLabel,
Object dependency,
const std::string &  dependencyPropLabel 
)
static

Removes a dependency between a property of an object and another property of the same or another object.

Parameters
dependentobject that depends on ( dependency, dependencyPropLabel )
dependentPropLabelproperty that depends on ( dependent, propLabel )
dependencyobject which is a dependency of ( dependent, propLabel )
dependencyPropLabelproperty that is a dependency of ( dependent, propLabel )

Referenced by setUpdater().

+ Here is the caller graph for this function:

static void fires::DependenciesManager::removeObject ( Object obj)
static

Removes an object both from dependencies and dependents.

This method is automatically called when a fires::Object is deleted

Parameters
objthe object that will be removed from dependents and depencendies

Referenced by setUpdater().

+ Here is the caller graph for this function:

static void fires::DependenciesManager::setDependentsDirty ( Object obj,
const std::string &  propLabel,
bool  includeSelf = false 
)
static

Sets a property of an object to be dirty.

Parameters
objthe object that holds the property to be set dirty
propLabelthe name of the property to be set dirty

Referenced by setUpdater().

+ Here is the caller graph for this function:

template<class UPDATER >
static void fires::DependenciesManager::setUpdater ( Object obj,
const std::string &  propLabel,
UPDATER *  updater,
void(UPDATER::*)(Object *, const std::string &)  memberFunc 
)
inlinestatic

Establishes a member function used as updater for a specific object and property.

Parameters
objobject to which associate the updater callback
propLabelproperty
updaterobject with the member function
memberFuncmember function used as updater callback

Definition at line 90 of file DependenciesManager.h.

References _dependsOn, addDependency(), getDirtiness(), fires::PropertyGIDsManager::getPropertyGID(), removeDependency(), removeObject(), setDependentsDirty(), and updateProperty().

+ Here is the call graph for this function:

static void fires::DependenciesManager::updateProperty ( Object obj,
const std::string &  propLabel 
)
static

Method to update a specific property.

This methos is not supposed to be called manually but is called automatically from Object::getProperty. If the proprety is dirty the updater function will be called (if exists), otherwise no update is needed.

Parameters
objthe object that holds the property to be updated
propLabelthe name of the property to be updated

Referenced by setUpdater().

+ Here is the caller graph for this function:


The documentation for this class was generated from the following file: