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  return __fix_warnings__;
81  }
82  const std::shared_ptr<T>& get() const
83  {
84  try {
86  } catch (std::exception& e) {
87  utils::orca_throw(utils::Formatter() << "Parameter '" << getName() << "' : " << e.what());
88  }
89  return __fix_warnings__;
90  }
91  template<class T2>
92  Parameter<std::shared_ptr<T> >& operator=(std::shared_ptr<T2> val)
93  {
94  this->set(val);
95  return *this;
96  }
97 private:
98  std::shared_ptr<T> __fix_warnings__ = nullptr;
99 };
100 
101 } // namespace common
102 } // 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:151
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:103
bool isSet() const
Definition: Parameter.h:144
void print() const
Definition: ParameterSharedPtr.h:64
Parameter< std::shared_ptr< T > > & operator=(std::shared_ptr< T2 > val)
Definition: ParameterSharedPtr.h:92
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
bool isSet() const
Definition: ParameterSharedPtr.h:69