Ignition Sensors

API Reference

3.1.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 IGNITION_SENSORS_SENSORFACTORY_HH_
18 #define IGNITION_SENSORS_SENSORFACTORY_HH_
19 
20 #include <memory>
21 #include <string>
22 #include <type_traits>
23 #include <sdf/sdf.hh>
24 
25 #include <ignition/common/Console.hh>
26 #include <ignition/common/PluginMacros.hh>
27 
28 #include <ignition/sensors/config.hh>
29 #include <ignition/sensors/Export.hh>
30 
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 
43  class IGNITION_SENSORS_VISIBLE SensorPlugin
44  {
46  public: IGN_COMMON_SPECIALIZE_INTERFACE(ignition::sensors::SensorPlugin)
47 
48 
49  public: virtual Sensor *New() = 0;
51  };
52 
55  template<class SensorType>
57  {
58  // Documentation inherited
59  public: SensorType *New() override
60  {
61  return new SensorType();
62  };
63  };
64 
69  class IGNITION_SENSORS_VISIBLE SensorFactory
70  {
72  public: SensorFactory();
73 
75  public: ~SensorFactory();
76 
88  public: template<typename T>
89  std::unique_ptr<T> CreateSensor(const sdf::Sensor &_sdf)
90  {
91  auto sensor = SensorFactory::CreateSensor(_sdf);
92 
93  if (sensor)
94  {
95  std::unique_ptr<T> result(
96  dynamic_cast<T *>(sensor.release()));
97 
98  if (!result)
99  ignerr << "SDF sensor type does not match template type\n";
100 
101  return result;
102  }
103 
104  ignerr << "Failed to create sensor of type["
105  << _sdf.TypeStr() << "]\n";
106  return nullptr;
107  }
108 
120  public: template<typename T>
121  std::unique_ptr<T> CreateSensor(sdf::ElementPtr _sdf)
122  {
123  auto sensor = SensorFactory::CreateSensor(_sdf);
124 
125  if (sensor)
126  {
127  std::unique_ptr<T> result(
128  dynamic_cast<T *>(sensor.release()));
129 
130  if (!result)
131  ignerr << "SDF sensor type does not match template type\n";
132 
133  return result;
134  }
135 
136  ignerr << "Failed to create sensor of type["
137  << _sdf->Get<std::string>("type") << "]\n";
138  return nullptr;
139  }
140 
152  public: std::unique_ptr<Sensor> CreateSensor(sdf::ElementPtr _sdf);
153 
167  public: std::unique_ptr<Sensor> CreateSensor(const sdf::Sensor &_sdf);
168 
171  public: void AddPluginPaths(const std::string &_path);
172 
176  private: std::shared_ptr<SensorPlugin> LoadSensorPlugin(
177  const std::string &_filename);
178 
180  private: std::unique_ptr<SensorFactoryPrivate> dataPtr;
181  };
182 
184  #define IGN_SENSORS_REGISTER_SENSOR(classname) \
185  IGN_COMMON_REGISTER_SINGLE_PLUGIN(\
186  ignition::sensors::SensorTypePlugin<classname>, \
187  ignition::sensors::SensorPlugin)
188  }
189  }
190 }
191 
192 #endif
std::unique_ptr< T > CreateSensor(sdf::ElementPtr _sdf)
Create a sensor from SDF with a known sensor type.
Definition: SensorFactory.hh:121
A factory class for creating sensors This class wll load a sensor plugin based on the given sensor ty...
Definition: SensorFactory.hh:69
a base sensor class
Definition: Sensor.hh:50
SensorType * New() override
Definition: SensorFactory.hh:59
STL class.
STL class.
Base sensor plugin interface.
Definition: SensorFactory.hh:43
Templated class for instantiating sensors of the specified type.
Definition: SensorFactory.hh:56
Definition: AirPressureSensor.hh:31
std::unique_ptr< T > CreateSensor(const sdf::Sensor &_sdf)
Create a sensor from a SDF DOM object with a known sensor type.
Definition: SensorFactory.hh:89