choreo_param_helpers.h
Go to the documentation of this file.
1 #ifndef FRAMEFAB_PARAM_HELPERS_H
2 #define FRAMEFAB_PARAM_HELPERS_H
3 
4 #include <ros/serialization.h>
5 #include <ros/node_handle.h>
6 #include <fstream>
7 
9 {
10 // Write a message to disk
11 template <class T> inline bool toFile(const std::string& path, const T& msg)
12 {
13  namespace ser = ros::serialization;
14  uint32_t serialize_size = ser::serializationLength(msg);
15  boost::shared_array<uint8_t> buffer(new uint8_t[serialize_size]);
16 
17  ser::OStream stream(buffer.get(), serialize_size);
18  ser::serialize(stream, msg);
19 
20  std::ofstream file(path.c_str(), std::ios::out | std::ios::binary);
21  if (!file)
22  {
23  return false;
24  }
25  else
26  {
27  file.write((char*)buffer.get(), serialize_size);
28  return file.good();
29  }
30 }
31 
32 // Restore a message from disk
33 template <class T> inline bool fromFile(const std::string& path, T& msg)
34 {
35  namespace ser = ros::serialization;
36 
37  std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
38  if (!ifs)
39  {
40  return false;
41  }
42 
43  ifs.seekg(0, std::ios::end);
44  std::streampos end = ifs.tellg();
45  ifs.seekg(0, std::ios::beg);
46  std::streampos begin = ifs.tellg();
47 
48  uint32_t file_size = end - begin;
49 
50  boost::shared_array<uint8_t> ibuffer(new uint8_t[file_size]);
51  ifs.read((char*)ibuffer.get(), file_size);
52  ser::IStream istream(ibuffer.get(), file_size);
53  ser::deserialize(istream, msg);
54  return true;
55 }
56 
57 // for loading parameters from server
58 // parameter loading helper
59 template <typename T> inline bool loadParam(ros::NodeHandle& nh, const std::string& name, T& value)
60 {
61  if (!nh.getParam(name, value))
62  {
63  ROS_WARN_STREAM("Unable to load param '" << name << "' in " << nh.getNamespace());
64  return false;
65  }
66  return true;
67 }
68 
69 inline bool loadBoolParam(ros::NodeHandle& nh, const std::string& name, uint8_t& value)
70 {
71  bool v;
72  if (loadParam(nh, name, v))
73  {
74  value = v; // bool to uint8_t
75  return true;
76  }
77  return false;
78 }
79 
80 } // end namespace choreo_param_helpers
81 
82 #endif
bool loadBoolParam(ros::NodeHandle &nh, const std::string &name, uint8_t &value)
bool fromFile(const std::string &path, T &msg)
bool loadParam(ros::NodeHandle &nh, const std::string &name, T &value)
bool toFile(const std::string &path, const T &msg)
const std::string & getNamespace() const
#define ROS_WARN_STREAM(args)
bool getParam(const std::string &key, std::string &s) const


choreo_param_helpers
Author(s):
autogenerated on Thu Jul 18 2019 03:58:59