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
|
#pragma once
#include "indexer/cell_id.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_covering.hpp"
#include "indexer/features_offsets_table.hpp"
#include "indexer/feature_source.hpp"
#include "indexer/features_vector.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/scale_index.hpp"
#include "indexer/unique_index.hpp"
#include "coding/file_container.hpp"
#include "base/macros.hpp"
#include <cstdint>
#include <functional>
#include <memory>
#include <utility>
#include <vector>
#include "defines.hpp"
class DataSource : public MwmSet
{
public:
using FeatureCallback = std::function<void(FeatureType &)>;
using FeatureIdCallback = std::function<void(FeatureID const &)>;
using StopSearchCallback = std::function<bool(void)>;
~DataSource() override = default;
/// Registers a new map.
std::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);
void ForEachFeatureIDInRect(FeatureIdCallback const & f, m2::RectD const & rect, int scale) const;
void ForEachInRect(FeatureCallback const & f, m2::RectD const & rect, int scale) const;
// Calls |f| for features closest to |center| until |stopCallback| returns true or distance
// |sizeM| from has been reached. Then for EditableDataSource calls |f| for each edited feature
// inside square with center |center| and side |2 * sizeM|. Edited features are not in the same
// hierarchy and there is no fast way to merge frozen and edited features.
void ForClosestToPoint(FeatureCallback const & f, StopSearchCallback const & stopCallback,
m2::PointD const & center, double sizeM, int scale) const;
void ForEachInScale(FeatureCallback const & f, int scale) const;
void ForEachInRectForMWM(FeatureCallback const & f, m2::RectD const & rect, int scale,
MwmId const & id) const;
// "features" must be sorted using FeatureID::operator< as predicate.
void ReadFeatures(FeatureCallback const & fn, std::vector<FeatureID> const & features) const;
void ReadFeature(FeatureCallback const & fn, FeatureID const & feature) const
{
return ReadFeatures(fn, {feature});
}
protected:
using ReaderCallback = std::function<void(MwmSet::MwmHandle const & handle,
covering::CoveringGetter & cov, int scale)>;
explicit DataSource(std::unique_ptr<FeatureSourceFactory> factory) : m_factory(std::move(factory)) {}
void ForEachInIntervals(ReaderCallback const & fn, covering::CoveringMode mode,
m2::RectD const & rect, int scale) const;
/// MwmSet overrides:
std::unique_ptr<MwmInfo> CreateInfo(platform::LocalCountryFile const & localFile) const override;
std::unique_ptr<MwmValueBase> CreateValue(MwmInfo & info) const override;
private:
friend class FeaturesLoaderGuard;
std::unique_ptr<FeatureSourceFactory> m_factory;
};
// DataSource which operates with features from mwm file and does not support features creation
// deletion or modification.
class FrozenDataSource : public DataSource
{
public:
FrozenDataSource() : DataSource(std::make_unique<FeatureSourceFactory>()) {}
};
/// Guard for loading features from particular MWM by demand.
/// @note This guard is suitable when mwm is loaded.
class FeaturesLoaderGuard
{
public:
FeaturesLoaderGuard(DataSource const & dataSource, DataSource::MwmId const & id)
: m_handle(dataSource.GetMwmHandleById(id)), m_source((*dataSource.m_factory)(m_handle))
{
}
MwmSet::MwmId const & GetId() const { return m_handle.GetId(); }
std::string GetCountryFileName() const;
bool IsWorld() const;
std::unique_ptr<FeatureType> GetOriginalFeatureByIndex(uint32_t index) const;
std::unique_ptr<FeatureType> GetOriginalOrEditedFeatureByIndex(uint32_t index) const;
/// Everyone, except Editor core, should use this method.
WARN_UNUSED_RESULT bool GetFeatureByIndex(uint32_t index, FeatureType & ft) const;
/// Editor core only method, to get 'untouched', original version of feature.
WARN_UNUSED_RESULT bool GetOriginalFeatureByIndex(uint32_t index, FeatureType & ft) const;
size_t GetNumFeatures() const { return m_source->GetNumFeatures(); }
private:
DataSource::MwmHandle m_handle;
std::unique_ptr<FeatureSource> m_source;
};
|