ORCA: Optimization-based framework for Robotic Control Applications
ParameterSharedPtr.h
Go to the documentation of this file.
1 #pragma once
3 #include <orca/common/Factory.h>
4 #include <typeinfo>
5 
6 namespace orca
7 {
8 namespace common
9 {
10 
11 inline std::string findType(const YAML::Node& n)
12 {
13  for(auto c : n)
14  {
15  try{
16  if(c.first.as<std::string>() == "type")
17  return c.second.as<std::string>();
18  }catch(...){}
19  }
20  return std::string();
21 }
22 
23 template<class T>
24 class Parameter<std::shared_ptr<T> > : public ParameterBase, public ParameterData< std::shared_ptr<T> >
25 {
26 public:
27  Parameter() {}
28  Parameter(const std::shared_ptr<T >& t)
29  {
30  this->set(t);
31  }
32  bool onLoadFromString(const std::string& s)
33  {
34  YAML::Node node = YAML::Load(s);
35  auto type_name = findType(node);
36 
37  if(type_name.empty())
38  utils::orca_throw(utils::Formatter() << "Could not find \"type\" key in the yaml file for param "
39  << getName() << " of type " << typeid(T).name() << "::Ptr"
40  << "\nConfig provided : \n" << s );
41 
42  LOG_DEBUG << "Param " << getName() << " is of type " << type_name;
43  // If already set, then we just need to configure it
44  if(this->isSet())
45  {
46  LOG_DEBUG << "Param " << getName() << " is already set, just configuring " << type_name;
47  return this->get()->configureFromString(s);
48  }
49  // If not set, we have to create it from the factory
50  auto task_base = Factory::Instance()->createPtr(getName(),type_name);
51 
52  if(!task_base)
53  utils::orca_throw(utils::Formatter() << "Param " << getName() << " Could not create a class of type " << type_name << "" );
54 
55  LOG_DEBUG << "Param " << getName() << " created class of type " << type_name << ", now configuring it if it has params itself.";
56 
57  if(!task_base->configureFromString(s))
58  return false;
59 
60  this->set( std::dynamic_pointer_cast<T>( task_base ));
61  return true;
62  }
63 
64  void print() const
65  {
66  std::cout << getName() << " [ " << typeid(T).name() << "::Ptr" << '\n';
67  }
68 
69  bool isSet() const
70  {
72  }
73  std::shared_ptr<T>& get()
74  {
75  try {
77  } catch (std::exception& e) {
78  utils::orca_throw(utils::Formatter() << "Parameter '" << getName() << "' : " << e.what());
79  }
80  }
81  const std::shared_ptr<T>& get() const
82  {
83  try {
85  } catch (std::exception& e) {
86  utils::orca_throw(utils::Formatter() << "Parameter '" << getName() << "' : " << e.what());
87  }
88  }
89  template<class T2>
90  Parameter<std::shared_ptr<T> >& operator=(std::shared_ptr<T2> val)
91  {
92  this->set(val);
93  return *this;
94  }
95 };
96 
97 } // namespace common
98 } // namespace orca
const std::string & getName() const
Definition: ParameterBase.h:50
This class holds the data for a parameter of any type.
Definition: ParameterData.h:14
std::shared_ptr< ConfigurableOrcaObject > createPtr(const std::string &instance_name, const std::string &class_name)
Definition: Factory.h:21
void orca_throw(const std::string &arg)
Definition: Utils.h:153
static Factory * Instance()
Definition: Factory.h:16
bool onLoadFromString(const std::string &s)
Definition: ParameterSharedPtr.h:32
Parameter(const std::shared_ptr< T > &t)
Definition: ParameterSharedPtr.h:28
T & get()
Definition: Parameter.h:149
std::string findType(const YAML::Node &n)
Definition: ParameterSharedPtr.h:11
Definition: Utils.h:105
Parameter< std::shared_ptr< T > > & operator=(std::shared_ptr< T2 > val)
Definition: ParameterSharedPtr.h:90
Definition: CartesianAccelerationPID.h:44
Parameter()
Definition: ParameterSharedPtr.h:27
ParameterBase is the public interface to any parameter.
Definition: ParameterBase.h:21
This class holds the conversion from a string (YAML string) to the data type.
Definition: Parameter.h:113
void print() const
Definition: ParameterSharedPtr.h:64
bool isSet() const
Definition: ParameterSharedPtr.h:69
bool isSet() const
Definition: Parameter.h:144