xoreos  0.0.5
requests.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef EVENTS_REQUESTS_H
26 #define EVENTS_REQUESTS_H
27 
28 #include <list>
29 
30 #include <boost/bind.hpp>
31 
32 #include "src/common/types.h"
33 #include "src/common/ptrlist.h"
34 #include "src/common/singleton.h"
35 #include "src/common/thread.h"
36 
37 #include "src/graphics/types.h"
38 
40 
41 namespace Graphics {
42  class Texture;
43 }
44 
45 namespace Events {
46 
48 typedef RequestList::iterator RequestID;
49 
65 class RequestManager : public Common::Singleton<RequestManager>, public Common::Thread {
66 public:
68 
69  void init();
70  void deinit();
71 
73  void dispatch(RequestID request);
75  void waitReply(RequestID request);
77  void forget(RequestID request);
78 
80  void dispatchAndWait(RequestID request);
82  void dispatchAndForget(RequestID request);
83 
85  void sync();
86 
88  template<typename T> T callInMainThread(const MainThreadFunctor<T> &f) {
90 
91  callInMainThread(caller);
92  if (!f.getError().empty()) {
93  throw Common::Exception(f.getError());
94  }
95 
96  return f.getReturnValue();
97  }
98 
103 
104  // Singleton
105  static void destroy();
106 
107 private:
109 
111 
114 
115  void clearList();
116 
117  void collectGarbage();
118 
119  void threadMethod();
120 
121  void callInMainThread(const MainThreadCallerFunctor &caller);
122 };
123 
124 } // End of namespace Events
125 
127 #define RequestMan Events::RequestManager::instance()
128 
129 #endif // EVENTS_REQUESTS_H
Class and macro for implementing singletons.
T callInMainThread(const MainThreadFunctor< T > &f)
Call this function in the main thread.
Definition: requests.h:88
const Common::Exception & getError() const
Definition: types.h:319
The request manager, handling all requests.
Definition: requests.h:65
void dispatchAndWait(RequestID request)
Dispatch a request and wait for the answer.
Definition: requests.cpp:112
Basic graphics types.
A list storing pointer to objects, with automatic deletion.
Common::Mutex _mutexUse
The mutex locking the use of the manager.
Definition: requests.h:108
Threading helpers.
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
A mutex.
Definition: mutex.h:40
A class that creates its own thread.
Definition: thread.h:44
RequestID rebuild(Graphics::GLContainer &glContainer)
Request that a GL container shall be rebuilt.
Definition: requests.cpp:128
ITCEvent
Specific type of the inter-thread communication.
Definition: types.h:70
Low-level type definitions to handle fixed width types portably.
StackException Exception
Definition: error.h:59
RequestList::iterator RequestID
Definition: requests.h:48
static void destroy()
Definition: requests.cpp:183
boost::function< void()> MainThreadCallerFunctor
Definition: types.h:346
void dispatch(RequestID request)
Dispatch a request.
Definition: requests.cpp:55
RequestList _requests
All currently active requests.
Definition: requests.h:110
void sync()
Request a sync, letting all prior requests finish.
Definition: requests.cpp:122
Common::PtrList< Request > RequestList
Definition: requests.h:47
RequestID newRequest(ITCEvent type)
Create a new, empty request of that type.
Definition: requests.cpp:144
A container of OpenGL elements.
Definition: glcontainer.h:35
void forget(RequestID request)
Ignore any answer we get.
Definition: requests.cpp:106
void dispatchAndForget(RequestID request)
Dispatch a request and ignore the answer.
Definition: requests.cpp:117
void waitReply(RequestID request)
Wait for a request to be answered.
Definition: requests.cpp:74
Inter-thread request event types.
T getReturnValue() const
Definition: types.h:318
A functor for a function that needs to be called in the main thread.
Definition: types.h:298
bool empty() const
Definition: error.cpp:80