6e7e42e9dd1a50fd164c01f870701cd6a77fda22
[ardour.git] / libs / pbd / pbd / event_loop.h
1 /*
2     Copyright (C) 2009 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __pbd_event_loop_h__
21 #define __pbd_event_loop_h__
22
23 #include <boost/function.hpp>
24 #include <boost/bind.hpp> /* we don't need this here, but anything calling call_slot() probably will, so this is convenient */
25 #include <glibmm/thread.h>
26
27 namespace PBD
28 {
29
30 class EventLoop 
31 {
32   public:
33         EventLoop() {}
34         virtual ~EventLoop() {}
35
36         enum RequestType {
37                 range_guarantee = ~0
38         };
39
40         struct BaseRequestObject;
41     
42         struct InvalidationRecord {
43             std::list<BaseRequestObject*> requests;
44             PBD::EventLoop* event_loop;
45             const char* file;
46             int line;
47
48             InvalidationRecord() : event_loop (0) {}
49         };
50
51         static void* invalidate_request (void* data);
52
53         struct BaseRequestObject {
54             RequestType             type;
55             bool                    valid;
56             InvalidationRecord*     invalidation;
57             boost::function<void()> the_slot;
58             
59             BaseRequestObject() : valid (true), invalidation (0) {}
60         };
61
62         virtual void call_slot (InvalidationRecord*, const boost::function<void()>&) = 0;
63         virtual Glib::Mutex& slot_invalidation_mutex() = 0;
64
65         static EventLoop* get_event_loop_for_thread();
66         static void set_event_loop_for_thread (EventLoop* ui);
67
68   private:
69         static Glib::StaticPrivate<EventLoop> thread_event_loop;
70
71 };
72
73 }
74
75 #define MISSING_INVALIDATOR 0 // used to mark places where we fail to provide an invalidator
76
77 #endif /* __pbd_event_loop_h__ */