Skip to content

Commit

Permalink
Refactor laser includes and singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
xela-95 committed Mar 11, 2024
1 parent 6b9d15d commit 43e1dcd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 40 deletions.
12 changes: 7 additions & 5 deletions plugins/laser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
project(laser)

add_subdirectory(singleton-laser)
add_subdirectory(singleton)

add_library(gz-sim-yarp-laser-system SHARED Laser.cc LaserDriver.cpp)

target_include_directories(gz-sim-yarp-laser-system
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")

target_compile_features(gz-sim-yarp-laser-system PRIVATE cxx_std_17)

target_link_libraries(gz-sim-yarp-laser-system
PRIVATE gz-plugin${GZ_PLUGIN_VER}::gz-plugin${GZ_PLUGIN_VER}
PRIVATE gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
PRIVATE
${YARP_LIBRARIES}
YARP::YARP_dev
YARP::YARP_os
YARP::YARP_init
gz-sim-yarp-handler
gz-sim-yarp-handler-laser)
gz-sim-yarp-laser-singleton)

# Add install target
install(TARGETS gz-sim-yarp-laser-system)
6 changes: 3 additions & 3 deletions plugins/laser/Laser.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "LaserDriver.cpp"
#include <LaserDriver.cpp>

#include <gz/msgs.hh>
#include <gz/plugin/Register.hh>
Expand Down Expand Up @@ -47,7 +47,7 @@ class Laser : public System,

if (m_laserDriver.isValid())
m_laserDriver.close();
HandlerLaser::getHandler()->removeSensor(sensorScopedName);
LaserDataSingleton::getHandler()->removeSensor(sensorScopedName);
yarp::os::Network::fini();
}

Expand Down Expand Up @@ -112,7 +112,7 @@ class Laser : public System,
}

// Insert the pointer in the singleton handler for retriving it in the yarp driver
HandlerLaser::getHandler()->setSensor(&(this->laserData));
LaserDataSingleton::getHandler()->setSensor(&(this->laserData));

driver_properties.put("device", "gazebo_laser");
driver_properties.put("sensor_name", sensorName);
Expand Down
9 changes: 5 additions & 4 deletions plugins/laser/LaserDriver.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "../../libraries/singleton-devices/Handler.hh"
#include "singleton-laser/Handler.hh"
#include <iostream>
#include <Handler.hh>
#include <LaserDataSingleton.hh>

#include <mutex>

#include <yarp/dev/DeviceDriver.h>
#include <yarp/dev/IRangefinder2D.h>
#include <yarp/dev/LaserMeasurementData.h>
Expand Down Expand Up @@ -48,7 +49,7 @@ class yarp::dev::gzyarp::LaserDriver : public yarp::dev::Lidar2DDeviceBase,
m_sensorName = config.find("sensor_name").asString().substr(pos + separator.size() - 1);
}
m_frameName = m_sensorName;
m_sensorData = ::gzyarp::HandlerLaser::getHandler()->getSensor(sensorScopedName);
m_sensorData = ::gzyarp::LaserDataSingleton::getHandler()->getSensor(sensorScopedName);

if (!m_sensorData)
{
Expand Down
14 changes: 0 additions & 14 deletions plugins/laser/singleton-laser/CMakeLists.txt

This file was deleted.

18 changes: 18 additions & 0 deletions plugins/laser/singleton/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

add_library(gz-sim-yarp-laser-singleton SHARED LaserDataSingleton.hh LaserDataSingleton.cc)

target_include_directories(gz-sim-yarp-laser-singleton
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")

target_compile_features(gz-sim-yarp-laser-singleton PRIVATE cxx_std_17)

target_link_libraries(gz-sim-yarp-laser-singleton
PRIVATE gz-plugin${GZ_PLUGIN_VER}::gz-plugin${GZ_PLUGIN_VER}
PRIVATE gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
PRIVATE
YARP::YARP_dev
YARP::YARP_os
YARP::YARP_init)

# Add install target
install(TARGETS gz-sim-yarp-laser-singleton)
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#include "Handler.hh"
#include <LaserDataSingleton.hh>

#include <yarp/os/Log.h>
#include <yarp/os/LogStream.h>

namespace gzyarp
{

HandlerLaser* HandlerLaser::getHandler()
LaserDataSingleton* LaserDataSingleton::getHandler()
{
std::lock_guard<std::mutex> lock(mutex());
if (!s_handle)
{
s_handle = new HandlerLaser();
s_handle = new LaserDataSingleton();
if (!s_handle)
yError() << "Error while calling gzyarp::HandlerLaser constructor";
}

return s_handle;
}

bool HandlerLaser::setSensor(LaserData* _sensorDataPtr)
bool LaserDataSingleton::setSensor(LaserData* _sensorDataPtr)
{
bool ret = false;
std::string sensorScopedName = _sensorDataPtr->sensorScopedName;
Expand All @@ -45,7 +46,7 @@ bool HandlerLaser::setSensor(LaserData* _sensorDataPtr)
return ret;
}

LaserData* HandlerLaser::getSensor(const std::string& sensorScopedName) const
LaserData* LaserDataSingleton::getSensor(const std::string& sensorScopedName) const
{
LaserData* tmp;

Expand All @@ -61,7 +62,7 @@ LaserData* HandlerLaser::getSensor(const std::string& sensorScopedName) const
return tmp;
}

void HandlerLaser::removeSensor(const std::string& sensorName)
void LaserDataSingleton::removeSensor(const std::string& sensorName)
{
SensorsMap::iterator sensor = m_sensorsMap.find(sensorName);
if (sensor != m_sensorsMap.end())
Expand All @@ -73,15 +74,15 @@ void HandlerLaser::removeSensor(const std::string& sensorName)
}
}

HandlerLaser::HandlerLaser()
LaserDataSingleton::LaserDataSingleton()
: m_sensorsMap()
{
m_sensorsMap.clear();
}

HandlerLaser* HandlerLaser::s_handle = NULL;
LaserDataSingleton* LaserDataSingleton::s_handle = NULL;

std::mutex& HandlerLaser::mutex()
std::mutex& LaserDataSingleton::mutex()
{
static std::mutex s_mutex;
return s_mutex;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <gz/common/Event.hh>
#include <mutex>
#include <string>
#include <vector>

#include <gz/common/Event.hh>

#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/PolyDriverList.h>

Expand All @@ -16,10 +18,10 @@ struct LaserData
namespace gzyarp
{

class HandlerLaser
class LaserDataSingleton
{
public:
static HandlerLaser* getHandler();
static LaserDataSingleton* getHandler();

bool setSensor(LaserData* _sensorDataPtr);

Expand All @@ -28,8 +30,8 @@ public:
void removeSensor(const std::string& sensorName);

private:
HandlerLaser();
static HandlerLaser* s_handle;
LaserDataSingleton();
static LaserDataSingleton* s_handle;
static std::mutex& mutex();
typedef std::map<std::string, LaserData*> SensorsMap;
SensorsMap m_sensorsMap; // map of known sensors
Expand Down

0 comments on commit 43e1dcd

Please sign in to comment.