image_cache.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #ifndef TILE_MAP_IMAGE_CACHE_H_
31 #define TILE_MAP_IMAGE_CACHE_H_
32 
33 #include <string>
34 #include <limits>
35 
36 #include <boost/cstdint.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 #include <QCache>
40 #include <QImage>
41 #include <QMap>
42 #include <QMutex>
43 #include <QNetworkReply>
44 #include <QObject>
45 #include <QSemaphore>
46 #include <QSet>
47 #include <QThread>
48 #include <set>
49 
50 namespace tile_map
51 {
52  class CacheThread;
53 
54  class Image
55  {
56  public:
57  Image(const QString& uri, size_t uri_hash, uint64_t priority = 0);
58  ~Image();
59 
60  QString Uri() const { return uri_; }
61  size_t UriHash() const { return uri_hash_; }
62 
64 
65  void InitializeImage();
66  void ClearImage();
67 
68  void AddFailure();
69  bool Failed() const { return failed_; }
70 
72  {
73  if (priority_ < std::numeric_limits<uint64_t>::max())
74  {
75  priority_++;
76  }
77  }
78  void SetPriority(uint64_t priority) { priority_ = priority; }
79  uint64_t Priority() const { return priority_; }
80 
81  bool Loading() const { return loading_; }
82  void SetLoading(bool loading) { loading_ = loading; }
83 
84  private:
85  QString uri_;
86 
87  size_t uri_hash_;
88 
89  bool loading_;
90  int32_t failures_;
91  bool failed_;
92  uint64_t priority_;
93 
95 
96  static const int MAXIMUM_FAILURES;
97  };
99 
100  class ImageCache : public QObject
101  {
102  Q_OBJECT
103 
104  public:
105  explicit ImageCache(const QString& cache_dir, size_t size = 4096);
106  ~ImageCache();
107 
108  ImagePtr GetImage(size_t uri_hash, const QString& uri, int32_t priority = 0);
109 
110  public Q_SLOTS:
111  void ProcessRequest(QString uri);
112  void ProcessReply(QNetworkReply* reply);
113  void Clear();
114 
115  private:
116  QNetworkAccessManager network_manager_;
117 
118  QString cache_dir_;
119 
120  QCache<size_t, ImagePtr> cache_;
121  QMap<size_t, ImagePtr> unprocessed_;
122  QSet<size_t> failed_;
123  QMap<QString, size_t> uri_to_hash_map_;
124 
125  QMutex cache_mutex_;
127  bool exit_;
128 
129  uint64_t tick_;
130 
132 
134 
135  friend class CacheThread;
136 
137  static const int MAXIMUM_NETWORK_REQUESTS;
138  };
139 
140  class CacheThread : public QThread
141  {
142  Q_OBJECT
143  public:
144  explicit CacheThread(ImageCache* parent);
145 
146  virtual void run();
147 
148  void notify();
149 
150  Q_SIGNALS:
151  void RequestImage(QString);
152 
153  private:
156 
157  static const int MAXIMUM_SEQUENTIAL_REQUESTS;
158  };
159 
160 
162 }
163 
164 #endif // TILE_MAP_IMAGE_CACHE_H_
tile_map::Image::MAXIMUM_FAILURES
static const int MAXIMUM_FAILURES
Definition: image_cache.h:96
tile_map::Image::priority_
uint64_t priority_
Definition: image_cache.h:92
tile_map::ImageCache::unprocessed_mutex_
QMutex unprocessed_mutex_
Definition: image_cache.h:126
tile_map::ImageCache::cache_mutex_
QMutex cache_mutex_
Definition: image_cache.h:125
tile_map::Image::SetLoading
void SetLoading(bool loading)
Definition: image_cache.h:82
tile_map::Image::failures_
int32_t failures_
Definition: image_cache.h:90
tile_map::ImageCache::uri_to_hash_map_
QMap< QString, size_t > uri_to_hash_map_
Definition: image_cache.h:123
boost::shared_ptr< QImage >
tile_map::Image::InitializeImage
void InitializeImage()
Definition: image_cache.cpp:66
tile_map::ImageCache::failed_
QSet< size_t > failed_
Definition: image_cache.h:122
tile_map::ImageCache::Clear
void Clear()
Definition: image_cache.cpp:115
tile_map::CacheThread::CacheThread
CacheThread(ImageCache *parent)
Definition: image_cache.cpp:250
tile_map::Image::loading_
bool loading_
Definition: image_cache.h:89
tile_map::ImageCache::ProcessRequest
void ProcessRequest(QString uri)
Definition: image_cache.cpp:192
tile_map::ImageCache::tick_
uint64_t tick_
Definition: image_cache.h:129
tile_map::CacheThread
Definition: image_cache.h:140
tile_map::ImageCache::network_request_semaphore_
QSemaphore network_request_semaphore_
Definition: image_cache.h:133
tile_map::Image::AddFailure
void AddFailure()
Definition: image_cache.cpp:76
tile_map::Image::GetImage
boost::shared_ptr< QImage > GetImage()
Definition: image_cache.h:63
tile_map::Image::Image
Image(const QString &uri, size_t uri_hash, uint64_t priority=0)
Definition: image_cache.cpp:52
tile_map::ImageCache
Definition: image_cache.h:100
tile_map::ImageCachePtr
boost::shared_ptr< ImageCache > ImageCachePtr
Definition: image_cache.h:161
tile_map::CacheThread::image_cache_
ImageCache * image_cache_
Definition: image_cache.h:154
tile_map::ImageCache::unprocessed_
QMap< size_t, ImagePtr > unprocessed_
Definition: image_cache.h:121
tile_map::Image::ClearImage
void ClearImage()
Definition: image_cache.cpp:71
tile_map
Definition: bing_source.h:45
tile_map::Image::failed_
bool failed_
Definition: image_cache.h:91
tile_map::ImageCache::MAXIMUM_NETWORK_REQUESTS
static const int MAXIMUM_NETWORK_REQUESTS
Definition: image_cache.h:137
tile_map::CacheThread::MAXIMUM_SEQUENTIAL_REQUESTS
static const int MAXIMUM_SEQUENTIAL_REQUESTS
Definition: image_cache.h:157
tile_map::ImageCache::~ImageCache
~ImageCache()
Definition: image_cache.cpp:104
tile_map::Image::Uri
QString Uri() const
Definition: image_cache.h:60
tile_map::Image::image_
boost::shared_ptr< QImage > image_
Definition: image_cache.h:94
tile_map::ImageCache::ImageCache
ImageCache(const QString &cache_dir, size_t size=4096)
Definition: image_cache.cpp:84
tile_map::Image::SetPriority
void SetPriority(uint64_t priority)
Definition: image_cache.h:78
tile_map::Image
Definition: image_cache.h:54
tile_map::CacheThread::waiting_mutex_
QMutex waiting_mutex_
Definition: image_cache.h:155
tile_map::ImageCache::network_manager_
QNetworkAccessManager network_manager_
Definition: image_cache.h:116
tile_map::Image::uri_hash_
size_t uri_hash_
Definition: image_cache.h:87
tile_map::Image::~Image
~Image()
Definition: image_cache.cpp:62
tile_map::ImageCache::ProcessReply
void ProcessReply(QNetworkReply *reply)
Definition: image_cache.cpp:207
tile_map::ImageCache::cache_
QCache< size_t, ImagePtr > cache_
Definition: image_cache.h:120
tile_map::Image::IncreasePriority
void IncreasePriority()
Definition: image_cache.h:71
tile_map::Image::UriHash
size_t UriHash() const
Definition: image_cache.h:61
tile_map::CacheThread::notify
void notify()
Definition: image_cache.cpp:257
tile_map::ImagePtr
boost::shared_ptr< Image > ImagePtr
Definition: image_cache.h:98
tile_map::CacheThread::RequestImage
void RequestImage(QString)
tile_map::ImageCache::GetImage
ImagePtr GetImage(size_t uri_hash, const QString &uri, int32_t priority=0)
Definition: image_cache.cpp:121
tile_map::Image::uri_
QString uri_
Definition: image_cache.h:85
tile_map::CacheThread::run
virtual void run()
Definition: image_cache.cpp:262
tile_map::Image::Failed
bool Failed() const
Definition: image_cache.h:69
tile_map::ImageCache::cache_dir_
QString cache_dir_
Definition: image_cache.h:118
tile_map::Image::Priority
uint64_t Priority() const
Definition: image_cache.h:79
tile_map::Image::Loading
bool Loading() const
Definition: image_cache.h:81
tile_map::ImageCache::cache_thread_
CacheThread * cache_thread_
Definition: image_cache.h:131
tile_map::ImageCache::exit_
bool exit_
Definition: image_cache.h:127


tile_map
Author(s): Marc Alban
autogenerated on Sun Sep 8 2024 02:27:24