Skip to content

Commit

Permalink
Merge pull request #1685 from Nicogene/cleanupSingleton
Browse files Browse the repository at this point in the history
Cleanup singleton
  • Loading branch information
Nicogene authored May 16, 2018
2 parents d911c21 + 7723709 commit 4e3ad5f
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 65 deletions.
2 changes: 0 additions & 2 deletions src/libYARP_OS/include/yarp/os/Carriers.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ class YARP_OS_API yarp::os::Carriers

static Carriers& getInstance();

static void removeInstance();

static Bottle listCarriers();

private:
Expand Down
6 changes: 2 additions & 4 deletions src/libYARP_OS/include/yarp/os/impl/LogForwarder.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class YARP_OS_API LogForwarder
{
public:
static LogForwarder* getInstance();
static void clearInstance();
void forward (const std::string& message);
protected:
LogForwarder();
Expand All @@ -34,9 +33,8 @@ class YARP_OS_API LogForwarder
char logPortName[MAX_STRING_SIZE];
yarp::os::BufferedPort<yarp::os::Bottle>* outputPort;
private:
LogForwarder(LogForwarder const&){};
LogForwarder& operator=(LogForwarder const&){return *this;}; //@@@checkme
static LogForwarder* instance;
LogForwarder(LogForwarder const&){}
LogForwarder& operator=(LogForwarder const&){return *this;}
};

} // namespace os
Expand Down
20 changes: 2 additions & 18 deletions src/libYARP_OS/src/Carriers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ using namespace yarp::os;
class Carriers::Private : public YarpPluginSelector
{
public:
static Carriers* yarp_carriers_instance;
static yarp::os::Mutex mutex;

std::vector<Carrier*> delegates;
Expand All @@ -52,7 +51,6 @@ class Carriers::Private : public YarpPluginSelector
virtual bool select(Searchable& options) override;
};

Carriers* Carriers::Private::yarp_carriers_instance = nullptr;
yarp::os::Mutex Carriers::Private::mutex {};


Expand Down Expand Up @@ -326,22 +324,8 @@ bool Carrier::reply(ConnectionState& proto, SizedWriter& writer)

Carriers& Carriers::getInstance()
{
yarp::os::LockGuard guard(Private::mutex);
if (Private::yarp_carriers_instance == nullptr) {
Private::yarp_carriers_instance = new Carriers();
yAssert(Private::yarp_carriers_instance != nullptr);
}
return *Private::yarp_carriers_instance;
}


void Carriers::removeInstance()
{
yarp::os::LockGuard guard(Private::mutex);
if (Private::yarp_carriers_instance != nullptr) {
delete Private::yarp_carriers_instance;
Private::yarp_carriers_instance = nullptr;
}
static Carriers instance;
return instance;
}


Expand Down
25 changes: 5 additions & 20 deletions src/libYARP_OS/src/LogForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,13 @@
#include <yarp/os/Log.h>
#include <yarp/os/SystemInfo.h>

yarp::os::LogForwarder* yarp::os::LogForwarder::instance = nullptr;
yarp::os::Semaphore *yarp::os::LogForwarder::sem = nullptr;

yarp::os::LogForwarder* yarp::os::LogForwarder::getInstance()
{
if (!instance)
{
instance = new LogForwarder;
}
return instance;
};

void yarp::os::LogForwarder::clearInstance()
{
if (instance)
{
delete instance;
};
};
static LogForwarder instance;
return &instance;
}

void yarp::os::LogForwarder::forward (const std::string& message)
{
Expand Down Expand Up @@ -74,8 +62,7 @@ yarp::os::LogForwarder::LogForwarder()
{
printf("LogForwarder error while connecting port %s\n", logPortName);
}
//yarp::os::Network::connect(logPortName, "/test");
};
}

yarp::os::LogForwarder::~LogForwarder()
{
Expand All @@ -89,13 +76,11 @@ yarp::os::LogForwarder::~LogForwarder()
b.addString("[INFO] Execution terminated\n");
outputPort->write(true);
outputPort->waitForWrite();
//outputPort->interrupt();
outputPort->close();
delete outputPort;
outputPort=nullptr;
}
sem->post();
delete sem;
sem = nullptr;
// yarp::os::NetworkBase::finiMinimum();
};
}
1 change: 0 additions & 1 deletion src/libYARP_OS/src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,6 @@ void NetworkBase::initMinimum(yarp::os::yarpClockType clockType, yarp::os::Clock
void NetworkBase::finiMinimum() {
if (__yarp_is_initialized==1) {
Time::useSystemClock();
Carriers::removeInstance();
removeNameSpace();
Bottle::fini();
BottleImpl::fini();
Expand Down
2 changes: 1 addition & 1 deletion src/libYARP_OS/src/RFModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class RFModuleThreadedHandler : public Thread {
RFModule& owner;

public:
RFModuleThreadedHandler(RFModule& owner) : owner(owner) {};
RFModuleThreadedHandler(RFModule& owner) : owner(owner) {}

void run() override { owner.runModule(); }
};
Expand Down
2 changes: 0 additions & 2 deletions src/libYARP_init/src/CustomInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ extern "C" int __yarp_is_initialized;

extern "C" void yarpCustomFini()
{
yarp::os::LogForwarder::clearInstance();
}


yarp::os::Network::Network() {
Network::init();
}
Expand Down
5 changes: 2 additions & 3 deletions src/libYARP_manager/include/yarp/manager/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ class ErrorLogger
int warningCount(void);

private:
ErrorLogger(){};
ErrorLogger(ErrorLogger const&){};
static ErrorLogger* pInstance;
ErrorLogger(){}
ErrorLogger(ErrorLogger const&){}
std::vector<std::string> errors;
std::vector<std::string> warnings;
};
Expand Down
8 changes: 2 additions & 6 deletions src/libYARP_manager/src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ std::ostream& operator << (std::ostream &os , StrStream& sstr)
* Singleton class ErrorLogger
*/

// Global static pointer used to ensure a single instance of the class.
ErrorLogger* ErrorLogger::pInstance = nullptr;

ErrorLogger* ErrorLogger::Instance()
{
if (!pInstance)
pInstance = new ErrorLogger;
return pInstance;
static ErrorLogger instance;
return &instance;
}

void ErrorLogger::addWarning(const char* szWarning) {
Expand Down
9 changes: 2 additions & 7 deletions src/yarpscope/plugin/plotmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

#include "plotmanager.h"

PlotManager *PlotManager::self = nullptr;

PlotManager::PlotManager(QObject *parent) :
QObject(parent)
{
self = this;
timer.setTimerType(Qt::PreciseTimer);
connect(&timer,SIGNAL(timeout()),this,SLOT(onTimeout()),Qt::DirectConnection);
}
Expand All @@ -35,10 +32,8 @@ PlotManager::~PlotManager()
/*! \brief Returns an instance of the class (Singleton). */
PlotManager * PlotManager::instance()
{
if(self == nullptr){
return new PlotManager();
}
return self;
static PlotManager instance;
return &instance;
}

/*! \brief Returns a list of all plotters. */
Expand Down
1 change: 0 additions & 1 deletion src/yarpscope/plugin/plotmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class PlotManager : public QObject

private:
QList <QObject*> plotterList;
static PlotManager *self;
QTimer timer;

signals:
Expand Down

0 comments on commit 4e3ad5f

Please sign in to comment.