]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.h
Alfredo's Loader Queue.
[lyx.git] / src / graphics / LoaderQueue.h
1 // -*- C++ -*-
2 /**
3  *  \file LoaderQueue.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  *  \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS. 
10  *
11  * This implements a threaded service queue which loads images on background.
12  * In order to request an image loading you call touch() with the pointer to
13  * the cached image. Then it will try to satisfy the request as soon as 
14  * posible (that's it: after finishing an eventual loading on progress)
15  * touch() returns inmediately, in order not tu disrupt the flow of the main
16  * thread.
17  * The service thread is the method loadNext(). It's actually not a thread,
18  * but implemented with a timer that comes back every x msec.
19  */
20
21 #ifndef LOADERQUEUE_H
22 #define LOADERQUEUE_H
23
24 #include "GraphicsCache.h"
25 #include "GraphicsCacheItem.h"
26
27 #include "frontends/Timeout.h"
28
29 #include <set>
30 #include <queue>
31
32 namespace grfx {
33
34 class LoaderQueue {
35 public:
36         //use this to request a loading
37         void touch(Cache::ItemPtr const & item);
38         //query if the clock is ticking
39         bool running() const;
40         //get the and only instance of the class
41         static LoaderQueue & get();
42 private:
43         //this class is a singleton class... use LoaderQueue::get() instead
44         LoaderQueue();
45         //in-progress loading queue (elements are unique here) 
46         std::list<Cache::ItemPtr> cache_queue_;
47         //makes faster the insertion of new elements
48         std::set<Cache::ItemPtr> cache_set_;
49         //newly touched element go here, loadNext move them to cache_queue_
50         std::queue<Cache::ItemPtr> bucket_;
51         //
52         Timeout timer;
53         //
54         bool running_;
55         //moves bucket_ to cache_queue_
56         void emptyBucket();
57         //adds or reprioritizes one element in cache_queue_
58         void addToQueue(Cache::ItemPtr const & item);
59         //this is the 'threaded' method, that does the loading in background
60         void loadNext();
61         //
62         void startLoader();
63         //
64         void stopLoader();
65 };
66
67 } // namespace grfx
68
69 #endif // LOADERQUEUE_H