Gazebo Sensors

API Reference

7.3.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/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  class GZ_SENSORS_VISIBLE SensorPlugin
46  {
49  public: virtual Sensor GZ_DEPRECATED(6) * New() = 0;
50  };
51 
56  template<class SensorType>
58  {
59  // Documentation inherited
60  public: SensorType GZ_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 GZ_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  gzerr << "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  gzerr << "Failed to load sensor [" << _sdf.Name()
106  << "] of type[" << _sdf.TypeStr() << "]" << std::endl;
107  return nullptr;
108  }
109 
110  if (!sensor->Init())
111  {
112  gzerr << "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  gzerr << "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  gzerr << "Failed to create sensor [" << name
144  << "] of type[" << type << "]" << std::endl;
145  return nullptr;
146  }
147 
148  if (!sensor->Load(_sdf))
149  {
150  gzerr << "Failed to load sensor [" << name
151  << "] of type[" << type << "]" << std::endl;
152  return nullptr;
153  }
154 
155  if (!sensor->Init())
156  {
157  gzerr << "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> GZ_DEPRECATED(6) CreateSensor(
179  sdf::ElementPtr _sdf);
180 
197  public: std::unique_ptr<Sensor> GZ_DEPRECATED(6) CreateSensor(
198  const sdf::Sensor &_sdf);
199 
203  public: void GZ_DEPRECATED(6) AddPluginPaths(const std::string &_path);
204 
205  GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
207  private: std::unique_ptr<SensorFactoryPrivate> dataPtr;
208  GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
209  };
210  }
211  }
212 }
213 
214 #endif