ros_param_data_source.hpp
Go to the documentation of this file.
1 /*
2  * (C) 2020, Intermodalics BVBA
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the names of its contributors
13  * may be used to endorse or promote products derived from this software
14  * without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __RTT_ROSPARAM__ROS_PARAM_DATA_SOURCE_HPP
31 #define __RTT_ROSPARAM__ROS_PARAM_DATA_SOURCE_HPP
32 
34 #include "rtt/Logger.hpp"
35 
36 #include "ros/param.h"
37 
38 using namespace RTT;
39 
40 namespace rtt_rosparam {
41 
53 template<typename T>
56 {
60  std::string mparam_name_;
62 
63 public:
64 
65  typedef boost::intrusive_ptr< RosParamDataSource<T> > shared_ptr;
66 
68  {
69  }
70 
71  RosParamDataSource(std::string param_name_id)
72  : mparam_name_(param_name_id),
73  mcached_data_()
74  {
75  }
76 
77  typename internal::DataSource<T>::result_t get() const
78  {
79  (void) evaluate();
80  return value();
81  }
82 
83  bool evaluate() const
84  {
85  if (!ros::param::getCached(mparam_name_, mcached_data_)) {
86  RTT::log(RTT::Error) << "The value of parameter " + mparam_name_ + " could not be fetched." << RTT::endlog();
87  return false;
88  }
89  return true;
90  }
91 
93  {
94  return mcached_data_;
95  }
96 
97  // There is not referred element, no allocation exists for this data source and it is not an alias.
98  // But it needs to return something, because otherwise the .write() calls that require a rvalue()
99  // would fail. So we use an internal cache
101  {
102  return mcached_data_;
103  }
104 
106  {
107  set() = t;
108  updated();
109  }
110 
111  // The referenced element is a cache, since the source of the data is external.
112  // After working with the reference, if something was assigned, the data
113  // source may need a call to updated() after set()
115  {
116  return mcached_data_;
117  }
118 
119  void updated()
120  {
121  ros::param::set(mparam_name_, mcached_data_);
122  }
123 
124  virtual RosParamDataSource<T>* clone() const
125  {
126  return new RosParamDataSource<T>(this->mparam_name_);
127  }
128 
129  virtual RosParamDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& already_cloned ) const
130  {
131  // Just like in ValueDataSource
132  if (already_cloned[this] != 0) {
133  assert(dynamic_cast<RosParamDataSource<T>*>(already_cloned[this]) == static_cast<RosParamDataSource<T>*>(already_cloned[this]));
134  return static_cast<RosParamDataSource<T>*>(already_cloned[this]);
135  }
136  already_cloned[this] = const_cast<RosParamDataSource<T>*>(this);
137  return const_cast<RosParamDataSource<T>*>(this);
138  }
139 
140 }; // class RosParamDataSource
141 
142 } // namespace rtt_rosparam
143 
144 #endif // __RTT_ROSPARAM__ROS_PARAM_DATA_SOURCE_HPP
boost::call_traits< value_t >::reference reference_t
internal::DataSource< T >::const_reference_t rvalue() const
internal::DataSource< T >::result_t value() const
ROSCPP_DECL void set(const std::string &key, const XmlRpc::XmlRpcValue &v)
virtual RosParamDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &already_cloned) const
RosParamDataSource(std::string param_name_id)
res evaluate()
ROSCPP_DECL bool getCached(const std::string &key, std::string &s)
virtual RosParamDataSource< T > * clone() const
internal::DataSource< T >::value_t mcached_data_
boost::intrusive_ptr< RosParamDataSource< T > > shared_ptr
details::GetConstRef< T >::type const_reference_t
static Logger & log()
static Logger::LogFunction endlog()
boost::call_traits< value_t >::param_type param_t


rtt_rosparam
Author(s): Ruben Smits
autogenerated on Mon May 10 2021 02:45:11