$darkmode
set_action.hpp File Reference
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <cloe/trigger.hpp>
#include <cloe/trigger/helper_macros.hpp>
Include dependency graph for set_action.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  cloe::actions::SetVariableAction< T >
 
class  cloe::actions::SetVariableActionFactory< T >
 

Macros

#define CLOE_TRIGGER_SET_ACTION_HPP_
 
#define DEFINE_SET_STATE_ACTION(xName, xname, xdescription, xState, xOperatorBlock)
 
#define DEFINE_SET_DATA_ACTION(xName, xActionName, xActionDesc, xDataType, xAttributeName, xAttributeType, xOperatorBlock)
 

Functions

template<typename T >
cloe::actions::from_string (const std::string &s)
 
template<>
double cloe::actions::from_string< double > (const std::string &s)
 
template<>
int cloe::actions::from_string< int > (const std::string &s)
 
template<>
bool cloe::actions::from_string< bool > (const std::string &s)
 

Detailed Description

This file defines actions that set a given variable.

Macro Definition Documentation

◆ DEFINE_SET_DATA_ACTION

#define DEFINE_SET_DATA_ACTION (   xName,
  xActionName,
  xActionDesc,
  xDataType,
  xAttributeName,
  xAttributeType,
  xOperatorBlock 
)

Macro DEFINE_SET_DATA_ACTION defines an action that sets an attribute from the configuration.

Parameters
xNametype identifier for action
xActionNamestring identifier of action (lowercase)
xActionDescstring description of action
xDataTypetype identifier for member data pointer
xAttributeNamestring identifier of the attribute (defines configuration)
xAttributeTypetype identifier of the attribute
xOperatorBlockcode block that makes use of ptr_ and value_

Given the type SimulationSync and the action "modify the simulation speed", we can create it in code like so:

DEFINE_SET_DATA_ACTION(RealtimeFactor, "realtime_factor", "modify simulation speed",
SimulationSync, "factor", double,
{
logger()->info("Setting target simulation speed: {}", value_);
ptr_->set_realtime_factor(value_);
})

This will define the following classes for us:

  • RealtimeFactor
  • RealtimeFactorFactory

This action can be registered with the register_action helper function. Refer to doc/reference/actions.rst for the configuration.

◆ DEFINE_SET_STATE_ACTION

#define DEFINE_SET_STATE_ACTION (   xName,
  xname,
  xdescription,
  xState,
  xOperatorBlock 
)
Value:
class xName : public ::cloe::Action { \
public: \
xName(const std::string& name, xState* ptr) : ::cloe::Action(name), ptr_(ptr) {} \
::cloe::ActionPtr clone() const override { return std::make_unique<xName>(name(), ptr_); } \
void operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { xOperatorBlock } \
void to_json(::cloe::Json&) const override {} \
\
private: \
xState* ptr_; \
}; \
\
class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
public: \
using ActionType = xName; \
\
_X_FACTORY(xName)(xState * ptr) : ::cloe::ActionFactory(xname, xdescription), ptr_(ptr) {} \
\
::cloe::ActionPtr make(const ::cloe::Conf&) const override { \
return std::make_unique<xName>(name(), ptr_); \
} \
\
::cloe::ActionPtr make(const std::string&) const override { \
return std::make_unique<xName>(name(), ptr_); \
} \
\
private: \
xState* ptr_; \
};
TriggerFactory< Action > ActionFactory
Definition: trigger.hpp:663
Definition: trigger.hpp:442
Definition: trigger.hpp:616
Definition: coordinator.hpp:36
virtual void to_json(Json &j) const =0
virtual ActionPtr clone() const =0
Definition: trigger.hpp:300
std::string name() const
Definition: entity.hpp:68
virtual void operator()(const Sync &, TriggerRegistrar &)=0

Macro DEFINE_SET_STATE_ACTION defines an action that has only a single state and no configuration.

Parameters
xNametype identifier for event
xnamestring identifier of event (lowercase)
xdescriptionstring description of event
xStatetype identifier for member state pointer
xOperatorBlockcode block that makes use of ptr_

Given the type Simulation and the action "abort the simulation", we can create it in code like so, note that the semicolon at the end is required.

DEFINE_SET_STATE_ACTION(Abort, "abort", "abort simulation", Simulation, {
ptr_->abort();
});

This will define the following classes for us:

  • Abort
  • AbortFactory

This action can be registered with the register_action helper function.