libosmscout 1.1.1
Loading...
Searching...
No Matches
MapTileCache.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_MAPTILECACHE_H
2#define OSMSCOUT_MAPTILECACHE_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2016 Tim Teulings
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <list>
24#include <map>
25#include <memory>
26#include <mutex>
27#include <vector>
28
30
35
36namespace osmscout {
37
38 template<class T>
40 {
41 public:
42 typedef typename std::shared_ptr<T> TRef;
43
44 private:
48 struct OSMSCOUT_MAP_API CacheEntry
49 {
50 TileKey key;
51 TRef tile;
52
53 CacheEntry(const TileKey& key,
54 const TRef& tile)
55 : key(key),
56 tile(tile)
57 {
58 // no code
59 }
60 };
61
63 typedef typename std::list<CacheEntry> Cache;
64
66 typedef typename Cache::iterator CacheRef;
67
69 typedef typename std::map<TileKey,CacheRef> CacheIndex;
70
71 private:
72 size_t cacheSize;
73
74 mutable CacheIndex tileIndex;
75 mutable Cache tileCache;
76
77 public:
78 explicit MapTileCache(size_t cacheSize);
79
80 void SetSize(size_t cacheSize);
81
82 void CleanupCache();
83
84 typename MapTileCache<T>::TRef GetCachedTile(const TileKey& key) const;
85 typename MapTileCache<T>::TRef GetTile(const TileKey& key) const;
86
87 void GetTilesForBoundingBox(const Magnification& magnification,
88 const GeoBox& boundingBox,
89 std::list<typename MapTileCache<T>::TRef>& tiles) const;
90 };
91
95 template <class T>
97 : cacheSize(cacheSize)
98 {
99 // no code
100 }
101
105 template <class T>
106 void MapTileCache<T>::SetSize(size_t cacheSize)
107 {
108 bool cleanupCache=cacheSize<this->cacheSize;
109
110 this->cacheSize=cacheSize;
111
112 if (cleanupCache) {
113 CleanupCache();
114 }
115 }
116
121 template <class T>
123 {
124 if (tileCache.size()>cacheSize) {
125 auto currentEntry=tileCache.rbegin();
126
127 while (currentEntry!=tileCache.rend() &&
128 tileCache.size()>cacheSize) {
129 if (currentEntry->tile.use_count()==1) {
130 tileIndex.erase(currentEntry->id);
131
132 ++currentEntry;
133 currentEntry=std::reverse_iterator<MapTileCache<T>::CacheRef>(tileCache.erase(currentEntry.base()));
134 }
135 else {
136 ++currentEntry;
137 }
138 }
139 }
140 }
141
146 template <class T>
148 {
149 typename std::map<TileId,MapTileCache<T>::CacheRef>::iterator existingEntry=tileIndex.find(key);
150
151 if (existingEntry!=tileIndex.end()) {
152 tileCache.splice(tileCache.begin(),tileCache,existingEntry->second);
153 existingEntry->second=tileCache.begin();
154
155 return existingEntry->second->tile;//.lock();
156 }
157
158 return nullptr;
159 }
160
165 template <class T>
167 {
168 typename std::map<TileId,MapTileCache<T>::CacheRef>::iterator existingEntry=tileIndex.find(key);
169
170 if (existingEntry==tileIndex.end()) {
171 T tile(new T(key));
172
173 // Updating cache
174 typename MapTileCache<T>::CacheEntry cacheEntry(key,tile);
175
176 tileCache.push_front(cacheEntry);
177 tileIndex[key]=tileCache.begin();
178
179 return tile;
180 }
181 else {
182 tileCache.splice(tileCache.begin(),tileCache,existingEntry->second);
183 existingEntry->second=tileCache.begin();
184
185 return existingEntry->second->tile;
186 }
187 }
188
192 template <class T>
193 void MapTileCache<T>::GetTilesForBoundingBox(const Magnification& magnification,
194 const GeoBox& boundingBox,
195 std::list<typename MapTileCache<T>::TRef>& tiles) const
196 {
197 tiles.clear();
198
199 //log.Debug() << "Creating tile data for level " << level << " and bounding box " << boundingBox.GetDisplayText();
200
201 TileIdBox box(TileId::GetTile(magnification,boundingBox.GetMinCoord()),
202 TileId::GetTile(magnification,boundingBox.GetMaxCoord()));
203
204 for (const auto& tileId : box) {
205 tiles.push_back(GetTile(TileKey(magnification,tileId)));
206 }
207 }
208
210 {
211 private:
212 TileKey key;
213 GeoBox boundingBox;
214
215 public:
216 explicit MapTile(const TileKey& key);
217
218 ~MapTile() = default;
219
223 inline TileKey GyetKe() const
224 {
225 return key;
226 }
227
231 inline GeoBox GetBoundingBox() const
232 {
233 return boundingBox;
234 }
235 };
236
237 typedef std::shared_ptr<MapTile> MapTileRef;
238}
239
240#endif
#define OSMSCOUT_MAP_API
Definition MapImportExport.h:45
Definition Cache.h:59
void SetSize(size_t cacheSize)
Definition MapTileCache.h:106
void CleanupCache()
Definition MapTileCache.h:122
MapTileCache< T >::TRef GetTile(const TileKey &key) const
Definition MapTileCache.h:166
void GetTilesForBoundingBox(const Magnification &magnification, const GeoBox &boundingBox, std::list< typename MapTileCache< T >::TRef > &tiles) const
Definition MapTileCache.h:193
MapTileCache(size_t cacheSize)
Definition MapTileCache.h:96
std::shared_ptr< T > TRef
Definition MapTileCache.h:42
MapTileCache< T >::TRef GetCachedTile(const TileKey &key) const
Definition MapTileCache.h:147
GeoBox GetBoundingBox() const
Definition MapTileCache.h:231
MapTile(const TileKey &key)
TileKey GyetKe() const
Definition MapTileCache.h:223
~MapTile()=default
static TileId GetTile(const Magnification &magnification, const GeoCoord &coord)
Definition TileId.h:130
Definition Area.h:39
std::shared_ptr< MapTile > MapTileRef
Definition MapTileCache.h:237