Merge branch 'master' into windows
[ardour.git] / libs / pbd / pbd / abstract_ui.h
1 /*
2     Copyright (C) 1998-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_abstract_ui_h__
21 #define __pbd_abstract_ui_h__
22
23 #include <map>
24 #include <string>
25 #include <pthread.h>
26
27 #include <glibmm/threads.h>
28
29 #include "pbd/receiver.h"
30 #include "pbd/ringbufferNPT.h"
31 #include "pbd/signals.h"
32 #include "pbd/base_ui.h"
33
34 class Touchable;
35
36 template<typename RequestObject>
37 class AbstractUI : public BaseUI
38 {
39   public:
40         AbstractUI (const std::string& name);
41         virtual ~AbstractUI() {}
42
43         void register_thread (std::string, pthread_t, std::string, uint32_t num_requests);
44         void call_slot (EventLoop::InvalidationRecord*, const boost::function<void()>&);
45         Glib::Threads::Mutex& slot_invalidation_mutex() { return request_buffer_map_lock; }
46
47         Glib::Threads::Mutex request_buffer_map_lock;
48
49   protected:
50         struct RequestBuffer : public PBD::RingBufferNPT<RequestObject> {
51                 bool dead;
52                 AbstractUI<RequestObject>& ui;
53                 RequestBuffer (uint32_t size, AbstractUI<RequestObject>& uir) 
54                         : PBD::RingBufferNPT<RequestObject> (size)
55                         , dead (false) 
56                         , ui (uir) {}
57         };
58         typedef typename RequestBuffer::rw_vector RequestBufferVector;
59
60 #if defined(__MINGW32__)
61
62         struct pthread_cmp
63         {
64                 bool operator() (const ptw32_handle_t& thread1, const ptw32_handle_t& thread2)
65                 {
66                         return thread1.p < thread2.p;
67                 }
68         };
69         typedef typename std::map<pthread_t,RequestBuffer*, pthread_cmp>::iterator RequestBufferMapIterator;
70         typedef std::map<pthread_t,RequestBuffer*, pthread_cmp> RequestBufferMap;
71 #else
72         typedef typename std::map<pthread_t,RequestBuffer*>::iterator RequestBufferMapIterator;
73         typedef std::map<pthread_t,RequestBuffer*> RequestBufferMap;
74 #endif
75
76         RequestBufferMap request_buffers;
77         static Glib::Threads::Private<RequestBuffer> per_thread_request_buffer;
78         
79         Glib::Threads::Mutex               request_list_lock;
80         std::list<RequestObject*> request_list;
81         
82         RequestObject* get_request (RequestType);
83         void handle_ui_requests ();
84         void send_request (RequestObject *);
85
86         virtual void do_request (RequestObject *) = 0;
87         PBD::ScopedConnection new_thread_connection;
88 };
89
90 #endif /* __pbd_abstract_ui_h__ */
91
92