UObjDeletionThread.h
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef UOBJDELETIONTHREAD_H
21 #define UOBJDELETIONTHREAD_H
22 
24 #include "rtabmap/utilite/UEvent.h"
26 
31 class UObjDeletedEvent : public UEvent
32 {
33 public:
34  UObjDeletedEvent(int objDeletionThreadId) : UEvent(objDeletionThreadId) {}
35  virtual ~UObjDeletedEvent() {}
39  virtual std::string getClassName() const {return std::string("UObjDeletedEvent");}
40 };
41 
47 template<class T>
49 {
50 public:
56  UObjDeletionThread(T * obj, int id=0) :
57  obj_(obj),
58  id_(id),
59  waitMs_(0) {}
60 
69  {
70  join(true);
71  if(obj_)
72  {
73  delete obj_;
74  }
75  }
76 
81  void startDeletion(int waitMs = 0) {waitMs_ = waitMs; this->start();}
82 
87  int id() const {return id_;}
88 
93  void setObj(T * obj)
94  {
95  join();
96  if(obj_)
97  {
98  delete obj_;
99  obj_ = 0;
100  }
101  obj_ = obj;
102  }
103 
104 private:
108  virtual void mainLoop()
109  {
110  if(waitMs_)
111  {
112  uSleep(waitMs_);
113  }
114  if(obj_)
115  {
116  delete obj_;
117  obj_ = 0;
118  }
119  this->kill();
120  UEventsManager::post(new UObjDeletedEvent(id_), false);
121  }
122 
123 private:
124  T * obj_;
125  int id_;
126  int waitMs_;
127 };
128 
129 #endif /* UOBJDELETIONTHREAD_H */
static void post(UEvent *event, bool async=true, const UEventsSender *sender=0)
Definition: UEvent.h:57
virtual void mainLoop()
virtual std::string getClassName() const
UObjDeletedEvent(int objDeletionThreadId)
void startDeletion(int waitMs=0)
UObjDeletionThread(T *obj, int id=0)
void uSleep(unsigned int ms)
virtual ~UObjDeletedEvent()


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:43:40