Ignition Sensors

API Reference

6.0.1
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/SuppressWarning.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 
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 
85  public: template<typename SensorType>
86  std::unique_ptr<SensorType> CreateSensor(const sdf::Sensor &_sdf)
87  {
88  auto sensor = std::make_unique<SensorType>();
89 
90  if (nullptr == sensor)
91  {
92  ignerr << "Failed to create sensor [" << _sdf.Name()
93  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
94  return nullptr;
95  }
96 
97  if (!sensor->Load(_sdf))
98  {
99  ignerr << "Failed to load sensor [" << _sdf.Name()
100  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
101  return nullptr;
102  }
103 
104  if (!sensor->Init())
105  {
106  ignerr << "Failed to initialize sensor [" << _sdf.Name()
107  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
108  return nullptr;
109  }
110 
111  return sensor;
112  }
113 
120  public: template<typename SensorType>
122  {
123  if (nullptr == _sdf)
124  {
125  ignerr << "Failed to create sensor, received null SDF "
126  << "pointer." << std::endl;
127  return nullptr;
128  }
129 
130  auto sensor = std::make_unique<SensorType>();
131 
132  auto type = _sdf->Get<std::string>("type");
133  auto name = _sdf->Get<std::string>("name");
134 
135  if (nullptr == sensor)
136  {
137  ignerr << "Failed to create sensor [" << name
138  << "] of type[" << type << "]" << std::endl;
139  return nullptr;
140  }
141 
142  if (!sensor->Load(_sdf))
143  {
144  ignerr << "Failed to load sensor [" << name
145  << "] of type[" << type << "]" << std::endl;
146  return nullptr;
147  }
148 
149  if (!sensor->Init())
150  {
151  ignerr << "Failed to initialize sensor [" << name
152  << "] of type[" << type << "]" << std::endl;
153  return nullptr;
154  }
155 
156  return sensor;
157  }
158 
166  public: std::unique_ptr<Sensor> IGN_DEPRECATED(6) CreateSensor(
167  sdf::ElementPtr _sdf);
168 
185  public: std::unique_ptr<Sensor> IGN_DEPRECATED(6) CreateSensor(
186  const sdf::Sensor &_sdf);
187 
191  public: void IGN_DEPRECATED(6) AddPluginPaths(const std::string &_path);
192 
193  IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
195  private: std::unique_ptr<SensorFactoryPrivate> dataPtr;
196  IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
197  };
198  }
199  }
200 }
201 
202 #endif
T endl(T... args)
A factory class for creating sensors This class instantiates sensor objects based on the sensor type ...
Definition: SensorFactory.hh:72
a base sensor class
Definition: Sensor.hh:59
SensorType * New() override
Instantiate new sensor.
Definition: SensorFactory.hh:60
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: SensorFactory.hh:86
STL class.
Base sensor plugin interface.
Definition: SensorFactory.hh:45
Templated class for instantiating sensors of the specified type.
Definition: SensorFactory.hh:57
std::unique_ptr< SensorType > CreateSensor(sdf::ElementPtr _sdf)
Create a sensor from an SDF element with a known sensor type.
Definition: SensorFactory.hh:121
Definition: AirPressureSensor.hh:31