Main Page · Modules · All Classes · Class Hierarchy
MSThread.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MSThread.hpp"
23 
24 #include <MCLog.hpp>
25 
27 {
28  exec();
29  RemoveObjects();
30 }
31 
32 
33 void MSThread::AddObject(QObject& object)
34 {
35  MC_LOG("this: %p obj: %p obj thread: %p", this, &object, object.thread());
36  if (ObjectThreads.contains(&object))
37  {
38  MC_WARNING("The object has been moved to this thread already!");
39  return;
40  }
41  ObjectThreads[&object] = object.thread();
42  object.moveToThread(this);
43 }
44 
45 
47 {
48  if (!ObjectThreads.isEmpty())
49  {
50  for (auto iter = ObjectThreads.begin(); iter != ObjectThreads.end(); ++iter)
51  {
52  MC_LOG("Move object (%p) to its original thread (%p -> %p)",
53  iter.key(), this, iter.value());
54  iter.key()->moveToThread(iter.value());
55  }
56  ObjectThreads.clear();
57  } else {
58  MC_WARNING("No object is registered to move back to its original thread.");
59  }
60 }
61 
62 
64 {
65  return ObjectThreads.isEmpty();
66 }
bool IsEmpty()
Whether object is attached to the thread.
Definition: MSThread.cpp:63
#define MC_WARNING(...)
Warning macro.
Definition: MCLog.hpp:43
void AddObject(QObject &object)
Add an object to this thread.
Definition: MSThread.cpp:33
void RemoveObjects()
Move the objects back to their original thread.
Definition: MSThread.cpp:46
QMap< QObject *, QThread * > ObjectThreads
Remember for the original thread of the moved objects.
Definition: MSThread.hpp:73
#define MC_LOG(...)
Debug macro.
Definition: MCLog.hpp:41
virtual void run() override
Run the thread.
Definition: MSThread.cpp:26