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 NetworkError(QNetworkReply::NetworkError error);
114  void Clear();
115 
116  private:
117  QNetworkAccessManager network_manager_;
118 
119  QString cache_dir_;
120 
121  QCache<size_t, ImagePtr> cache_;
122  QMap<size_t, ImagePtr> unprocessed_;
123  QSet<size_t> failed_;
124  QMap<QString, size_t> uri_to_hash_map_;
125 
126  QMutex cache_mutex_;
128  bool exit_;
129 
130  uint64_t tick_;
131 
133 
135 
136  friend class CacheThread;
137 
138  static const int MAXIMUM_NETWORK_REQUESTS;
139  };
140 
141  class CacheThread : public QThread
142  {
143  Q_OBJECT
144  public:
145  explicit CacheThread(ImageCache* parent);
146 
147  virtual void run();
148 
149  void notify();
150 
151  Q_SIGNALS:
152  void RequestImage(QString);
153 
154  private:
157 
158  static const int MAXIMUM_SEQUENTIAL_REQUESTS;
159  };
160 
161 
163 }
164 
165 #endif // TILE_MAP_IMAGE_CACHE_H_
QMap< size_t, ImagePtr > unprocessed_
Definition: image_cache.h:122
boost::shared_ptr< QImage > GetImage()
Definition: image_cache.h:63
static const int MAXIMUM_FAILURES
Definition: image_cache.h:96
void IncreasePriority()
Definition: image_cache.h:71
int32_t failures_
Definition: image_cache.h:90
QSet< size_t > failed_
Definition: image_cache.h:123
boost::shared_ptr< QImage > image_
Definition: image_cache.h:94
QNetworkAccessManager network_manager_
Definition: image_cache.h:117
void InitializeImage()
Definition: image_cache.cpp:66
boost::shared_ptr< ImageCache > ImageCachePtr
Definition: image_cache.h:162
uint64_t Priority() const
Definition: image_cache.h:79
size_t uri_hash_
Definition: image_cache.h:87
size_t UriHash() const
Definition: image_cache.h:61
QString Uri() const
Definition: image_cache.h:60
QSemaphore network_request_semaphore_
Definition: image_cache.h:134
QCache< size_t, ImagePtr > cache_
Definition: image_cache.h:121
void SetPriority(uint64_t priority)
Definition: image_cache.h:78
Image(const QString &uri, size_t uri_hash, uint64_t priority=0)
Definition: image_cache.cpp:52
boost::shared_ptr< Image > ImagePtr
Definition: image_cache.h:98
static const int MAXIMUM_NETWORK_REQUESTS
Definition: image_cache.h:138
bool Failed() const
Definition: image_cache.h:69
uint64_t priority_
Definition: image_cache.h:92
QMap< QString, size_t > uri_to_hash_map_
Definition: image_cache.h:124
void run(ClassLoader *loader)
bool Loading() const
Definition: image_cache.h:81
static const int MAXIMUM_SEQUENTIAL_REQUESTS
Definition: image_cache.h:158
CacheThread * cache_thread_
Definition: image_cache.h:132
void SetLoading(bool loading)
Definition: image_cache.h:82


tile_map
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 19:25:35