compatibility.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef MCL_3DL_COMPAT_COMPATIBILITY_H
31 #define MCL_3DL_COMPAT_COMPATIBILITY_H
32 
33 #include <ros/ros.h>
34 
35 #include <string>
36 
37 namespace mcl_3dl_compat
38 {
39 #define STATIC_ASSERT(EXPR) static_assert(EXPR, #EXPR)
40 
41 // Update cycle
42 // 1. Increment current_level and indicate topic changes using mcl_3dl_compat::subscribe/advertise.
43 // Set default_level to supported_level.
44 // 2. Set default_level to current_level.
45 // 3. Increment supported_level and remove old topic names.
46 #ifndef UNDEF_COMPATIBILITY_LEVEL
47 const int current_level = 1;
48 const int supported_level = 0;
50 
51 STATIC_ASSERT(supported_level <= current_level && current_level <= supported_level + 1);
52 STATIC_ASSERT(supported_level <= default_level && default_level <= current_level);
53 #endif
54 
55 int getCompat()
56 {
57  int compat(default_level);
58  ros::NodeHandle("~").param("compatible", compat, compat);
59 
60  return compat;
61 }
63 {
64  if (getCompat() < supported_level)
65  {
66  const std::string message =
67  "======= [Obsolated] your configuration for " + ros::this_node::getName() + " is outdated =======";
68  ROS_FATAL("%s", message.c_str());
69  ros::shutdown();
70  throw std::runtime_error(message);
71  }
72  else if (getCompat() > current_level)
73  {
74  const std::string message =
75  "======= [Unsupported] your configuration for " + ros::this_node::getName() + " is futuredated =======";
76  ROS_FATAL("%s", message.c_str());
77  ros::shutdown();
78  throw std::runtime_error(message);
79  }
80  else if (getCompat() != current_level)
81  {
83  "======= [Deprecated] %s is run in compatible mode =======\n"
84  "=========================================================\n"
85  "Set _compatible:=%d to switch to new topic namespaces.\n"
86  "Compatible mode will be obsolated in the future update.\n"
87  "=========================================================",
88  ros::this_node::getName().c_str(), current_level);
89  }
90 }
92 {
94  return std::string("~/");
95  if (nh.getUnresolvedNamespace() == std::string())
96  return std::string();
97  return nh.getNamespace() + "/";
98 }
99 template <class M, class T>
101  ros::NodeHandle& nh_new,
102  const std::string& topic_new,
103  ros::NodeHandle& nh_old,
104  const std::string& topic_old,
105  uint32_t queue_size,
106  void (T::*fp)(M) const,
107  T* obj,
108  const ros::TransportHints& transport_hints = ros::TransportHints())
109 {
110  if (getCompat() != current_level)
111  {
112  ROS_ERROR(
113  "Use %s (%s%s) topic instead of %s (%s%s)",
114  nh_new.resolveName(topic_new, false).c_str(),
115  getSimplifiedNamespace(nh_new).c_str(), topic_new.c_str(),
116  nh_old.resolveName(topic_old, false).c_str(),
117  getSimplifiedNamespace(nh_old).c_str(), topic_old.c_str());
118  return nh_old.subscribe(topic_old, queue_size, fp, obj, transport_hints);
119  }
120  else
121  {
122  return nh_new.subscribe(topic_new, queue_size, fp, obj, transport_hints);
123  }
124 }
125 template <class M, class T>
127  ros::NodeHandle& nh_new,
128  const std::string& topic_new,
129  ros::NodeHandle& nh_old,
130  const std::string& topic_old,
131  uint32_t queue_size,
132  void (T::*fp)(M),
133  T* obj,
134  const ros::TransportHints& transport_hints = ros::TransportHints())
135 {
136  if (getCompat() != current_level)
137  {
138  ROS_ERROR(
139  "Use %s (%s%s) topic instead of %s (%s%s)",
140  nh_new.resolveName(topic_new, false).c_str(),
141  getSimplifiedNamespace(nh_new).c_str(), topic_new.c_str(),
142  nh_old.resolveName(topic_old, false).c_str(),
143  getSimplifiedNamespace(nh_old).c_str(), topic_old.c_str());
144  return nh_old.subscribe(topic_old, queue_size, fp, obj, transport_hints);
145  }
146  else
147  {
148  return nh_new.subscribe(topic_new, queue_size, fp, obj, transport_hints);
149  }
150 }
151 template <class M>
153  ros::NodeHandle& nh_new,
154  const std::string& topic_new,
155  ros::NodeHandle& nh_old,
156  const std::string& topic_old,
157  uint32_t queue_size,
158  bool latch = false)
159 {
160  if (getCompat() != current_level)
161  {
162  ROS_ERROR(
163  "Use %s (%s%s) topic instead of %s (%s%s)",
164  nh_new.resolveName(topic_new, false).c_str(),
165  getSimplifiedNamespace(nh_new).c_str(), topic_new.c_str(),
166  nh_old.resolveName(topic_old, false).c_str(),
167  getSimplifiedNamespace(nh_old).c_str(), topic_old.c_str());
168  return nh_old.advertise<M>(topic_old, queue_size, latch);
169  }
170  else
171  {
172  return nh_new.advertise<M>(topic_new, queue_size, latch);
173  }
174 }
175 template <class T, class MReq, class MRes>
177  ros::NodeHandle& nh_new,
178  const std::string& service_new,
179  ros::NodeHandle& nh_old,
180  const std::string& service_old,
181  bool (T::*srv_func)(MReq&, MRes&),
182  T* obj)
183 {
184  if (getCompat() != current_level)
185  {
186  ROS_ERROR(
187  "Use %s (%s%s) service instead of %s (%s%s)",
188  nh_new.resolveName(service_new, false).c_str(),
189  getSimplifiedNamespace(nh_new).c_str(), service_new.c_str(),
190  nh_old.resolveName(service_old, false).c_str(),
191  getSimplifiedNamespace(nh_old).c_str(), service_old.c_str());
192  return nh_old.advertiseService(service_old, srv_func, obj);
193  }
194  else
195  {
196  return nh_new.advertiseService(service_new, srv_func, obj);
197  }
198 }
199 
200 template <typename T>
202  ros::NodeHandle& nh,
203  const std::string& param_name_new,
204  const std::string& param_name_old)
205 {
206  if (nh.hasParam(param_name_old))
207  {
208  ROS_ERROR(
209  "Use %s parameter instead of %s",
210  nh.resolveName(param_name_new, false).c_str(),
211  nh.resolveName(param_name_old, false).c_str());
212  if (nh.hasParam(param_name_new))
213  {
214  ROS_ERROR(
215  "%s is also defined. Ignoring %s.",
216  nh.resolveName(param_name_new, false).c_str(),
217  nh.resolveName(param_name_old, false).c_str());
218  return;
219  }
220  T value;
221  nh.getParam(param_name_old, value);
222  nh.setParam(param_name_new, value);
223  }
224 }
225 } // namespace mcl_3dl_compat
226 
227 #endif // MCL_3DL_COMPAT_COMPATIBILITY_H
const std::string & getUnresolvedNamespace() const
std::string getSimplifiedNamespace(ros::NodeHandle &nh)
Definition: compatibility.h:91
#define ROS_FATAL(...)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::string resolveName(const std::string &name, bool remap=true) const
const int default_level
Definition: compatibility.h:49
ROSCPP_DECL const std::string & getName()
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
const int supported_level
Definition: compatibility.h:48
STATIC_ASSERT(supported_level<=current_level &&current_level<=supported_level+1)
ros::ServiceServer advertiseService(ros::NodeHandle &nh_new, const std::string &service_new, ros::NodeHandle &nh_old, const std::string &service_old, bool(T::*srv_func)(MReq &, MRes &), T *obj)
const int current_level
Definition: compatibility.h:47
#define ROS_ERROR_ONCE(...)
const std::string & getNamespace() const
ros::Publisher advertise(ros::NodeHandle &nh_new, const std::string &topic_new, ros::NodeHandle &nh_old, const std::string &topic_old, uint32_t queue_size, bool latch=false)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void checkCompatMode()
Definition: compatibility.h:62
ros::Subscriber subscribe(ros::NodeHandle &nh_new, const std::string &topic_new, ros::NodeHandle &nh_old, const std::string &topic_old, uint32_t queue_size, void(T::*fp)(M) const, T *obj, const ros::TransportHints &transport_hints=ros::TransportHints())
bool getParam(const std::string &key, std::string &s) const
void paramRename(ros::NodeHandle &nh, const std::string &param_name_new, const std::string &param_name_old)
ROSCPP_DECL void shutdown()
bool hasParam(const std::string &key) const
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 8 2019 03:32:36