Gazebo Sensors

API Reference

6.8.1
gz/sensors/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/common/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 ignition
34 {
35  namespace sensors
36  {
37  // Inline bracket to help doxygen filtering.
38  inline namespace IGNITION_SENSORS_VERSION_NAMESPACE {
39  // forward declaration
40  class SensorFactoryPrivate;
41 
45  class IGNITION_SENSORS_VISIBLE SensorPlugin
46  {
49  public: virtual Sensor IGN_DEPRECATED(6) * New() = 0;
50  };
51 
56  template<class SensorType>
58  {
59  // Documentation inherited
60  public: SensorType IGN_DEPRECATED(6) * New() override
61  {
62  return new SensorType();
63  };
64  };
65 
69  // After removing plugin functionality, the sensor factory class doesn't
70  // hold any internal state. Consider converting the functionality in this
71  // class to helper functions.
72  class IGNITION_SENSORS_VISIBLE SensorFactory
73  {
75  public: SensorFactory();
76 
78  public: ~SensorFactory();
79 
91  public: template<typename SensorType>
92  std::unique_ptr<SensorType> CreateSensor(const sdf::Sensor &_sdf)
93  {
94  auto sensor = std::make_unique<SensorType>();
95 
96  if (nullptr == sensor)
97  {
98  ignerr << "Failed to create sensor [" << _sdf.Name()
99  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
100  return nullptr;
101  }
102 
103  if (!sensor->Load(_sdf))
104  {
105  ignerr << "Failed to load sensor [" << _sdf.Name()
106  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
107  return nullptr;
108  }
109 
110  if (!sensor->Init())
111  {
112  ignerr << "Failed to initialize sensor [" << _sdf.Name()
113  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
114  return nullptr;
115  }
116 
117  return sensor;
118  }
119 
126  public: template<typename SensorType>
128  {
129  if (nullptr == _sdf)
130  {
131  ignerr << "Failed to create sensor, received null SDF "
132  << "pointer." << std::endl;
133  return nullptr;
134  }
135 
136  auto sensor = std::make_unique<SensorType>();
137 
138  auto type = _sdf->Get<std::string>("type");
139  auto name = _sdf->Get<std::string>("name");
140 
141  if (nullptr == sensor)
142  {
143  ignerr << "Failed to create sensor [" << name
144  << "] of type[" << type << "]" << std::endl;
145  return nullptr;
146  }
147 
148  if (!sensor->Load(_sdf))
149  {
150  ignerr << "Failed to load sensor [" << name
151  << "] of type[" << type << "]" << std::endl;
152  return nullptr;
153  }
154 
155  if (!sensor->Init())
156  {
157  ignerr << "Failed to initialize sensor [" << name
158  << "] of type[" << type << "]" << std::endl;
159  return nullptr;
160  }
161 
162  return sensor;
163  }
164 
178  public: std::unique_ptr<Sensor> IGN_DEPRECATED(6) CreateSensor(
179  sdf::ElementPtr _sdf);
180 
197  public: std::unique_ptr<Sensor> IGN_DEPRECATED(6) CreateSensor(
198  const sdf::Sensor &_sdf);
199 
203  public: void IGN_DEPRECATED(6) AddPluginPaths(const std::string &_path);
204 
205  IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
207  private: std::unique_ptr<SensorFactoryPrivate> dataPtr;
208  IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
209  };
210  }
211  }
212 }
213 
214 #endif
Definition: gz/sensors/AirPressureSensor.hh:31
STL class.
SensorType * New() override
Instantiate new sensor.
Definition: gz/sensors/SensorFactory.hh:60
std::unique_ptr< SensorType > CreateSensor(sdf::ElementPtr _sdf)
Create a sensor from an SDF element with a known sensor type.
Definition: gz/sensors/SensorFactory.hh:127
Base sensor plugin interface.
Definition: gz/sensors/SensorFactory.hh:45
virtual Sensor * New()=0
Instantiate new sensor.
T endl(T... args)
STL namespace.
void AddPluginPaths(const std::string &_path)
Add additional path to search for sensor plugins.
Templated class for instantiating sensors of the specified type.
Definition: gz/sensors/SensorFactory.hh:57
A factory class for creating sensors This class instantiates sensor objects based on the sensor type ...
Definition: gz/sensors/SensorFactory.hh:72
a base sensor class
Definition: gz/sensors/Sensor.hh:59
STL class.
std::unique_ptr< SensorType > CreateSensor(const sdf::Sensor &_sdf)
Create a sensor from a SDF DOM object with a known sensor type.
Definition: gz/sensors/SensorFactory.hh:92