-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathDynamicFactory.cc
More file actions
180 lines (154 loc) · 5.38 KB
/
DynamicFactory.cc
File metadata and controls
180 lines (154 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <algorithm>
#include <fstream>
#include <filesystem>
#include <vector>
#include "DynamicFactory.hh"
#include "gz/utils/Environment.hh"
#include <gz/msgs/InstallationDirectories.hh>
namespace {
constexpr const char * kDescriptorEnv = "GZ_DESCRIPTOR_PATH";
#ifdef _WIN32
constexpr char kEnvironmentVariableSeparator = ';';
#else
constexpr char kEnvironmentVariableSeparator = ':';
#endif
//////////////////////////////////////////////////
/// \brief split at a one character delimiter to get a vector of something
/// \param[in] _orig The string to split
/// \param[in] _delim a character to split the string at
/// \returns vector of split pieces of the string excluding the delimiter
std::vector<std::string> split(const std::string &_orig, char _delim)
{
std::vector<std::string> pieces;
size_t pos1 = 0;
size_t pos2 = _orig.find(_delim);
while (pos2 != std::string::npos)
{
pieces.push_back(_orig.substr(pos1, pos2-pos1));
pos1 = pos2+1;
pos2 = _orig.find(_delim, pos2+1);
}
pieces.push_back(_orig.substr(pos1, _orig.size()-pos1));
return pieces;
}
} // namespace
namespace gz::msgs {
//////////////////////////////////////////////////
DynamicFactory::DynamicFactory()
{
// Try to get the list of paths from an environment variable.
std::string descPaths;
if (gz::utils::env(kDescriptorEnv, descPaths)) {
// Load all the descriptors found in the paths set with GZ_DESCRIPTOR_PATH.
this->LoadDescriptors(descPaths);
}
auto globalPath =
std::filesystem::path(gz::msgs::getInstallPrefix()) /
"share" / "gz" / "protos";
if (std::filesystem::exists(globalPath))
{
// Load descriptors from the global share path
this->LoadDescriptors(globalPath.string());
}
}
//////////////////////////////////////////////////
void DynamicFactory::LoadDescriptors(const std::string &_paths)
{
if (_paths.empty())
return;
// Split all the directories containing .desc files.
std::vector<std::string> descDirs =
split(_paths, kEnvironmentVariableSeparator);
for (const std::string &descDir : descDirs)
{
for (auto const &dirIter : std::filesystem::directory_iterator{descDir})
{
// Ignore files without the .desc extension.
if (dirIter.path().extension() != ".desc" &&
dirIter.path().extension() != ".gz_desc")
continue;
std::ifstream ifs(dirIter.path().string(), std::ifstream::in);
if (!ifs.is_open())
{
std::cerr << "DynamicFactory(): Unable to open ["
<< dirIter.path() << "]"
<< std::endl;
continue;
}
google::protobuf::FileDescriptorSet fileDescriptorSet;
if (!fileDescriptorSet.ParseFromIstream(&ifs))
{
std::cerr << "DynamicFactory(): Unable to parse descriptor set from ["
<< dirIter.path() << "]" << std::endl;
continue;
}
for (const google::protobuf::FileDescriptorProto &fileDescriptorProto :
fileDescriptorSet.file())
{
// If the descriptor already exists in the database, then skip it.
// This may happen as gz_desc files can potentially contain the
// transitive message definitions
google::protobuf::FileDescriptorProto checkDescriptorProto;
if (this->db.FindFileByName(
fileDescriptorProto.name(), &checkDescriptorProto))
{
continue;
}
if (!static_cast<bool>(pool.BuildFile(fileDescriptorProto)))
{
std::cerr << "DynamicFactory(). Unable to place descriptors from ["
<< dirIter.path()
<< "] in the descriptor pool" << std::endl;
}
this->db.Add(fileDescriptorProto);
}
}
}
}
//////////////////////////////////////////////////
void DynamicFactory::Types(std::vector<std::string> &_types)
{
std::vector<std::string> messages;
this->db.FindAllMessageNames(&messages);
std::copy(messages.begin(), messages.end(), std::back_inserter(_types));
}
//////////////////////////////////////////////////
DynamicFactory::MessagePtr DynamicFactory::New(const std::string &_msgType)
{
// Shortcut if the type has been already registered.
auto messageIt = dynamicMsgMap.find(_msgType);
if (messageIt != dynamicMsgMap.end())
return messageIt ->second();
// Nothing to do if we don't know about this type in the descriptor map.
const auto *descriptor = pool.FindMessageTypeByName(_msgType);
if (!static_cast<bool>(descriptor))
return nullptr;
google::protobuf::Message *msgPtr(
dynamicMessageFactory.GetPrototype(descriptor)->New());
// Create the lambda for registration purposes.
auto f = [msgPtr]() -> MessagePtr
{
MessagePtr ptr(msgPtr->New());
return ptr;
};
// Register the new type for the future.
dynamicMsgMap[_msgType] = f;
return f();
}
} // namespace gz::msgs