Ignition Gazebo

API Reference

6.9.0
components/Model.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 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 IGNITION_GAZEBO_COMPONENTS_MODEL_HH_
18 #define IGNITION_GAZEBO_COMPONENTS_MODEL_HH_
19 
20 #include <string>
21 
22 #include <sdf/Model.hh>
23 #include <sdf/Root.hh>
24 
27 #include <ignition/gazebo/config.hh>
28 
29 namespace ignition
30 {
31 namespace gazebo
32 {
33 // Inline bracket to help doxygen filtering.
34 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
35 namespace serializers
36 {
38  {
43  public: static std::ostream &Serialize(std::ostream &_out,
44  const sdf::Model &_model)
45  {
46  sdf::ElementPtr modelElem = _model.Element();
47  if (!modelElem)
48  {
49  ignerr << "Unable to serialize sdf::Model" << std::endl;
50  return _out;
51  }
52 
53  _out << "<?xml version=\"1.0\" ?>"
54  << "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
55  << modelElem->ToString("")
56  << "</sdf>";
57  return _out;
58  }
59 
64  public: static std::istream &Deserialize(std::istream &_in,
65  sdf::Model &_model)
66  {
67  sdf::Root root;
69 
70  sdf::Errors errors = root.LoadSdfString(sdf);
71  if (!root.Model())
72  {
73  ignerr << "Unable to unserialize sdf::Model" << std::endl;
74  return _in;
75  }
76 
77  _model = *root.Model();
78  return _in;
79  }
80  };
81 }
82 
83 namespace components
84 {
87  IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Model", Model)
88 
89 
90  using ModelSdf = Component<sdf::Model,
91  class ModelTag,
92  serializers::SdfModelSerializer>;
93  IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.ModelSdf", ModelSdf)
94 }
95 }
96 }
97 }
98 
99 #endif
static std::istream & Deserialize(std::istream &_in, sdf::Model &_model)
Deserialization for sdf::Model.
Definition: components/Model.hh:64
T endl(T... args)
STL class.
STL class.
Component< NoData, class ModelTag > Model
A component that identifies an entity as being a model.
Definition: components/Model.hh:86
#define IGN_GAZEBO_REGISTER_COMPONENT(_compType, _classname)
Static component registration macro.
Definition: Factory.hh:373
Definition: components/Model.hh:37
#define ignerr
A component type that wraps any data type. The intention is for this class to be used to create simpl...
Definition: Component.hh:324
This library is part of the Ignition Robotics project.
STL class.
static std::ostream & Serialize(std::ostream &_out, const sdf::Model &_model)
Serialization for sdf::Model.
Definition: components/Model.hh:43