frame_id_processor.cpp
Go to the documentation of this file.
1 
26 
27 #include "ros/ros.h"
28 
29 #include <string>
30 
31 namespace message_relay
32 {
33 
34 const boost::unordered_map<std::string, FrameIdProcessor::Operation> FrameIdProcessor::operation_name_map_ =
35  boost::assign::map_list_of("", FrameIdProcessor::NONE)("add", FrameIdProcessor::ADD_PREFIX)
37  ("selective_remove", FrameIdProcessor::SELECTIVE_REMOVE_PREFIX);
38 
39 const boost::unordered_map<FrameIdProcessor::Operation, FrameIdProcessor::Operation>
40  FrameIdProcessor::operation_inverse_map_ = boost::assign::map_list_of
46 
47 FrameIdProcessor::ConstPtr FrameIdProcessor::create(std::string tf_prefix, std::string prefix_operation_string,
48  boost::unordered_set<std::string> global_frame_names)
49 {
50  try
51  {
52  FrameIdProcessor::Operation prefix_operation = operation_name_map_.at(prefix_operation_string);
53  return FrameIdProcessor::create(tf_prefix, prefix_operation, global_frame_names);
54  }
55  catch (const std::out_of_range &ex)
56  {
57  ROS_FATAL_STREAM("Invalid prefix operation " << prefix_operation_string << " specified");
58  throw ex;
59  }
60 }
61 
63  boost::unordered_set<std::string> global_frame_names)
64 {
65  return FrameIdProcessor::ConstPtr(new const FrameIdProcessor(tf_prefix, prefix_operation, global_frame_names));
66 }
67 
69 {
70  if (processor)
71  {
72  return FrameIdProcessor::ConstPtr(new const FrameIdProcessor(processor->tf_prefix_,
73  operation_inverse_map_.at(processor->prefix_operation_), processor->global_frame_names_));
74  }
75  else
76  {
78  }
79 }
80 
81 FrameIdProcessor::FrameIdProcessor(std::string tf_prefix, FrameIdProcessor::Operation prefix_operation,
82  boost::unordered_set<std::string> global_frame_names)
83  : tf_prefix_(tf_prefix), prefix_operation_(prefix_operation),
84  global_frame_names_(global_frame_names)
85 { }
86 
87 void FrameIdProcessor::process(std::string &frame_id) const
88 {
89  switch (prefix_operation_)
90  {
92  break;
93 
95  ROS_DEBUG_STREAM("Checking for prefix in " << frame_id);
96  if (std::mismatch(tf_prefix_.begin(), tf_prefix_.end(), frame_id.begin()).first == tf_prefix_.end())
97  {
98  ROS_DEBUG_STREAM("frame_id " << frame_id << " already contains prefix " << tf_prefix_);
99  break;
100  }
101  else
102  {
103  // fall through to ADD_PREFIX
104  }
106  ROS_DEBUG_STREAM("Adding prefix to " << frame_id);
107  if (!frame_id.empty() && !global_frame_names_.count(frame_id))
108  {
109  frame_id = tf_prefix_ + "/" + frame_id;
110  ROS_DEBUG_STREAM("Result " << frame_id);
111  }
112  else
113  {
114  ROS_DEBUG_STREAM(frame_id << " is a global frame");
115  }
116  break;
117 
119  ROS_DEBUG_STREAM("Checking for prefix in " << frame_id);
120  if (std::mismatch(tf_prefix_.begin(), tf_prefix_.end(), frame_id.begin()).first == tf_prefix_.end())
121  {
122  frame_id = frame_id.substr(tf_prefix_.length() + 1);
123  ROS_DEBUG_STREAM("Result " << frame_id);
124  }
125  else
126  {
127  ROS_DEBUG_STREAM("frame_id " << frame_id << " doesn't contain prefix " << tf_prefix_);
128  }
129  break;
130 
131  case REMOVE_PREFIX:
132  ROS_DEBUG_STREAM("Removing prefix from " << frame_id);
133  if (!global_frame_names_.count(frame_id))
134  {
135  // Check that frame id is at least one character longer than tf_prefix, and that the prefix is followed
136  // by a dividing forward slash
137  if (frame_id.length() > tf_prefix_.length() + 1 && frame_id[tf_prefix_.length()] == '/')
138  {
139  frame_id = frame_id.substr(tf_prefix_.length() + 1);
140  ROS_DEBUG_STREAM("Result " << frame_id);
141  }
142  else
143  {
144  ROS_DEBUG_STREAM("frame_id " << frame_id << " doesn't contain prefix " << tf_prefix_);
145  }
146  }
147  else
148  {
149  ROS_DEBUG_STREAM(frame_id << " is a global frame");
150  }
151  break;
152 
153  default:
154  ROS_ASSERT_MSG(false, "Invalid prefix operation");
155  }
156 }
157 
158 } // namespace message_relay
static const boost::unordered_map< Operation, Operation > operation_inverse_map_
boost::shared_ptr< const FrameIdProcessor > ConstPtr
static const boost::unordered_map< std::string, Operation > operation_name_map_
static ConstPtr create(std::string tf_prefix, std::string prefix_operation_string, boost::unordered_set< std::string > global_frame_names=boost::unordered_set< std::string >())
static ConstPtr inverse(const ConstPtr &processor)
#define ROS_FATAL_STREAM(args)
FrameIdProcessor(std::string tf_prefix, Operation prefix_operation, boost::unordered_set< std::string > global_frame_names)
void process(std::string &frame_id) const
#define ROS_ASSERT_MSG(cond,...)
#define ROS_DEBUG_STREAM(args)
boost::unordered_set< std::string > global_frame_names_


message_relay
Author(s):
autogenerated on Wed Jul 17 2019 03:27:53