image_cache.h
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #ifndef TILE_MAP_IMAGE_CACHE_H_
00031 #define TILE_MAP_IMAGE_CACHE_H_
00032 
00033 #include <string>
00034 #include <limits>
00035 
00036 #include <boost/cstdint.hpp>
00037 #include <boost/shared_ptr.hpp>
00038 
00039 #include <QCache>
00040 #include <QImage>
00041 #include <QMap>
00042 #include <QMutex>
00043 #include <QNetworkReply>
00044 #include <QObject>
00045 #include <QSemaphore>
00046 #include <QThread>
00047 #include <set>
00048 
00049 namespace tile_map
00050 {
00051   class CacheThread;
00052 
00053   class Image
00054   {
00055   public:
00056     Image(const QString& uri, size_t uri_hash, uint64_t priority = 0);
00057     ~Image();
00058 
00059     QString Uri() const { return uri_; }
00060     size_t UriHash() const { return uri_hash_; }
00061   
00062     boost::shared_ptr<QImage> GetImage() { return image_; }
00063     
00064     void InitializeImage();
00065     void ClearImage();
00066     
00067     void AddFailure();
00068     bool Failed() const { return failed_; }
00069 
00070     void IncreasePriority()
00071     {
00072       if (priority_ < std::numeric_limits<uint64_t>::max())
00073       {
00074         priority_++;
00075       }
00076     }
00077     void SetPriority(uint64_t priority) { priority_ = priority; }
00078     uint64_t Priority() const { return priority_; }
00079 
00080     bool Loading() const { return loading_; }
00081     void SetLoading(bool loading) { loading_ = loading; }
00082 
00083   private:
00084     QString uri_;
00085     
00086     size_t uri_hash_;
00087     
00088     bool loading_;
00089     int32_t failures_;
00090     bool failed_;
00091     uint64_t priority_;
00092     
00093     mutable boost::shared_ptr<QImage> image_;
00094 
00095     static const int MAXIMUM_FAILURES;
00096   };
00097   typedef boost::shared_ptr<Image> ImagePtr;
00098 
00099   class ImageCache : public QObject
00100   {
00101     Q_OBJECT
00102     
00103   public:
00104     explicit ImageCache(const QString& cache_dir, size_t size = 4096);
00105     ~ImageCache();
00106     
00107     ImagePtr GetImage(size_t uri_hash, const QString& uri, int32_t priority = 0);
00108   
00109   public Q_SLOTS:
00110     void ProcessRequest(QString uri);
00111     void ProcessReply(QNetworkReply* reply);
00112     void NetworkError(QNetworkReply::NetworkError error);
00113     void Clear();
00114   
00115   private:
00116     QNetworkAccessManager network_manager_;
00117     
00118     QString cache_dir_;
00119 
00120     QCache<size_t, ImagePtr> cache_;
00121     QMap<size_t, ImagePtr> unprocessed_;
00122     QMap<QString, size_t> uri_to_hash_map_;
00123     
00124     QMutex cache_mutex_;
00125     QMutex unprocessed_mutex_;
00126     bool exit_;
00127 
00128     uint64_t tick_;
00129     
00130     CacheThread* cache_thread_;
00131 
00132     QSemaphore network_request_semaphore_;
00133 
00134     friend class CacheThread;
00135 
00136     static const int MAXIMUM_NETWORK_REQUESTS;
00137   };
00138   
00139   class CacheThread : public QThread
00140   {
00141     Q_OBJECT
00142     public:
00143       explicit CacheThread(ImageCache* parent);
00144       
00145       virtual void run();
00146 
00147       void notify();
00148 
00149     Q_SIGNALS:
00150       void RequestImage(QString);
00151 
00152     private:
00153       ImageCache* p;
00154       QMutex waiting_mutex_;
00155 
00156       static const int MAXIMUM_SEQUENTIAL_REQUESTS;
00157   };
00158     
00159   
00160   typedef boost::shared_ptr<ImageCache> ImageCachePtr;
00161 }
00162 
00163 #endif  // TILE_MAP_IMAGE_CACHE_H_


tile_map
Author(s): Marc Alban
autogenerated on Thu Aug 24 2017 02:46:24