tile_cache_delay.h
Go to the documentation of this file.
1 /* Copyright 2018-2019 TomTom N.V.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7 http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License. */
14 
15 #pragma once
16 
17 #include <vector>
18 
19 #include <QTimer>
20 
21 #include "tile_cache.h"
22 
23 template <typename Tile>
25 
26 namespace detail
27 {
34 {
35  QTimer timer;
37 
38  ExpiringArea(Area area) : area(std::move(area))
39  {
40  timer.setSingleShot(true);
41  int constexpr timeout = 2000; // in ms
42  timer.start(timeout);
43  }
44 
45  ExpiringArea(ExpiringArea&&) = default;
46  ExpiringArea(ExpiringArea const& p) : area(p.area)
47  {
48  *this = p;
49  }
50 
56  template <typename Tile>
57  bool ready(TileCacheDelay<Tile> const& cache) const
58  {
59  if (!timer.isActive())
60  {
61  return true;
62  }
63 
64  return cache.isAreaReady(area);
65  }
66 
67  ExpiringArea& operator=(ExpiringArea&&) = default;
68 
70  {
71  area = p.area;
72 
73  // QTimer has no copy operator
74  if (p.timer.isActive())
75  {
76  timer.start(p.timer.remainingTime());
77  }
78  return *this;
79  }
80 };
81 
86 {
88  std::vector<ExpiringArea> history_;
89 
90 public:
94  void fit(Area const& area)
95  {
96  history_.erase(std::remove_if(history_.begin(), history_.end(),
97  [&area](ExpiringArea const& p) { return !areaContainsTile(p.area, area.center); }),
98  history_.end());
99  }
100 
106  void add(Area const& area)
107  {
108  auto const it =
109  std::find_if(history_.begin(), history_.end(), [&area](ExpiringArea const& p) { return p.area == area; });
110  if (it == history_.end())
111  {
112  history_.emplace_back(area);
113  }
114  };
115 
119  template <typename Tile>
120  bool ready(TileCacheDelay<Tile> const& cache, TileId const& to_find) const
121  {
122  return std::any_of(history_.begin(), history_.end(), [&to_find, &cache](ExpiringArea const& ea) {
123  return areaContainsTile(ea.area, to_find) && ea.ready(cache);
124  });
125  }
126 };
127 } // namespace detail
128 
148 template <typename Tile>
149 class TileCacheDelay : public TileCache<Tile>
150 {
152  friend detail::ExpiringArea;
153 
154 public:
155  void request(Area const& area)
156  {
158 
159  history_.fit(area);
160  history_.add(area);
161  }
162 
166  Tile const* ready(TileId const& to_find) const
167  {
168  Tile const* tile = TileCache<Tile>::ready(to_find);
169  if (tile && history_.ready(*this, to_find))
170  {
171  return tile;
172  }
173 
174  return nullptr;
175  }
176 };
void request(Area const &area)
bool ready(TileCacheDelay< Tile > const &cache) const
bool areaContainsTile(Area const &haystack, TileId const &needle)
Definition: area.h:75
void request(Area const &area)
Definition: tile_cache.h:90
bool ready(TileCacheDelay< Tile > const &cache, TileId const &to_find) const
A cache for tiles.
Definition: tile_cache.h:61
TileId center
Definition: area.h:35
ExpiringArea & operator=(ExpiringArea const &p)
Definition: area.h:31
Tile const * ready(TileId const &to_find) const
std::vector< ExpiringArea > history_
History of areas that will be or were drawn in Rviz.
detail::AreaHistory history_
bool isAreaReady(Area const &area) const
Definition: tile_cache.h:158
ExpiringArea(ExpiringArea const &p)
ExpiringArea & operator=(ExpiringArea &&)=default
void fit(Area const &area)
Definition: tile_id.h:31
void add(Area const &area)
Tile const * ready(TileId const &to_find) const
Definition: tile_cache.h:112


rviz_satellite
Author(s): Gareth Cross , Andre Schröder
autogenerated on Thu May 4 2023 02:31:43