UObjDeletionThread.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef UOBJDELETIONTHREAD_H
00021 #define UOBJDELETIONTHREAD_H
00022 
00023 #include "rtabmap/utilite/UThreadNode.h"
00024 #include "rtabmap/utilite/UEvent.h"
00025 #include "rtabmap/utilite/UEventsManager.h"
00026 
00031 class UObjDeletedEvent : public UEvent
00032 {
00033 public:
00034         UObjDeletedEvent(int objDeletionThreadId) : UEvent(objDeletionThreadId) {}
00035         virtual ~UObjDeletedEvent() {}
00039         virtual std::string getClassName() const {return std::string("UObjDeletedEvent");}
00040 };
00041 
00047 template<class T>
00048 class UObjDeletionThread : public UThread
00049 {
00050 public:
00056         UObjDeletionThread(T * obj, int id=0) :
00057                 obj_(obj),
00058                 id_(id),
00059                 waitMs_(0) {}
00060 
00068         virtual ~UObjDeletionThread()
00069         {
00070                 join(true);
00071                 if(obj_)
00072                 {
00073                         delete obj_;
00074                 }
00075         }
00076 
00081         void startDeletion(int waitMs = 0) {waitMs_ = waitMs; this->start();}
00082 
00087         int id() const {return id_;}
00088 
00093         void setObj(T * obj)
00094         {
00095                 join();
00096                 if(obj_)
00097                 {
00098                         delete obj_;
00099                         obj_ = 0;
00100                 }
00101                 obj_ = obj;
00102         }
00103 
00104 private:
00108         virtual void mainLoop()
00109         {
00110                 if(waitMs_)
00111                 {
00112                         uSleep(waitMs_);
00113                 }
00114                 if(obj_)
00115                 {
00116                         delete obj_;
00117                         obj_ = 0;
00118                 }
00119                 this->kill();
00120                 UEventsManager::post(new UObjDeletedEvent(id_), false);
00121         }
00122 
00123 private:
00124         T * obj_;
00125         int id_;
00126         int waitMs_;
00127 };
00128 
00129 #endif /* UOBJDELETIONTHREAD_H */


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:28