Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions include/gz/msgs/Factory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ namespace gz
public: template<typename T>
static std::unique_ptr<T> New(const std::string &_msgType)
{
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Using [" << msgType
<< "] instead." << std::endl;
}
return std::unique_ptr<T>(
static_cast<T*>(New(_msgType).release()));
static_cast<T*>(New(msgType).release()));
}

/// \brief Create a new instance of a message.
Expand All @@ -77,8 +85,16 @@ namespace gz
static std::unique_ptr<T> New(const std::string &_msgType,
const std::string &_args)
{
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Using [" << msgType
<< "] instead." << std::endl;
}
return std::unique_ptr<T>(
static_cast<T*>(New(_msgType, _args).release()));
static_cast<T*>(New(msgType, _args).release()));
}

/// \brief Create a new instance of a message.
Expand Down
47 changes: 37 additions & 10 deletions src/Factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,25 @@ class DynamicFactory
/// type could not be handled.
public: static ProtoUniquePtr New(const std::string &_msgType)
{
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Using [" << msgType << "] instead."
<< std::endl;
}

// Shortcut if the type has been already registered.
std::map<std::string, std::function<ProtoUniquePtr()>>::iterator msgF =
dynamicMsgMap.find(_msgType);
dynamicMsgMap.find(msgType);

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

// Nothing to do if we don't know about this type in the descriptor map.
const google::protobuf::Descriptor *descriptor =
pool.FindMessageTypeByName(_msgType);
pool.FindMessageTypeByName(msgType);
if (!descriptor)
return nullptr;

Expand Down Expand Up @@ -227,33 +236,42 @@ void Factory::Register(const std::string &_msgType,
std::unique_ptr<google::protobuf::Message> Factory::New(
const std::string &_msgType)
{
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Using [" << msgType << "] instead."
<< std::endl;
}

std::unique_ptr<google::protobuf::Message> msg;

std::string type;
// Convert "gz.msgs." to "gz_msgs.".
if (_msgType.find("gz.msgs.") == 0)
if (msgType.find("gz.msgs.") == 0)
{
type = "gz_msgs." + _msgType.substr(8);
type = "gz_msgs." + msgType.substr(8);
}
// Convert ".gz.msgs." to "gz_msgs.".
else if (_msgType.find(".gz.msgs.") == 0)
else if (msgType.find(".gz.msgs.") == 0)
{
type = "gz_msgs." + _msgType.substr(9);
type = "gz_msgs." + msgType.substr(9);
}
else
{
// Fix typenames that are missing "gz_msgs." at the beginning.
if (_msgType.find("gz_msgs.") != 0)
if (msgType.find("gz_msgs.") != 0)
type = "gz_msgs.";
type += _msgType;
type += msgType;
}

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

// Check if we have the message descriptor.
msg = dynamicFactory.New(_msgType);
msg = dynamicFactory.New(msgType);

return msg;
}
Expand All @@ -262,7 +280,16 @@ std::unique_ptr<google::protobuf::Message> Factory::New(
std::unique_ptr<google::protobuf::Message> Factory::New(
const std::string &_msgType, const std::string &_args)
{
std::unique_ptr<google::protobuf::Message> msg = New(_msgType);
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Using [" << msgType << "] instead."
<< std::endl;
}

std::unique_ptr<google::protobuf::Message> msg = New(msgType);
if (msg)
{
google::protobuf::TextFormat::ParseFromString(_args, msg.get());
Expand Down