1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#pragma once
#include "indexer/cell_id.hpp"
#include "indexer/data_factory.hpp"
#include "indexer/feature_covering.hpp"
#include "indexer/features_offsets_table.hpp"
#include "indexer/features_vector.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/osm_editor.hpp"
#include "indexer/scale_index.hpp"
#include "indexer/unique_index.hpp"
#include "coding/file_container.hpp"
#include "defines.hpp"
#include "base/macros.hpp"
#include "base/observer_list.hpp"
#include "std/algorithm.hpp"
#include "std/limits.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"
class MwmInfoEx : public MwmInfo
{
public:
unique_ptr<feature::FeaturesOffsetsTable> m_table;
};
class MwmValue : public MwmSet::MwmValueBase
{
public:
FilesContainerR const m_cont;
IndexFactory m_factory;
platform::LocalCountryFile const m_file;
feature::FeaturesOffsetsTable const * m_table;
explicit MwmValue(platform::LocalCountryFile const & localFile);
void SetTable(MwmInfoEx & info);
inline feature::DataHeader const & GetHeader() const { return m_factory.GetHeader(); }
inline version::MwmVersion const & GetMwmVersion() const { return m_factory.GetMwmVersion(); }
inline string const & GetCountryFileName() const { return m_file.GetCountryFile().GetNameWithoutExt(); }
};
class Index : public MwmSet
{
protected:
/// @name MwmSet overrides.
//@{
unique_ptr<MwmInfo> CreateInfo(platform::LocalCountryFile const & localFile) const override;
unique_ptr<MwmValueBase> CreateValue(MwmInfo & info) const override;
void OnMwmDeregistered(platform::LocalCountryFile const & localFile) override;
//@}
public:
/// An Observer interface to MwmSet. Note that these functions can
/// be called from *ANY* thread because most signals are sent when
/// some thread releases its MwmHandle, so overrides must be as fast
/// as possible and non-blocking when it's possible.
class Observer
{
public:
virtual ~Observer() = default;
/// Called when a map is registered for a first time and can be
/// used.
virtual void OnMapRegistered(platform::LocalCountryFile const & localFile) {}
/// Called when a map is deregistered and can not be used.
virtual void OnMapDeregistered(platform::LocalCountryFile const & localFile) {}
};
/// Registers a new map.
pair<MwmId, RegResult> RegisterMap(platform::LocalCountryFile const & localFile);
/// Deregisters a map from internal records.
///
/// \param countryFile A countryFile denoting a map to be deregistered.
/// \return True if the map was successfully deregistered. If map is locked
/// now, returns false.
bool DeregisterMap(platform::CountryFile const & countryFile);
bool AddObserver(Observer & observer);
bool RemoveObserver(Observer const & observer);
private:
template <typename F> class ReadMWMFunctor
{
F & m_f;
osm::Editor & m_editor = osm::Editor::Instance();
public:
ReadMWMFunctor(F & f) : m_f(f) {}
/// Used by Editor to inject new features.
void operator()(FeatureType & feature)
{
m_f(feature);
}
void operator()(MwmHandle const & handle, covering::CoveringGetter & cov, uint32_t scale) const
{
MwmValue const * pValue = handle.GetValue<MwmValue>();
if (pValue)
{
feature::DataHeader const & header = pValue->GetHeader();
// Prepare needed covering.
uint32_t const lastScale = header.GetLastScale();
// In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
if (scale > lastScale)
scale = lastScale;
// Use last coding scale for covering (see index_builder.cpp).
covering::IntervalsT const & interval = cov.Get(lastScale);
// Prepare features reading.
FeaturesVector const fv(pValue->m_cont, header, pValue->m_table);
ScaleIndex<ModelReaderPtr> index(pValue->m_cont.GetReader(INDEX_FILE_TAG),
pValue->m_factory);
// iterate through intervals
CheckUniqueIndexes checkUnique(header.GetFormat() >= version::Format::v5);
MwmId const & mwmID = handle.GetId();
for (auto const & i : interval)
{
index.ForEachInIntervalAndScale(
[&](uint32_t index)
{
FeatureType feature;
switch (m_editor.GetFeatureStatus(mwmID, index))
{
case osm::Editor::EDeleted: return;
case osm::Editor::EModified:
VERIFY(m_editor.GetEditedFeature(mwmID, index, feature), ());
m_f(feature);
return;
case osm::Editor::ECreated: CHECK(false, ("Created features index should be generated."));
case osm::Editor::EUntouched: break;
}
if (checkUnique(index))
{
fv.GetByIndex(index, feature);
feature.SetID(FeatureID(mwmID, index));
m_f(feature);
}
},
i.first, i.second, scale);
}
}
}
};
template <typename F> class ReadFeatureIndexFunctor
{
F & m_f;
osm::Editor & m_editor = osm::Editor::Instance();
public:
ReadFeatureIndexFunctor(F & f) : m_f(f) {}
/// Used by Editor to inject new features.
void operator()(FeatureID const & fid) const
{
m_f(fid);
}
void operator()(MwmHandle const & handle, covering::CoveringGetter & cov, uint32_t scale) const
{
MwmValue const * pValue = handle.GetValue<MwmValue>();
if (pValue)
{
feature::DataHeader const & header = pValue->GetHeader();
// Prepare needed covering.
int const lastScale = header.GetLastScale();
// In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
if (scale > lastScale)
scale = lastScale;
// Use last coding scale for covering (see index_builder.cpp).
covering::IntervalsT const & interval = cov.Get(lastScale);
ScaleIndex<ModelReaderPtr> const index(pValue->m_cont.GetReader(INDEX_FILE_TAG), pValue->m_factory);
// Iterate through intervals.
CheckUniqueIndexes checkUnique(header.GetFormat() >= version::Format::v5);
MwmId const & mwmID = handle.GetId();
for (auto const & i : interval)
{
index.ForEachInIntervalAndScale([&] (uint32_t index)
{
if (osm::Editor::EDeleted != m_editor.GetFeatureStatus(mwmID, index) && checkUnique(index))
m_f(FeatureID(mwmID, index));
}, i.first, i.second, scale);
}
}
}
};
public:
template <typename F>
void ForEachInRect(F & f, m2::RectD const & rect, uint32_t scale) const
{
ReadMWMFunctor<F> implFunctor(f);
ForEachInIntervals(implFunctor, covering::ViewportWithLowLevels, rect, scale);
}
template <typename F>
void ForEachFeatureIDInRect(F & f, m2::RectD const & rect, uint32_t scale) const
{
ReadFeatureIndexFunctor<F> implFunctor(f);
ForEachInIntervals(implFunctor, covering::LowLevelsOnly, rect, scale);
}
template <typename F>
void ForEachInScale(F & f, uint32_t scale) const
{
ReadMWMFunctor<F> implFunctor(f);
ForEachInIntervals(implFunctor, covering::FullCover, m2::RectD::GetInfiniteRect(), scale);
}
// "features" must be sorted using FeatureID::operator< as predicate.
template <typename F>
void ReadFeatures(F & f, vector<FeatureID> const & features) const
{
auto fidIter = features.begin();
auto const endIter = features.end();
auto & editor = osm::Editor::Instance();
while (fidIter != endIter)
{
MwmId const & id = fidIter->m_mwmId;
MwmHandle const handle = GetMwmHandleById(id);
if (handle.IsAlive())
{
MwmValue const * pValue = handle.GetValue<MwmValue>();
FeaturesVector const featureReader(pValue->m_cont, pValue->GetHeader(), pValue->m_table);
do
{
osm::Editor::FeatureStatus const fts = editor.GetFeatureStatus(id, fidIter->m_index);
ASSERT_NOT_EQUAL(osm::Editor::EDeleted, fts, ("Deleted feature was cached. Please review your code."));
FeatureType featureType;
if (fts == osm::Editor::EModified)
{
VERIFY(editor.GetEditedFeature(id, fidIter->m_index, featureType), ());
}
else
{
featureReader.GetByIndex(fidIter->m_index, featureType);
featureType.SetID(*fidIter);
}
f(featureType);
}
while (++fidIter != endIter && id == fidIter->m_mwmId);
}
else
{
// Skip unregistered mwm files.
while (++fidIter != endIter && id == fidIter->m_mwmId);
}
}
}
/// Guard for loading features from particular MWM by demand.
class FeaturesLoaderGuard
{
public:
FeaturesLoaderGuard(Index const & parent, MwmId id);
inline MwmSet::MwmId const & GetId() const { return m_handle.GetId(); }
string GetCountryFileName() const;
bool IsWorld() const;
void GetFeatureByIndex(uint32_t index, FeatureType & ft) const;
inline FeaturesVector const & GetFeaturesVector() const { return m_vector; }
private:
MwmHandle m_handle;
FeaturesVector m_vector;
osm::Editor & m_editor = osm::Editor::Instance();
};
template <typename F>
void ForEachInRectForMWM(F & f, m2::RectD const & rect, uint32_t scale, MwmId const & id) const
{
MwmHandle const handle = GetMwmHandleById(id);
if (handle.IsAlive())
{
covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels);
ReadMWMFunctor<F> fn(f);
fn(handle, cov, scale);
}
}
private:
template <typename F>
void ForEachInIntervals(F & f, covering::CoveringMode mode, m2::RectD const & rect,
uint32_t scale) const
{
vector<shared_ptr<MwmInfo>> mwms;
GetMwmsInfo(mwms);
covering::CoveringGetter cov(rect, mode);
MwmId worldID[2];
osm::Editor & editor = osm::Editor::Instance();
for (shared_ptr<MwmInfo> const & info : mwms)
{
if (info->m_minScale <= scale && scale <= info->m_maxScale &&
rect.IsIntersect(info->m_limitRect))
{
MwmId const id(info);
switch (info->GetType())
{
case MwmInfo::COUNTRY:
{
MwmHandle const handle = GetMwmHandleById(id);
f(handle, cov, scale);
// Check created features container.
// Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
editor.ForEachFeatureInMwmRectAndScale(id, f, rect, scale);
}
break;
case MwmInfo::COASTS:
worldID[0] = id;
break;
case MwmInfo::WORLD:
worldID[1] = id;
break;
}
}
}
if (worldID[0].IsAlive())
{
MwmHandle const handle = GetMwmHandleById(worldID[0]);
f(handle, cov, scale);
// Check edited/created features container.
// Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
editor.ForEachFeatureInMwmRectAndScale(worldID[0], f, rect, scale);
}
if (worldID[1].IsAlive())
{
MwmHandle const handle = GetMwmHandleById(worldID[1]);
f(handle, cov, scale);
// Check edited/created features container.
// Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
editor.ForEachFeatureInMwmRectAndScale(worldID[1], f, rect, scale);
}
}
my::ObserverList<Observer> m_observers;
};
|