mirror of
https://github.com/introlab/rtabmap.git
synced 2026-09-02 01:20:25 +08:00
Merged pcl_integration branch to trunk
git-svn-id: http://rtabmap.googlecode.com/svn/trunk/rtabmap@1014 f169173b-cf89-36c8-b27e-44dbe73f0c83
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
|
||||
|
||||
#include "rtabmap/utilite/UEventsSender.h"
|
||||
class UEvent;
|
||||
|
||||
/**
|
||||
@@ -124,8 +125,11 @@ class UEvent;
|
||||
* @see UThreadNode
|
||||
*
|
||||
*/
|
||||
class UTILITE_EXP UEventsHandler{
|
||||
class UTILITE_EXP UEventsHandler : public UEventsSender {
|
||||
public:
|
||||
|
||||
void registerToEventsManager();
|
||||
void unregisterFromEventsManager();
|
||||
|
||||
|
||||
protected:
|
||||
@@ -172,11 +176,6 @@ protected:
|
||||
*/
|
||||
virtual ~UEventsHandler();
|
||||
|
||||
/**
|
||||
* For convenience to post an event. This is the same than calling UEventsManager::post().
|
||||
*/
|
||||
void post(UEvent * event, bool async = true);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "rtabmap/utilite/UDestroyer.h"
|
||||
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
// TODO Not implemented... for multithreading event handling
|
||||
class UEventDispatcher : public UThread
|
||||
@@ -106,8 +107,21 @@ public:
|
||||
* @param event the event to be posted.
|
||||
* @param async if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous).
|
||||
*/
|
||||
static void post(UEvent * event, bool async = true);
|
||||
|
||||
static void post(UEvent * event, bool async = true, const UEventsSender * sender = 0);
|
||||
|
||||
static void createPipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName);
|
||||
|
||||
static void removePipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName);
|
||||
|
||||
static void removeAllPipes(const UEventsSender * sender);
|
||||
static void removeNullPipes(const UEventsSender * sender);
|
||||
|
||||
protected:
|
||||
|
||||
/*
|
||||
@@ -161,7 +175,7 @@ private:
|
||||
/*
|
||||
* This method dispatches an event to all handlers.
|
||||
*/
|
||||
virtual void dispatchEvent(UEvent * event);
|
||||
virtual void dispatchEvent(UEvent * event, const UEventsSender * sender);
|
||||
|
||||
/*
|
||||
* This method is used to add an events
|
||||
@@ -191,17 +205,49 @@ private:
|
||||
* @param event the event to be posted.
|
||||
* @param async if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous).
|
||||
*/
|
||||
void _postEvent(UEvent * event, bool async = true);
|
||||
|
||||
void _postEvent(UEvent * event, bool async = true, const UEventsSender * sender = 0);
|
||||
|
||||
std::list<UEventsHandler*> getPipes(
|
||||
const UEventsSender * sender,
|
||||
const std::string & eventName);
|
||||
|
||||
void _createPipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName);
|
||||
|
||||
void _removePipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName);
|
||||
|
||||
void _removeAllPipes(const UEventsSender * sender);
|
||||
void _removeNullPipes(const UEventsSender * sender);
|
||||
|
||||
private:
|
||||
|
||||
class Pipe
|
||||
{
|
||||
public:
|
||||
Pipe(const UEventsSender * sender, const UEventsHandler * receiver, const std::string & eventName) :
|
||||
sender_(sender),
|
||||
receiver_(receiver),
|
||||
eventName_(eventName)
|
||||
{}
|
||||
const UEventsSender * sender_;
|
||||
const UEventsHandler * receiver_;
|
||||
const std::string eventName_;
|
||||
};
|
||||
|
||||
static UEventsManager* instance_; /* The EventsManager instance pointer. */
|
||||
static UDestroyer<UEventsManager> destroyer_; /* The EventsManager's destroyer. */
|
||||
std::list<UEvent*> events_; /* The events list. */
|
||||
std::list<std::pair<UEvent*, const UEventsSender * > > events_; /* The events list. */
|
||||
std::list<UEventsHandler*> handlers_; /* The handlers list. */
|
||||
UMutex eventsMutex_; /* The mutex of the events list, */
|
||||
UMutex handlersMutex_; /* The mutex of the handlers list. */
|
||||
USemaphore postEventSem_; /* Semaphore used to signal when an events is posted. */
|
||||
std::list<Pipe> pipes_;
|
||||
UMutex pipesMutex_;
|
||||
};
|
||||
|
||||
#endif // UEVENTSMANAGER_H
|
||||
|
||||
31
utilite/include/rtabmap/utilite/UEventsSender.h
Normal file
31
utilite/include/rtabmap/utilite/UEventsSender.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* UEventsSender.h
|
||||
*
|
||||
* Created on: 2013-10-14
|
||||
* Author: Mathieu
|
||||
*/
|
||||
|
||||
#ifndef UEVENTSSENDER_H_
|
||||
#define UEVENTSSENDER_H_
|
||||
|
||||
#include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
|
||||
|
||||
class UEvent;
|
||||
|
||||
class UTILITE_EXP UEventsSender
|
||||
{
|
||||
public:
|
||||
UEventsSender(){}
|
||||
virtual ~UEventsSender();
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* For convenience to post an event. This is the same than calling UEventsManager::post()
|
||||
* with the sender reference.
|
||||
*/
|
||||
void post(UEvent * event, bool async = true) const;
|
||||
};
|
||||
|
||||
|
||||
#endif /* UEVENTSSENDER_H_ */
|
||||
@@ -70,8 +70,17 @@ public:
|
||||
*/
|
||||
static std::string getName(const std::string & filePath);
|
||||
|
||||
/**
|
||||
* Get the file extension.
|
||||
* @return the file extension
|
||||
*/
|
||||
static std::string getExtension(const std::string &filePath);
|
||||
|
||||
/**
|
||||
* Copy a file.
|
||||
* @param from the file path
|
||||
* @param to destination file path
|
||||
*/
|
||||
static void copy(const std::string & from, const std::string & to);
|
||||
|
||||
public:
|
||||
@@ -130,6 +139,10 @@ public:
|
||||
*/
|
||||
std::string getExtension() {return getExtension(path_);}
|
||||
|
||||
/**
|
||||
* Copy a file.
|
||||
* @param to destination file path
|
||||
*/
|
||||
void copy(const std::string & to) {copy(path_, to);}
|
||||
|
||||
private:
|
||||
|
||||
@@ -30,6 +30,39 @@
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#if _MSC_VER
|
||||
#undef min
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Return true if the number is NAN.
|
||||
*/
|
||||
template<class T>
|
||||
inline bool uIsNan(const T & value)
|
||||
{
|
||||
#ifdef __APPLE__
|
||||
return std::isnan(value);
|
||||
#elif _MSC_VER
|
||||
return _isnan(value);
|
||||
#else
|
||||
return isnan(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the number is finite.
|
||||
*/
|
||||
template<class T>
|
||||
inline bool uIsFinite(const T & value)
|
||||
{
|
||||
#if _MSC_VER
|
||||
return _finite(value);
|
||||
#else
|
||||
return std::isfinite(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum of a vector.
|
||||
* @param v the array
|
||||
@@ -49,7 +82,7 @@ inline T uMax(const T * v, unsigned int size, unsigned int & index)
|
||||
max = v[0];
|
||||
for(unsigned int i=1; i<size; ++i)
|
||||
{
|
||||
if(max < v[i])
|
||||
if(uIsNan(max) || (max < v[i] && !uIsNan(v[i])))
|
||||
{
|
||||
max = v[i];
|
||||
index = i;
|
||||
@@ -114,7 +147,7 @@ inline T uMin(const T * v, unsigned int size, unsigned int & index)
|
||||
min = v[0];
|
||||
for(unsigned int i=1; i<size; ++i)
|
||||
{
|
||||
if(min > v[i])
|
||||
if(uIsNan(min) || (min > v[i] && !uIsNan(v[i])))
|
||||
{
|
||||
min = v[i];
|
||||
index = i;
|
||||
@@ -182,15 +215,16 @@ inline void uMinMax(const T * v, unsigned int size, T & min, T & max, unsigned i
|
||||
}
|
||||
min = v[0];
|
||||
max = v[0];
|
||||
|
||||
for(unsigned int i=1; i<size; ++i)
|
||||
{
|
||||
if(min > v[i])
|
||||
if(uIsNan(min) || (min > v[i] && !uIsNan(v[i])))
|
||||
{
|
||||
min = v[i];
|
||||
indexMin = i;
|
||||
}
|
||||
|
||||
if(max < v[i])
|
||||
if(uIsNan(max) || (max < v[i] && !uIsNan(v[i])))
|
||||
{
|
||||
max = v[i];
|
||||
indexMax = i;
|
||||
@@ -627,7 +661,7 @@ inline std::vector<T> uXMatch(const T * vA, const T * vB, unsigned int sizeA, un
|
||||
}
|
||||
else if(method == UXCorrBiased || method == UXCovBiased)
|
||||
{
|
||||
den = std::max(sizeA, sizeB);
|
||||
den = (T)std::max(sizeA, sizeB);
|
||||
}
|
||||
|
||||
if(sizeA == sizeB)
|
||||
@@ -755,7 +789,7 @@ inline T uXMatch(const T * vA, const T * vB, unsigned int sizeA, unsigned int si
|
||||
}
|
||||
else if(method == UXCorrBiased || method == UXCovBiased)
|
||||
{
|
||||
den = std::max(sizeA, sizeB);
|
||||
den = (T)std::max(sizeA, sizeB);
|
||||
}
|
||||
else if(method == UXCorrUnbiased || method == UXCovUnbiased)
|
||||
{
|
||||
@@ -821,7 +855,7 @@ inline std::vector<float> uHamming(unsigned int L)
|
||||
float pi = 3.14159265f;
|
||||
for(unsigned int n=0; n<N; ++n)
|
||||
{
|
||||
w[n] = 0.54-0.46*std::cos(2*pi*n/N);
|
||||
w[n] = 0.54f-0.46f*std::cos(2.0f*pi*float(n)/float(N));
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
* \section logger ULogger
|
||||
* The ULogger can be used anywhere in the application to log messages (formated like a printf). The
|
||||
* logger can be set (ULogger::setType()) to print in a file or in the console (with colors depending on the severity). Convenient
|
||||
* macros are given, working like a printf(), as UDEBUG(), UINFO(), UWARN(), UERROR(), UFATAL(). Small example:
|
||||
* macros are given, working like a printf(), as UDEBUG(), UINFO(), UWARN(), UERROR(), UFATAL(), UASSERT(). Small example:
|
||||
* @code
|
||||
* ...
|
||||
* UINFO("A simple message with number %d", 42);
|
||||
@@ -77,17 +77,17 @@
|
||||
* // The UEventsHandler::handleEvent() of "handler" will then be called by the UEventsManager's events dispatching thread.
|
||||
* ...
|
||||
* @endcode
|
||||
* Look at the <b>full example</b> in page of UEventsHandler on how communication works with threads (UThreadNode).
|
||||
* Look at the <b>full example</b> in page of UEventsHandler on how communication works with threads (UThread).
|
||||
*
|
||||
* \section threadnode UThreadNode, UMutex, USemaphore
|
||||
* The multi-threading framework use a UThreadNode as an abstract class to implement a
|
||||
* thread in object-style. Reimplement UThreadNode::mainLoop() then call UThreadNode::start() to
|
||||
* start the main loop of the thread. Threads can be joined by calling UThreadNode::join() and
|
||||
* killed by calling UThreadNode::kill(). Classes UMutex and USemaphore provide blocking mechanisms to
|
||||
* \section thread UThread, UMutex, USemaphore
|
||||
* The multi-threading framework use a UThread as an abstract class to implement a
|
||||
* thread in object-style. Reimplement UThread::mainLoop() then call UThread::start() to
|
||||
* start the main loop of the thread. Threads can be joined by calling UThread::join() and
|
||||
* killed by calling UThread::kill(). Classes UMutex and USemaphore provide blocking mechanisms to
|
||||
* protect data between threads.
|
||||
* @code
|
||||
* ...
|
||||
* MyThread t; // MyThread is an implementation of UThreadNode
|
||||
* MyThread t; // MyThread is an implementation of UThread
|
||||
* t.start();
|
||||
* t.join(); // Wait the thread to finish
|
||||
* ...
|
||||
@@ -168,6 +168,9 @@
|
||||
* useful widgets is built. Use class UPlot to create a plot like MATLAB, and incrementally add
|
||||
* new values like a scope. USpectrogram is used to
|
||||
* show audio frequency frames.
|
||||
* - UPlot,
|
||||
* - USpectrogram,
|
||||
* - UImageView.
|
||||
* @image html UPlot.gif
|
||||
* @image html USpectrogram.png
|
||||
*
|
||||
@@ -175,13 +178,26 @@
|
||||
* If FMOD is found on the system, the UtiLite audio
|
||||
* library is built (libutilite_audio.so, libutilite_audio.dll). It is a wrapper
|
||||
* of FMOD methods with a convenient interface to extract audio frames.
|
||||
* - UAudioRecorder,
|
||||
* - UAudioRecorderFile,
|
||||
* - UAudioRecorderMic
|
||||
* - UAudioCapture,
|
||||
* - UAudioCaptureFile,
|
||||
* - UAudioCaptureMic,
|
||||
* - UAudioCaptureFFT,
|
||||
* - UAudioPlayer,
|
||||
* - UAudioPlayerTone,
|
||||
* - UWav,
|
||||
* - UMp3Encoder (only if Lame is also found on the system).
|
||||
*
|
||||
* \section cvLib OpenCV stuff (libutilite_cv.so : OPTIONAL)
|
||||
* If OpenCV is found on the system, the UtiLite cv
|
||||
* library is built (libutilite_cv.so, libutilite_cv.dll). It provides
|
||||
* image capture classes used to read from a webcam, a video file
|
||||
* or a directory of images. If UtiLite is also built with Qt, a
|
||||
* convenient function uCvMat2QImage() can be used to convert a cv::Mat
|
||||
* image to a QImage.
|
||||
* - UVideoCapture,
|
||||
* - UImageFolderCapture,
|
||||
* - UColorTable,
|
||||
* - uCvMat2QImage() (only if Qt is also found on the system).
|
||||
*/
|
||||
|
||||
/*! \page uResourceGeneratorPage uResourceGenerator
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
SET(SRC_FILES
|
||||
UEventsManager.cpp
|
||||
UEventsHandler.cpp
|
||||
UEventsSender.cpp
|
||||
UFile.cpp
|
||||
UDirectory.cpp
|
||||
UConversion.cpp
|
||||
@@ -34,9 +35,10 @@ PROPERTIES
|
||||
)
|
||||
|
||||
INSTALL(TARGETS rtabmap_utilite
|
||||
RUNTIME DESTINATION bin COMPONENT runtime
|
||||
LIBRARY DESTINATION lib COMPONENT devel
|
||||
ARCHIVE DESTINATION lib COMPONENT devel)
|
||||
EXPORT RTABMapTargets
|
||||
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT runtime
|
||||
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT devel
|
||||
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT devel)
|
||||
|
||||
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../include/ DESTINATION include/ COMPONENT devel FILES_MATCHING PATTERN "*.h" PATTERN ".svn" EXCLUDE)
|
||||
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../include/ DESTINATION "${INSTALL_INCLUDE_DIR}" COMPONENT devel FILES_MATCHING PATTERN "*.h" PATTERN ".svn" EXCLUDE)
|
||||
|
||||
|
||||
@@ -22,10 +22,15 @@
|
||||
|
||||
UEventsHandler::~UEventsHandler()
|
||||
{
|
||||
UEventsManager::removeHandler(this);
|
||||
unregisterFromEventsManager();
|
||||
}
|
||||
|
||||
void UEventsHandler::post(UEvent * event, bool async)
|
||||
|
||||
void UEventsHandler::registerToEventsManager()
|
||||
{
|
||||
UEventsManager::post(event, async);
|
||||
UEventsManager::addHandler(this);
|
||||
}
|
||||
void UEventsHandler::unregisterFromEventsManager()
|
||||
{
|
||||
UEventsManager::removeHandler(this);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ void UEventsManager::removeHandler(UEventsHandler* handler)
|
||||
}
|
||||
}
|
||||
|
||||
void UEventsManager::post(UEvent * event, bool async)
|
||||
void UEventsManager::post(UEvent * event, bool async, const UEventsSender * sender)
|
||||
{
|
||||
if(!event)
|
||||
{
|
||||
@@ -60,7 +60,65 @@ void UEventsManager::post(UEvent * event, bool async)
|
||||
}
|
||||
else
|
||||
{
|
||||
UEventsManager::getInstance()->_postEvent(event, async);
|
||||
UEventsManager::getInstance()->_postEvent(event, async, sender);
|
||||
}
|
||||
}
|
||||
|
||||
void UEventsManager::createPipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName)
|
||||
{
|
||||
if(!sender || !receiver)
|
||||
{
|
||||
UERROR("Sender and/or receiver is null!");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
UEventsManager::getInstance()->_createPipe(sender, receiver, eventName);
|
||||
}
|
||||
}
|
||||
|
||||
void UEventsManager::removePipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName)
|
||||
{
|
||||
if(!sender || !receiver)
|
||||
{
|
||||
UERROR("Sender and/or receiver is null!");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
UEventsManager::getInstance()->_removePipe(sender, receiver, eventName);
|
||||
}
|
||||
}
|
||||
|
||||
void UEventsManager::removeAllPipes(const UEventsSender * sender)
|
||||
{
|
||||
if(!sender)
|
||||
{
|
||||
UERROR("Sender is null!");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
UEventsManager::getInstance()->_removeAllPipes(sender);
|
||||
}
|
||||
}
|
||||
|
||||
void UEventsManager::removeNullPipes(const UEventsSender * sender)
|
||||
{
|
||||
if(!sender)
|
||||
{
|
||||
UERROR("Sender is null!");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
UEventsManager::getInstance()->_removeNullPipes(sender);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,9 +142,9 @@ UEventsManager::~UEventsManager()
|
||||
join(true);
|
||||
|
||||
// Free memory
|
||||
for(std::list<UEvent*>::iterator it=events_.begin(); it!=events_.end(); ++it)
|
||||
for(std::list<std::pair<UEvent*, const UEventsSender*> >::iterator it=events_.begin(); it!=events_.end(); ++it)
|
||||
{
|
||||
delete *it;
|
||||
delete it->first;
|
||||
}
|
||||
events_.clear();
|
||||
|
||||
@@ -116,8 +174,8 @@ void UEventsManager::dispatchEvents()
|
||||
return;
|
||||
}
|
||||
|
||||
std::list<UEvent*>::iterator it;
|
||||
std::list<UEvent*> eventsBuf;
|
||||
std::list<std::pair<UEvent*, const UEventsSender*> >::iterator it;
|
||||
std::list<std::pair<UEvent*, const UEventsSender*> > eventsBuf;
|
||||
|
||||
// Copy events in a buffer :
|
||||
// Other threads can post events
|
||||
@@ -132,17 +190,29 @@ void UEventsManager::dispatchEvents()
|
||||
// Past events to handlers
|
||||
for(it=eventsBuf.begin(); it!=eventsBuf.end(); ++it)
|
||||
{
|
||||
dispatchEvent(*it);
|
||||
delete *it;
|
||||
dispatchEvent(it->first, it->second);
|
||||
delete it->first;
|
||||
}
|
||||
eventsBuf.clear();
|
||||
}
|
||||
|
||||
void UEventsManager::dispatchEvent(UEvent * event)
|
||||
void UEventsManager::dispatchEvent(UEvent * event, const UEventsSender * sender)
|
||||
{
|
||||
UEventsHandler * handler;
|
||||
std::list<UEventsHandler*> handlers;
|
||||
|
||||
// Verify if there are pipes with the sender for his type of event
|
||||
if(sender)
|
||||
{
|
||||
handlers = getPipes(sender, event->getClassName());
|
||||
}
|
||||
|
||||
handlersMutex_.lock();
|
||||
std::list<UEventsHandler*> handlers = handlers_;
|
||||
if(handlers.size() == 0)
|
||||
{
|
||||
//No pipes, send to all handlers
|
||||
handlers = handlers_;
|
||||
}
|
||||
|
||||
for(std::list<UEventsHandler*>::iterator it=handlers.begin(); it!=handlers.end(); ++it)
|
||||
{
|
||||
// Check if the handler is still in the
|
||||
@@ -150,7 +220,7 @@ void UEventsManager::dispatchEvent(UEvent * event)
|
||||
// removeHandler() is called in EventsHandler::handleEvent())
|
||||
if(std::find(handlers_.begin(), handlers_.end(), *it) != handlers_.end())
|
||||
{
|
||||
handler = *it;
|
||||
UEventsHandler * handler = *it;
|
||||
handlersMutex_.unlock();
|
||||
|
||||
// To be able to add/remove an handler in a handleEvent call (without a deadlock)
|
||||
@@ -161,7 +231,6 @@ void UEventsManager::dispatchEvent(UEvent * event)
|
||||
}
|
||||
}
|
||||
handlersMutex_.unlock();
|
||||
|
||||
}
|
||||
|
||||
void UEventsManager::_addHandler(UEventsHandler* handler)
|
||||
@@ -204,10 +273,22 @@ void UEventsManager::_removeHandler(UEventsHandler* handler)
|
||||
}
|
||||
}
|
||||
handlersMutex_.unlock();
|
||||
|
||||
pipesMutex_.lock();
|
||||
{
|
||||
for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!= pipes_.end(); ++iter)
|
||||
{
|
||||
if(iter->receiver_ == handler)
|
||||
{
|
||||
iter->receiver_ = 0; // set to null
|
||||
}
|
||||
}
|
||||
}
|
||||
pipesMutex_.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void UEventsManager::_postEvent(UEvent * event, bool async)
|
||||
void UEventsManager::_postEvent(UEvent * event, bool async, const UEventsSender * sender)
|
||||
{
|
||||
if(!this->isKilled())
|
||||
{
|
||||
@@ -215,7 +296,7 @@ void UEventsManager::_postEvent(UEvent * event, bool async)
|
||||
{
|
||||
eventsMutex_.lock();
|
||||
{
|
||||
events_.push_back(event);
|
||||
events_.push_back(std::make_pair(event, sender));
|
||||
}
|
||||
eventsMutex_.unlock();
|
||||
|
||||
@@ -224,7 +305,7 @@ void UEventsManager::_postEvent(UEvent * event, bool async)
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatchEvent(event);
|
||||
dispatchEvent(event, sender);
|
||||
delete event;
|
||||
}
|
||||
}
|
||||
@@ -233,3 +314,152 @@ void UEventsManager::_postEvent(UEvent * event, bool async)
|
||||
delete event;
|
||||
}
|
||||
}
|
||||
|
||||
std::list<UEventsHandler*> UEventsManager::getPipes(
|
||||
const UEventsSender * sender,
|
||||
const std::string & eventName)
|
||||
{
|
||||
std::list<UEventsHandler*> pipes;
|
||||
pipesMutex_.lock();
|
||||
|
||||
for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!= pipes_.end(); ++iter)
|
||||
{
|
||||
if(iter->sender_ == sender && iter->eventName_.compare(eventName) == 0)
|
||||
{
|
||||
bool added = false;
|
||||
if(iter->receiver_)
|
||||
{
|
||||
handlersMutex_.lock();
|
||||
for(std::list<UEventsHandler*>::iterator jter=handlers_.begin(); jter!=handlers_.end(); ++jter)
|
||||
{
|
||||
if(*jter == iter->receiver_)
|
||||
{
|
||||
pipes.push_back(*jter);
|
||||
added = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
handlersMutex_.unlock();
|
||||
}
|
||||
if(!added)
|
||||
{
|
||||
// Add nulls
|
||||
pipes.push_back(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pipesMutex_.unlock();
|
||||
return pipes;
|
||||
}
|
||||
|
||||
void UEventsManager::_createPipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName)
|
||||
{
|
||||
pipesMutex_.lock();
|
||||
bool exist = false;
|
||||
for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!= pipes_.end();++iter)
|
||||
{
|
||||
if(iter->sender_ == sender && iter->receiver_ == receiver && iter->eventName_.compare(eventName) == 0)
|
||||
{
|
||||
exist = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!exist)
|
||||
{
|
||||
bool handlerFound = false;
|
||||
handlersMutex_.lock();
|
||||
for(std::list<UEventsHandler*>::iterator iter=handlers_.begin(); iter!=handlers_.end(); ++iter)
|
||||
{
|
||||
if(*iter == receiver)
|
||||
{
|
||||
handlerFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
handlersMutex_.unlock();
|
||||
if(handlerFound)
|
||||
{
|
||||
pipes_.push_back(Pipe(sender, receiver, eventName));
|
||||
}
|
||||
else
|
||||
{
|
||||
UERROR("Cannot create the pipe because the receiver is not yet "
|
||||
"added to UEventsManager's handlers list.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UWARN("Pipe between sender %p and receiver %p with event %s was already created.",
|
||||
sender, receiver, eventName.c_str());
|
||||
}
|
||||
pipesMutex_.unlock();
|
||||
}
|
||||
|
||||
void UEventsManager::_removePipe(
|
||||
const UEventsSender * sender,
|
||||
const UEventsHandler * receiver,
|
||||
const std::string & eventName)
|
||||
{
|
||||
pipesMutex_.lock();
|
||||
|
||||
bool removed = false;
|
||||
for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!= pipes_.end();)
|
||||
{
|
||||
if(iter->sender_ == sender && iter->receiver_ == receiver && iter->eventName_.compare(eventName) == 0)
|
||||
{
|
||||
iter = pipes_.erase(iter);
|
||||
removed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
if(!removed)
|
||||
{
|
||||
UWARN("Pipe between sender %p and receiver %p with event %s didn't exist.",
|
||||
sender, receiver, eventName.c_str());
|
||||
}
|
||||
|
||||
pipesMutex_.unlock();
|
||||
}
|
||||
|
||||
void UEventsManager::_removeAllPipes(const UEventsSender * sender)
|
||||
{
|
||||
pipesMutex_.lock();
|
||||
for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!=pipes_.end();)
|
||||
{
|
||||
if(iter->sender_ == sender)
|
||||
{
|
||||
iter = pipes_.erase(iter);
|
||||
}
|
||||
else
|
||||
{
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
pipesMutex_.unlock();
|
||||
}
|
||||
|
||||
void UEventsManager::_removeNullPipes(const UEventsSender * sender)
|
||||
{
|
||||
pipesMutex_.lock();
|
||||
for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!=pipes_.end();)
|
||||
{
|
||||
if(iter->receiver_ == 0)
|
||||
{
|
||||
iter = pipes_.erase(iter);
|
||||
}
|
||||
else
|
||||
{
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
pipesMutex_.unlock();
|
||||
}
|
||||
|
||||
31
utilite/src/UEventsSender.cpp
Normal file
31
utilite/src/UEventsSender.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* utilite is a cross-platform library with
|
||||
* useful utilities for fast and small developing.
|
||||
* Copyright (C) 2010 Mathieu Labbe
|
||||
*
|
||||
* utilite is free library: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* utilite is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "rtabmap/utilite/UEventsSender.h"
|
||||
#include "rtabmap/utilite/UEventsManager.h"
|
||||
|
||||
UEventsSender::~UEventsSender()
|
||||
{
|
||||
UEventsManager::removeAllPipes(this);
|
||||
}
|
||||
|
||||
void UEventsSender::post(UEvent * event, bool async) const
|
||||
{
|
||||
UEventsManager::post(event, async, this);
|
||||
}
|
||||
@@ -88,15 +88,12 @@ protected:
|
||||
private:
|
||||
virtual void _write(const char* msg, va_list arg)
|
||||
{
|
||||
if(arg != 0)
|
||||
{
|
||||
vprintf(msg, arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s", msg);
|
||||
}
|
||||
vprintf(msg, arg);
|
||||
}
|
||||
virtual void _writeStr(const char* msg)
|
||||
{
|
||||
printf("%s", msg);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -158,16 +155,16 @@ private:
|
||||
{
|
||||
if(fout_)
|
||||
{
|
||||
if(arg != 0)
|
||||
{
|
||||
vfprintf(fout_, msg, arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(fout_, "%s", msg);
|
||||
}
|
||||
vfprintf(fout_, msg, arg);
|
||||
}
|
||||
}
|
||||
virtual void _writeStr(const char* msg)
|
||||
{
|
||||
if(fout_)
|
||||
{
|
||||
fprintf(fout_, "%s", msg);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string fileName_; ///< the file name
|
||||
@@ -243,7 +240,7 @@ void ULogger::flush()
|
||||
|
||||
void ULogger::_flush()
|
||||
{
|
||||
ULogger::getInstance()->_write(bufferedMsgs_.c_str(), 0);
|
||||
ULogger::getInstance()->_writeStr(bufferedMsgs_.c_str());
|
||||
bufferedMsgs_.clear();
|
||||
}
|
||||
|
||||
@@ -277,7 +274,7 @@ void ULogger::write(const char* msg, ...)
|
||||
}
|
||||
else
|
||||
{
|
||||
ULogger::getInstance()->_write(time.c_str(), 0);
|
||||
ULogger::getInstance()->_writeStr(time.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,7 +297,7 @@ void ULogger::write(const char* msg, ...)
|
||||
}
|
||||
else
|
||||
{
|
||||
ULogger::getInstance()->_write(endline.c_str(), 0);
|
||||
ULogger::getInstance()->_writeStr(endline.c_str());
|
||||
}
|
||||
}
|
||||
loggerMutex_.unlock();
|
||||
@@ -444,7 +441,7 @@ void ULogger::write(ULogger::Level level,
|
||||
}
|
||||
else
|
||||
{
|
||||
ULogger::getInstance()->_write(color, 0);
|
||||
ULogger::getInstance()->_writeStr(color);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -458,9 +455,9 @@ void ULogger::write(ULogger::Level level,
|
||||
}
|
||||
else
|
||||
{
|
||||
ULogger::getInstance()->_write(levelStr.c_str(), 0);
|
||||
ULogger::getInstance()->_write(time.c_str(), 0);
|
||||
ULogger::getInstance()->_write(whereStr.c_str(), 0);
|
||||
ULogger::getInstance()->_writeStr(levelStr.c_str());
|
||||
ULogger::getInstance()->_writeStr(time.c_str());
|
||||
ULogger::getInstance()->_writeStr(whereStr.c_str());
|
||||
ULogger::getInstance()->_write(msg, args);
|
||||
}
|
||||
if(type_ == ULogger::kTypeConsole && printColored_)
|
||||
@@ -474,7 +471,7 @@ void ULogger::write(ULogger::Level level,
|
||||
}
|
||||
else
|
||||
{
|
||||
ULogger::getInstance()->_write(COLOR_NORMAL, 0);
|
||||
ULogger::getInstance()->_writeStr(COLOR_NORMAL);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -484,7 +481,7 @@ void ULogger::write(ULogger::Level level,
|
||||
}
|
||||
else
|
||||
{
|
||||
ULogger::getInstance()->_write(endline.c_str(), 0);
|
||||
ULogger::getInstance()->_writeStr(endline.c_str());
|
||||
}
|
||||
va_end (args);
|
||||
}
|
||||
@@ -493,14 +490,7 @@ void ULogger::write(ULogger::Level level,
|
||||
{
|
||||
std::string fullMsg = uFormat("%s%s%s", levelStr.c_str(), time.c_str(), whereStr.c_str());
|
||||
va_start(args, msg);
|
||||
if(args != 0)
|
||||
{
|
||||
fullMsg.append(uFormatv(msg, args));
|
||||
}
|
||||
else
|
||||
{
|
||||
fullMsg.append(msg);
|
||||
}
|
||||
fullMsg.append(uFormatv(msg, args));
|
||||
va_end(args);
|
||||
if(level >= exitLevel_)
|
||||
{
|
||||
@@ -517,18 +507,14 @@ void ULogger::write(ULogger::Level level,
|
||||
|
||||
if(level >= exitLevel_)
|
||||
{
|
||||
printf("\n*******\n%s message occurred!\n", levelName_[level]);
|
||||
printf(" %s%s%s", levelStr.c_str(), time.c_str(), whereStr.c_str());
|
||||
va_start(args, msg);
|
||||
if(args != 0)
|
||||
printf("\n*******\n%s message occurred! Application will now exit.\n", levelName_[level]);
|
||||
if(type_ != kTypeConsole)
|
||||
{
|
||||
printf(" %s%s%s", levelStr.c_str(), time.c_str(), whereStr.c_str());
|
||||
va_start(args, msg);
|
||||
vprintf(msg, args);
|
||||
va_end(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s", msg);
|
||||
}
|
||||
va_end(args);
|
||||
printf("\n*******\n");
|
||||
destroyer_.setDoomed(0);
|
||||
delete instance_; // If a FileLogger is used, this will close the file.
|
||||
|
||||
Reference in New Issue
Block a user