Plugin.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 SDF_PLUGIN_HH_
18#define SDF_PLUGIN_HH_
19
20#include <memory>
21#include <string>
22#include <vector>
23
24#include <sdf/Element.hh>
25#include <sdf/Error.hh>
26#include <sdf/parser.hh>
27#include <sdf/Types.hh>
28#include "sdf/sdf_config.h"
29#include "sdf/system_util.hh"
30
31#ifdef _WIN32
32// Disable warning C4251 which is triggered by
33// std::unique_ptr
34#pragma warning(push)
35#pragma warning(disable: 4251)
36#endif
37
38namespace sdf
39{
40 // Inline bracket to help doxygen filtering.
41 inline namespace SDF_VERSION_NAMESPACE {
42 //
43 class PluginPrivate;
44
46 {
48 public: Plugin();
49
51 public: ~Plugin();
52
55 public: Plugin(const Plugin &_plugin);
56
59 public: Plugin(Plugin &&_plugin) noexcept;
60
68 public: Plugin(const std::string &_filename, const std::string &_name,
69 const std::string &_xmlContent = "");
70
79 public: Plugin(sdf::Errors &_errors, const std::string &_filename,
80 const std::string &_name,
81 const std::string &_xmlContent = "");
82
89 public: Errors Load(ElementPtr _sdf);
90
95 public: const std::string &Name() const;
96
101 public: void SetName(const std::string &_name);
102
105 public: const std::string &Filename() const;
106
109 public: void ClearContents();
110
114 public: const std::vector<sdf::ElementPtr> &Contents() const;
115
120 public: void InsertContent(const sdf::ElementPtr _elem);
121
127 public: void InsertContent(sdf::Errors &_errors,
128 const sdf::ElementPtr _elem);
129
137 public: bool InsertContent(const std::string _content);
138
147 public: bool InsertContent(sdf::Errors &_errors,
148 const std::string _content);
149
153 public: void SetFilename(const std::string &_filename);
154
159 public: sdf::ElementPtr Element() const;
160
166 public: sdf::ElementPtr ToElement() const;
167
174 public: sdf::ElementPtr ToElement(sdf::Errors &_errors) const;
175
179 public: Plugin &operator=(const Plugin &_plugin);
180
184 public: Plugin &operator=(Plugin &&_plugin) noexcept;
185
190 public: bool operator==(const Plugin &_plugin) const;
191
196 public: bool operator!=(const Plugin &_plugin) const;
197
201 public: friend std::ostream &operator<<(std::ostream& _out,
202 const sdf::Plugin &_plugin)
203 {
204 return _out << _plugin.ToElement()->ToString("");
205 }
206
210 public: friend std::istream &operator>>(std::istream &_in,
211 sdf::Plugin &_plugin)
212 {
213 std::ostringstream stream;
214 stream << "<sdf version='" << SDF_VERSION << "'>";
215 stream << std::string(std::istreambuf_iterator<char>(_in), {});
216 stream << "</sdf>";
217
218 sdf::SDFPtr sdfParsed(new sdf::SDF());
219 sdf::init(sdfParsed);
220 bool result = sdf::readString(stream.str(), sdfParsed);
221 if (!result)
222 return _in;
223
224 _plugin.ClearContents();
225 _plugin.Load(sdfParsed->Root()->GetFirstElement());
226
227 return _in;
228 }
229
237 private: void Init(sdf::Errors &_errors, const std::string &_filename,
238 const std::string &_name, const std::string &_xmlContent);
239
241 private: std::unique_ptr<sdf::PluginPrivate> dataPtr;
242 };
243
245 using Plugins = std::vector<Plugin>;
246}
247}
248
249#ifdef _WIN32
250#pragma warning(pop)
251#endif
252
253#endif
Definition: Plugin.hh:46
sdf::ElementPtr ToElement() const
Create and return an SDF element filled with data from this plugin.
void SetFilename(const std::string &_filename)
Set the filename of the shared library.
Plugin & operator=(const Plugin &_plugin)
Copy assignment operator.
bool InsertContent(const std::string _content)
Insert XML content into this plugin.
Plugin & operator=(Plugin &&_plugin) noexcept
Move assignment operator.
sdf::ElementPtr Element() const
Get a pointer to the SDF element that was used during load.
friend std::istream & operator>>(std::istream &_in, sdf::Plugin &_plugin)
Input stream operator for a Plugin.
Definition: Plugin.hh:210
void ClearContents()
Remove the contents of the plugin, this is everything that is a child element of the <plugin>.
const std::string & Name() const
Get the name of the plugin.
sdf::ElementPtr ToElement(sdf::Errors &_errors) const
Create and return an SDF element filled with data from this plugin.
Plugin(const std::string &_filename, const std::string &_name, const std::string &_xmlContent="")
A constructor that initializes the plugin's filename, name, and optionally the content.
void InsertContent(sdf::Errors &_errors, const sdf::ElementPtr _elem)
Insert an element into the plugin content.
const std::string & Filename() const
Get the filename of the shared library.
bool operator!=(const Plugin &_plugin) const
Plugin inequality operator.
friend std::ostream & operator<<(std::ostream &_out, const sdf::Plugin &_plugin)
Output stream operator for a Plugin.
Definition: Plugin.hh:201
Plugin(Plugin &&_plugin) noexcept
Move constructor.
bool InsertContent(sdf::Errors &_errors, const std::string _content)
Insert XML content into this plugin.
bool operator==(const Plugin &_plugin) const
Plugin equality operator.
Errors Load(ElementPtr _sdf)
Load the plugin based on a element pointer.
void SetName(const std::string &_name)
Set the name of the plugin.
Plugin()
Default constructor.
void InsertContent(const sdf::ElementPtr _elem)
Insert an element into the plugin content.
const std::vector< sdf::ElementPtr > & Contents() const
Get the plugin contents.
Plugin(const Plugin &_plugin)
Copy constructor.
Plugin(sdf::Errors &_errors, const std::string &_filename, const std::string &_name, const std::string &_xmlContent="")
A constructor that initializes the plugin's filename, name, and optionally the content.
Base SDF class.
Definition: SDFImpl.hh:119
GZ_SDFORMAT_VISIBLE bool init(SDFPtr _sdf)
Initialize the SDF interface from the embedded root spec file.
GZ_SDFORMAT_VISIBLE bool readString(const std::string &_xmlString, SDFPtr _sdf, Errors &_errors)
Populate the SDF values from a string.
std::shared_ptr< SDF > SDFPtr
Definition: SDFImpl.hh:52
std::vector< Plugin > Plugins
A vector of Plugin.
Definition: Plugin.hh:245
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:95
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:55
namespace for Simulation Description Format parser
Definition: Actor.hh:35
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:25