Gazebo Sensors

API Reference

8.2.0
SensorFactory.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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_SENSORS_SENSORFACTORY_HH_
18 #define GZ_SENSORS_SENSORFACTORY_HH_
19 
20 #include <memory>
21 #include <string>
22 #include <type_traits>
23 #include <sdf/sdf.hh>
24 
25 #include <gz/common/Console.hh>
26 #include <gz/utils/SuppressWarning.hh>
27 
28 #include <gz/sensors/config.hh>
29 #include <gz/sensors/Export.hh>
30 
31 #include "gz/sensors/Sensor.hh"
32 
33 namespace gz
34 {
35  namespace sensors
36  {
37  // Inline bracket to help doxygen filtering.
38  inline namespace GZ_SENSORS_VERSION_NAMESPACE {
39  // forward declaration
40  class SensorFactoryPrivate;
41 
45  // After removing plugin functionality, the sensor factory class doesn't
46  // hold any internal state. Consider converting the functionality in this
47  // class to helper functions.
48  class GZ_SENSORS_VISIBLE SensorFactory
49  {
51  public: SensorFactory();
52 
54  public: ~SensorFactory();
55 
67  public: template<typename SensorType>
68  std::unique_ptr<SensorType> CreateSensor(const sdf::Sensor &_sdf)
69  {
70  auto sensor = std::make_unique<SensorType>();
71 
72  if (nullptr == sensor)
73  {
74  gzerr << "Failed to create sensor [" << _sdf.Name()
75  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
76  return nullptr;
77  }
78 
79  if (!sensor->Load(_sdf))
80  {
81  gzerr << "Failed to load sensor [" << _sdf.Name()
82  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
83  return nullptr;
84  }
85 
86  if (!sensor->Init())
87  {
88  gzerr << "Failed to initialize sensor [" << _sdf.Name()
89  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
90  return nullptr;
91  }
92 
93  return sensor;
94  }
95 
102  public: template<typename SensorType>
104  {
105  if (nullptr == _sdf)
106  {
107  gzerr << "Failed to create sensor, received null SDF "
108  << "pointer." << std::endl;
109  return nullptr;
110  }
111 
112  auto sensor = std::make_unique<SensorType>();
113 
114  auto type = _sdf->Get<std::string>("type");
115  auto name = _sdf->Get<std::string>("name");
116 
117  if (nullptr == sensor)
118  {
119  gzerr << "Failed to create sensor [" << name
120  << "] of type[" << type << "]" << std::endl;
121  return nullptr;
122  }
123 
124  if (!sensor->Load(_sdf))
125  {
126  gzerr << "Failed to load sensor [" << name
127  << "] of type[" << type << "]" << std::endl;
128  return nullptr;
129  }
130 
131  if (!sensor->Init())
132  {
133  gzerr << "Failed to initialize sensor [" << name
134  << "] of type[" << type << "]" << std::endl;
135  return nullptr;
136  }
137 
138  return sensor;
139  }
140 
141  GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
143  private: std::unique_ptr<SensorFactoryPrivate> dataPtr;
144  GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
145  };
146  }
147  }
148 }
149 
150 #endif