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 
38 namespace 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 
77  public: Errors Load(ElementPtr _sdf);
78 
83  public: std::string Name() const;
84 
89  public: void SetName(const std::string &_name);
90 
93  public: std::string Filename() const;
94 
97  public: void ClearContents();
98 
102  public: const std::vector<sdf::ElementPtr> &Contents() const;
103 
108  public: void InsertContent(const sdf::ElementPtr _elem);
109 
117  public: bool InsertContent(const std::string _content);
118 
122  public: void SetFilename(const std::string &_filename);
123 
128  public: sdf::ElementPtr Element() const;
129 
135  public: sdf::ElementPtr ToElement() const;
136 
140  public: Plugin &operator=(const Plugin &_plugin);
141 
145  public: Plugin &operator=(Plugin &&_plugin) noexcept;
146 
151  public: bool operator==(const Plugin &_plugin) const;
152 
157  public: bool operator!=(const Plugin &_plugin) const;
158 
162  public: friend std::ostream &operator<<(std::ostream& _out,
163  const sdf::Plugin &_plugin)
164  {
165  return _out << _plugin.ToElement()->ToString("");
166  }
167 
171  public: friend std::istream &operator>>(std::istream &_in,
172  sdf::Plugin &_plugin)
173  {
174  std::ostringstream stream;
175  stream << "<sdf version='" << SDF_VERSION << "'>";
176  stream << std::string(std::istreambuf_iterator<char>(_in), {});
177  stream << "</sdf>";
178 
179  sdf::SDFPtr sdfParsed(new sdf::SDF());
180  sdf::init(sdfParsed);
181  bool result = sdf::readString(stream.str(), sdfParsed);
182  if (!result)
183  return _in;
184 
185  _plugin.ClearContents();
186  _plugin.Load(sdfParsed->Root()->GetFirstElement());
187 
188  return _in;
189  }
190 
192  std::unique_ptr<sdf::PluginPrivate> dataPtr;
193  };
194 
196  using Plugins = std::vector<Plugin>;
197 }
198 }
199 
200 #ifdef _WIN32
201 #pragma warning(pop)
202 #endif
203 
204 #endif
sdf::SDF_VERSION_NAMESPACE::SDFPtr
std::shared_ptr< SDF > SDFPtr
Definition: SDFImpl.hh:52
sdf::SDF_VERSION_NAMESPACE::Plugin::Load
Errors Load(ElementPtr _sdf)
Load the plugin based on a element pointer.
Error.hh
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:34
sdf::SDF_VERSION_NAMESPACE::init
IGNITION_SDFORMAT_VISIBLE bool init(SDFPtr _sdf)
Initialize the SDF interface from the embedded root spec file.
sdf::SDF_VERSION_NAMESPACE::SDF
Base SDF class.
Definition: SDFImpl.hh:118
sdf::SDF_VERSION_NAMESPACE::Element
class IGNITION_SDFORMAT_VISIBLE Element
Definition: Element.hh:50
sdf::SDF_VERSION_NAMESPACE::readString
IGNITION_SDFORMAT_VISIBLE bool readString(const std::string &_xmlString, SDFPtr _sdf, Errors &_errors)
Populate the SDF values from a string.
Types.hh
sdf_config.h
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:25
sdf::SDF_VERSION_NAMESPACE::Plugin::operator>>
friend std::istream & operator>>(std::istream &_in, sdf::Plugin &_plugin)
Input stream operator for a Plugin.
Definition: Plugin.hh:171
sdf::SDF_VERSION_NAMESPACE::Plugin
Definition: Plugin.hh:45
sdf::SDF_VERSION_NAMESPACE::Plugin::dataPtr
std::unique_ptr< sdf::PluginPrivate > dataPtr
Private data pointer.
Definition: Plugin.hh:192
sdf::SDF_VERSION_NAMESPACE::Errors
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:106
sdf::SDF_VERSION_NAMESPACE::Plugin::ClearContents
void ClearContents()
Remove the contents of the plugin, this is everything that is a child element of the <plugin>.
sdf::SDF_VERSION_NAMESPACE::Plugin::operator<<
friend std::ostream & operator<<(std::ostream &_out, const sdf::Plugin &_plugin)
Output stream operator for a Plugin.
Definition: Plugin.hh:162
system_util.hh
sdf::SDF_VERSION_NAMESPACE::Plugins
std::vector< Plugin > Plugins
A vector of Plugin.
Definition: Plugin.hh:196
sdf::SDF_VERSION_NAMESPACE::ElementPtr
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:54
sdf::SDF_VERSION_NAMESPACE::Plugin::ToElement
sdf::ElementPtr ToElement() const
Create and return an SDF element filled with data from this plugin.
Element.hh
parser.hh