Gazebo Sensors

API Reference

3.6.0
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/PluginMacros.hh>
27 #include <gz/common/SuppressWarning.hh>
28 
29 #include <gz/sensors/config.hh>
30 #include <gz/sensors/Export.hh>
31 
32 #include "gz/sensors/Sensor.hh"
33 
34 namespace ignition
35 {
36  namespace sensors
37  {
38  // Inline bracket to help doxygen filtering.
39  inline namespace IGNITION_SENSORS_VERSION_NAMESPACE {
40  // forward declaration
41  class SensorFactoryPrivate;
42 
44  class IGNITION_SENSORS_VISIBLE SensorPlugin
45  {
47  public: IGN_COMMON_SPECIALIZE_INTERFACE(gz::sensors::SensorPlugin)
48 
49 
50  public: virtual Sensor *New() = 0;
52  };
53 
56  template<class SensorType>
58  {
59  // Documentation inherited
60  public: SensorType *New() override
61  {
62  return new SensorType();
63  };
64  };
65 
70  class IGNITION_SENSORS_VISIBLE SensorFactory
71  {
73  public: SensorFactory();
74 
76  public: ~SensorFactory();
77 
89  public: template<typename T>
90  std::unique_ptr<T> CreateSensor(const sdf::Sensor &_sdf)
91  {
92  auto sensor = SensorFactory::CreateSensor(_sdf);
93 
94  if (sensor)
95  {
96  std::unique_ptr<T> result(
97  dynamic_cast<T *>(sensor.release()));
98 
99  if (!result)
100  ignerr << "SDF sensor type does not match template type\n";
101 
102  return result;
103  }
104 
105  ignerr << "Failed to create sensor of type["
106  << _sdf.TypeStr() << "]\n";
107  return nullptr;
108  }
109 
121  public: template<typename T>
122  std::unique_ptr<T> CreateSensor(sdf::ElementPtr _sdf)
123  {
124  auto sensor = SensorFactory::CreateSensor(_sdf);
125 
126  if (sensor)
127  {
128  std::unique_ptr<T> result(
129  dynamic_cast<T *>(sensor.release()));
130 
131  if (!result)
132  ignerr << "SDF sensor type does not match template type\n";
133 
134  return result;
135  }
136 
137  ignerr << "Failed to create sensor of type["
138  << _sdf->Get<std::string>("type") << "]\n";
139  return nullptr;
140  }
141 
153  public: std::unique_ptr<Sensor> CreateSensor(sdf::ElementPtr _sdf);
154 
168  public: std::unique_ptr<Sensor> CreateSensor(const sdf::Sensor &_sdf);
169 
172  public: void AddPluginPaths(const std::string &_path);
173 
177  private: std::shared_ptr<SensorPlugin> LoadSensorPlugin(
178  const std::string &_filename);
179 
180  IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
182  private: std::unique_ptr<SensorFactoryPrivate> dataPtr;
183  IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
184  };
185 
187  #define IGN_SENSORS_REGISTER_SENSOR(classname) \
188  IGN_COMMON_REGISTER_SINGLE_PLUGIN(\
189  gz::sensors::SensorTypePlugin<classname>, \
190  gz::sensors::SensorPlugin)
191  }
192  }
193 }
194 
195 #endif
Definition: gz/sensors/AirPressureSensor.hh:32
STL class.
STL class.
SensorType * New() override
Definition: gz/sensors/SensorFactory.hh:60
Base sensor plugin interface.
Definition: gz/sensors/SensorFactory.hh:44
std::unique_ptr< T > CreateSensor(sdf::ElementPtr _sdf)
Create a sensor from SDF with a known sensor type.
Definition: gz/sensors/SensorFactory.hh:122
void AddPluginPaths(const std::string &_path)
Add additional path to search for sensor plugins.
std::unique_ptr< T > CreateSensor(const sdf::Sensor &_sdf)
Create a sensor from a SDF DOM object with a known sensor type.
Definition: gz/sensors/SensorFactory.hh:90
Templated class for instantiating sensors of the specified type.
Definition: gz/sensors/SensorFactory.hh:57
A factory class for creating sensors This class wll load a sensor plugin based on the given sensor ty...
Definition: gz/sensors/SensorFactory.hh:70
a base sensor class
Definition: gz/sensors/Sensor.hh:60
STL class.