Skip to content

Commit 717d39f

Browse files
authored
[ign to gz] Factory::New accept ignition with warning (#281)
Signed-off-by: Louise Poubel <louise@openrobotics.org>
1 parent 0a09d3d commit 717d39f

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

include/gz/msgs/Factory.hh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@ namespace gz
6464
public: template<typename T>
6565
static std::unique_ptr<T> New(const std::string &_msgType)
6666
{
67+
auto msgType = _msgType;
68+
if (msgType.find("ignition") == 0)
69+
{
70+
msgType.replace(0, 8, "gz");
71+
std::cerr << "Trying to create deprecated message type ["
72+
<< _msgType << "]. Using [" << msgType
73+
<< "] instead." << std::endl;
74+
}
6775
return std::unique_ptr<T>(
68-
static_cast<T*>(New(_msgType).release()));
76+
static_cast<T*>(New(msgType).release()));
6977
}
7078

7179
/// \brief Create a new instance of a message.
@@ -77,8 +85,16 @@ namespace gz
7785
static std::unique_ptr<T> New(const std::string &_msgType,
7886
const std::string &_args)
7987
{
88+
auto msgType = _msgType;
89+
if (msgType.find("ignition") == 0)
90+
{
91+
msgType.replace(0, 8, "gz");
92+
std::cerr << "Trying to create deprecated message type ["
93+
<< _msgType << "]. Using [" << msgType
94+
<< "] instead." << std::endl;
95+
}
8096
return std::unique_ptr<T>(
81-
static_cast<T*>(New(_msgType, _args).release()));
97+
static_cast<T*>(New(msgType, _args).release()));
8298
}
8399

84100
/// \brief Create a new instance of a message.

src/Factory.cc

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,25 @@ class DynamicFactory
161161
/// type could not be handled.
162162
public: static ProtoUniquePtr New(const std::string &_msgType)
163163
{
164+
auto msgType = _msgType;
165+
if (msgType.find("ignition") == 0)
166+
{
167+
msgType.replace(0, 8, "gz");
168+
std::cerr << "Trying to create deprecated message type ["
169+
<< _msgType << "]. Using [" << msgType << "] instead."
170+
<< std::endl;
171+
}
172+
164173
// Shortcut if the type has been already registered.
165174
std::map<std::string, std::function<ProtoUniquePtr()>>::iterator msgF =
166-
dynamicMsgMap.find(_msgType);
175+
dynamicMsgMap.find(msgType);
167176

168177
if (msgF != dynamicMsgMap.end())
169178
return msgF->second();
170179

171180
// Nothing to do if we don't know about this type in the descriptor map.
172181
const google::protobuf::Descriptor *descriptor =
173-
pool.FindMessageTypeByName(_msgType);
182+
pool.FindMessageTypeByName(msgType);
174183
if (!descriptor)
175184
return nullptr;
176185

@@ -227,33 +236,42 @@ void Factory::Register(const std::string &_msgType,
227236
std::unique_ptr<google::protobuf::Message> Factory::New(
228237
const std::string &_msgType)
229238
{
239+
auto msgType = _msgType;
240+
if (msgType.find("ignition") == 0)
241+
{
242+
msgType.replace(0, 8, "gz");
243+
std::cerr << "Trying to create deprecated message type ["
244+
<< _msgType << "]. Using [" << msgType << "] instead."
245+
<< std::endl;
246+
}
247+
230248
std::unique_ptr<google::protobuf::Message> msg;
231249

232250
std::string type;
233251
// Convert "gz.msgs." to "gz_msgs.".
234-
if (_msgType.find("gz.msgs.") == 0)
252+
if (msgType.find("gz.msgs.") == 0)
235253
{
236-
type = "gz_msgs." + _msgType.substr(8);
254+
type = "gz_msgs." + msgType.substr(8);
237255
}
238256
// Convert ".gz.msgs." to "gz_msgs.".
239-
else if (_msgType.find(".gz.msgs.") == 0)
257+
else if (msgType.find(".gz.msgs.") == 0)
240258
{
241-
type = "gz_msgs." + _msgType.substr(9);
259+
type = "gz_msgs." + msgType.substr(9);
242260
}
243261
else
244262
{
245263
// Fix typenames that are missing "gz_msgs." at the beginning.
246-
if (_msgType.find("gz_msgs.") != 0)
264+
if (msgType.find("gz_msgs.") != 0)
247265
type = "gz_msgs.";
248-
type += _msgType;
266+
type += msgType;
249267
}
250268

251269
// Create a new message if a FactoryFn has been assigned to the message type
252270
if (msgMap->find(type) != msgMap->end())
253271
return ((*msgMap)[type]) ();
254272

255273
// Check if we have the message descriptor.
256-
msg = dynamicFactory.New(_msgType);
274+
msg = dynamicFactory.New(msgType);
257275

258276
return msg;
259277
}
@@ -262,7 +280,16 @@ std::unique_ptr<google::protobuf::Message> Factory::New(
262280
std::unique_ptr<google::protobuf::Message> Factory::New(
263281
const std::string &_msgType, const std::string &_args)
264282
{
265-
std::unique_ptr<google::protobuf::Message> msg = New(_msgType);
283+
auto msgType = _msgType;
284+
if (msgType.find("ignition") == 0)
285+
{
286+
msgType.replace(0, 8, "gz");
287+
std::cerr << "Trying to create deprecated message type ["
288+
<< _msgType << "]. Using [" << msgType << "] instead."
289+
<< std::endl;
290+
}
291+
292+
std::unique_ptr<google::protobuf::Message> msg = New(msgType);
266293
if (msg)
267294
{
268295
google::protobuf::TextFormat::ParseFromString(_args, msg.get());

0 commit comments

Comments
 (0)