Gazebo Msgs

API Reference

11.0.1
MessageFactory.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 Open Source Robotics Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16*/
17#ifndef GZ_MSGS_MESSAGE_FACTORY_HH_
18#define GZ_MSGS_MESSAGE_FACTORY_HH_
19
20#include <functional>
21#include <map>
22#include <memory>
23#include <string>
24#include <vector>
25
26#include "gz/msgs/config.hh"
27#include "gz/msgs/Export.hh"
28#include "gz/msgs/detail/dynamic_message_cast.hh"
29#include <gz/utils/ImplPtr.hh>
30
31namespace gz::msgs {
33 class DynamicFactory;
34
35 // Inline bracket to help doxygen filtering.
36 inline namespace GZ_MSGS_VERSION_NAMESPACE {
37
43 class GZ_MSGS_VISIBLE MessageFactory
44 {
46 public: using Message = google::protobuf::Message;
47
50
52 public: using FactoryFn = std::function<MessagePtr(void)>;
53
56
58 public: MessageFactory();
59
61 public: ~MessageFactory();
62
66 public: void Register(const std::string &_msgType, FactoryFn _factoryFn);
67
72 public: template<typename T>
74 {
75 return detail::dynamic_message_cast<T>(New(_msgType));
76 }
77
83 public: template<typename T>
85 const std::string &_args)
86 {
87 return detail::dynamic_message_cast<T>(New(_msgType, _args));
88 }
89
94 public: MessagePtr New(const std::string &_msgType);
95
102 const std::string &_msgType, const std::string &_args);
103
106 public: void Types(std::vector<std::string> &_types);
107
111 public: void LoadDescriptors(const std::string &_paths);
112
114 private: FactoryFnCollection msgMap;
115
117 GZ_UTILS_UNIQUE_IMPL_PTR_FWD(gz::msgs::DynamicFactory, dynamicFactory)
118 };
119}
120} // namespace gz::msgs
121#endif // GZ_MSGS_MESSAGE_FACTORY_HH_