SurgSim::Blocks::FunctionBehavior Class Reference

A Behavior that can run any callable target in its update function. More...

`#include <SurgSim/Blocks/FunctionBehavior.h>`

Inheritance diagram for SurgSim::Blocks::FunctionBehavior:

## Public Member Functions

| FunctionBehavior (const std::string &name)
| Constructor. More...
| SURGSIM_CLASSNAME (SurgSim::Blocks::FunctionBehavior)
void | setFunction (std::function< void(double)> function)
| Set the function to run in this behavior's update. More...
void | setTargetManagerType (int type)
| Set which manager will handle this behavior. More...
int | getTargetManagerType () const override
| Specifies which manger will handle this behavior. More...
void | update (double dt) override
| Update the behavior. More...
Public Member Functions inherited from SurgSim::Framework::Behavior
| Behavior (const std::string &name)
virtual | ~Behavior ()
Public Member Functions inherited from SurgSim::Framework::Component
| Component (const std::string &name)
| Constructor. More...
virtual | ~Component ()
| Destructor. More...
std::string | getName () const
| Gets component name. More...
std::string | getFullName () const
| Gets a string containing the name of the Component and (if it has one) its SceneElement. More...
void | setName (const std::string &name)
| Sets the name of component. More...
boost::uuids::uuid | getUuid () const
| Gets the id of the component. More...
bool | isInitialized () const
bool | initialize (const std::weak_ptr< Runtime > &runtime)
| Initialize this component, this needs to be called before wakeUp() can be called. More...
bool | isAwake () const
bool | wakeUp ()
| Wakeup this component, this will be called when the component is inserted into the ComponentManager that is responsible for handling this component. More...
void | retire ()
| Retire this component, this will be called when the component is removed from the ComponentManager that is responsible for handling this component. More...
void | setScene (std::weak_ptr< Scene > scene)
| Sets the scene. More...
std::shared_ptr< Scene > | getScene ()
| Gets the scene. More...
void | setSceneElement (std::weak_ptr< SceneElement > sceneElement)
| Sets the scene element. More...
std::shared_ptr< SceneElement > | getSceneElement ()
| Gets the scene element. More...
std::shared_ptr< const SceneElement > | getSceneElement () const
| Gets the scene element, constant version. More...
std::shared_ptr< Runtime > | getRuntime () const
| Get the runtime which contains this component. More...
virtual std::string | getClassName () const
| The class name for this class, this being the base class it should return SurgSim::Framework::Component but this would make missing implemenentations of this hard to catch, therefore this calls SURGSIM_FAILURE. More...
std::shared_ptr< Component > | getSharedPtr ()
| Gets a shared pointer to this component. More...
virtual void | doRetire ()
| Interface to be implemented by derived classes Has a default implementation, does nothing. More...
bool | isActive () const
virtual void | setLocalActive (bool val)
| Set the component's active state. More...
bool | isLocalActive () const
Public Member Functions inherited from SurgSim::Framework::Accessible
| Accessible ()
| Default Constructor. More...
| ~Accessible ()
| Destructor. More...
template<class T >
T | getValue (const std::string &name) const
| Retrieves the value with the name by executing the getter if it is found and tries to convert it to the given type. More...
boost::any | getValue (const std::string &name) const
| Retrieves the value with the name by executing the getter if it is found. More...
template<class T >
bool | getValue (const std::string &name, T *value) const
| Retrieves the value with the name by executing the getter if it is found, and converts it to the type of the output parameter. More...
void | setValue (const std::string &name, const boost::any &value)
| Sets a value of a property that has setter. More...
bool | isReadable (const std::string &name) const
| Check whether a property is readable. More...
bool | isWriteable (const std::string &name) const
| Check whether a property is writable. More...
void | setGetter (const std::string &name, GetterType func)
| Sets a getter for a given property. More...
void | setSetter (const std::string &name, SetterType func)
| Sets a setter for a given property. More...
void | setAccessors (const std::string &name, GetterType getter, SetterType setter)
| Sets the accessors getter and setter in one function. More...
void | removeAccessors (const std::string &name)
| Removes all the accessors (getter and setter) for a given property. More...
void | forwardProperty (const std::string &name, const Accessible &target, const std::string &targetProperty)
| Adds a property with the given name that uses the targets accessors, in effect forwarding the value to the target. More...
void | setSerializable (const std::string &name, EncoderType encoder, DecoderType decoder)
| Sets the functions used to convert data from and to a YAML::Node. More...
void | setDecoder (const std::string &name, DecoderType decoder)
| Sets the functions used to convert data from a YAML::Node. More...
YAML::Node | encode () const
| Encode this Accessible to a YAML::Node. More...
void | decode (const YAML::Node &node, const std::vector< std::string > &ignoredProperties=std::vector< std::string >())
| Decode this Accessible from a YAML::Node, will throw an exception if the data type cannot be converted. More...
template<>
boost::any | getValue (const std::string &name) const

## Private Member Functions

bool | doInitialize () override
| Interface to be implemented by derived classes. More...
bool | doWakeUp () override
| Interface to be implemented by derived classes. More...

## Private Attributes

int | m_targetManager
| The manager type that will handle this behavior. More...
std::function< void(double)> | m_function
| The function to run in update. More...

## Additional Inherited Members

Public Types inherited from SurgSim::Framework::Accessible
typedef std::function< boost::any(void)> | GetterType
typedef std::function< void(boost::any)> | SetterType
typedef std::function< YAML::Node(void)> | EncoderType
typedef std::function< void(const YAML::Node *)> | DecoderType
Public Types inherited from SurgSim::Framework::FactoryBase1< Component, std::string >
typedef ObjectFactory1< Component, std::string > | FactoryType
Static Public Member Functions inherited from SurgSim::Framework::FactoryBase1< Component, std::string >
static FactoryType & | getFactory ()
Protected Member Functions inherited from SurgSim::Framework::Component
virtual std::shared_ptr< PoseComponent > | getPoseComponent ()
| Get the PoseComponent for this component. More...
virtual std::shared_ptr< const PoseComponent > | getPoseComponent () const
| Get the PoseComponent for this component, constant access. More...

## Detailed Description

A Behavior that can run any callable target in its update function.

This behavior has a customizable update function, that can run any callable target, ie functions, lambda expressions, bind expressions, as well as pointers to member functions.

Example Usage:

void f1(double dt) { std::cout << dt; }
void f2(double dt, double a) { std::cout << dt + a; }
int main()
{
// A FunctionBehavior that runs a function
auto behavior1 = std::make_shared<SurgSim::Blocks:FunctionBehavior>("Behavior 1");
behavior1->setTargetManagerType(SurgSim::Framework::MANAGER_TYPE_BEHAVIOR);
behavior1->setFunction(f1);
// A FunctionBehavior that runs a std::bind function
auto behavior2 = std::make_shared<SurgSim::Blocks:FunctionBehavior>("Behavior 2");
behavior2->setTargetManagerType(SurgSim::Framework::MANAGER_TYPE_GRAPHICS);
behavior2->setFunction(std::bind(f2, std::placeholders::_1, 2.0));
// A FunctionBehavior that runs a lambda function
auto behavior3 = std::make_shared<SurgSim::Blocks:FunctionBehavior>("Behavior 3");
behavior3->setTargetManagerType(SurgSim::Framework::MANAGER_TYPE_PHYSICS);
behavior3->setFunction([](double dt) { std::cout << dt; });
auto element = std::make_shared<SurgSim::Framework::BasicSceneElement>("Element");
element->addComponent(behavior1);
element->addComponent(behavior2);
element->addComponent(behavior3);
return 0;
}

Note
  The FunctionBehavior is incompatible with serialization

## Constructor & Destructor Documentation

## FunctionBehavior()

SurgSim::Blocks::FunctionBehavior::FunctionBehavior | ( | const std::string & | name | ) |

explicit

Constructor.

Parameters

  name | Name of the behavior

## Member Function Documentation

## doInitialize()

bool SurgSim::Blocks::FunctionBehavior::doInitialize | ( |  | ) |

override private virtual

Interface to be implemented by derived classes.

Returns
  True if component is initialized successfully; otherwise, false.

Implements SurgSim::Framework::Component.

## doWakeUp()

bool SurgSim::Blocks::FunctionBehavior::doWakeUp | ( |  | ) |

override private virtual

Interface to be implemented by derived classes.

Returns
  True if component is woken up successfully; otherwise, false.

Implements SurgSim::Framework::Component.

## getTargetManagerType()

int SurgSim::Blocks::FunctionBehavior::getTargetManagerType | ( |  | ) | const

override virtual

Specifies which manger will handle this behavior.

Reimplemented from SurgSim::Framework::Behavior.

## setFunction()

void SurgSim::Blocks::FunctionBehavior::setFunction | ( | std::function< void(double)> | function | ) |

Set the function to run in this behavior's update.

Parameters

  function | The function

## setTargetManagerType()

void SurgSim::Blocks::FunctionBehavior::setTargetManagerType | ( | int | type | ) |

Set which manager will handle this behavior.

Parameters

  type | Type of manager for this behavior

## SURGSIM_CLASSNAME()

SurgSim::Blocks::FunctionBehavior::SURGSIM_CLASSNAME | ( | SurgSim::Blocks::FunctionBehavior |  | ) |

## update()

void SurgSim::Blocks::FunctionBehavior::update | ( | double | dt | ) |

override virtual

Update the behavior.

Parameters

  dt | The length of time (seconds) between update calls.

Implements SurgSim::Framework::Behavior.

## Member Data Documentation

## m_function

std::function<void(double)> SurgSim::Blocks::FunctionBehavior::m_function

private

The function to run in update.

## m_targetManager

int SurgSim::Blocks::FunctionBehavior::m_targetManager

private

The manager type that will handle this behavior.

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

- SurgSim/Blocks/FunctionBehavior.h
- SurgSim/Blocks/FunctionBehavior.cpp