Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_meta.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 443a0f4704c7baa360d4c046cd33ef70c00aeaf1 (plain)
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
#pragma once

#include "coding/multilang_utf8_string.hpp"
#include "coding/reader.hpp"

#include "std/algorithm.hpp"
#include "std/limits.hpp"
#include "std/map.hpp"
#include "std/string.hpp"
#include "std/vector.hpp"


namespace feature
{
class MetadataBase
{
protected:
  // TODO: Change uint8_t to appropriate type when FMD_COUNT reaches 256.
  void Set(uint8_t type, string const & value)
  {
    auto found = m_metadata.find(type);
    if (found == m_metadata.end())
    {
      if (!value.empty())
        m_metadata[type] = value;
    }
    else
    {
      if (value.empty())
        m_metadata.erase(found);
      else
        found->second = value;
    }
  }

public:
  bool Has(uint8_t type) const
  {
    auto const it = m_metadata.find(type);
    return it != m_metadata.end();
  }

  string Get(uint8_t type) const
  {
    auto const it = m_metadata.find(type);
    return (it == m_metadata.end()) ? string() : it->second;
  }

  vector<uint8_t> GetPresentTypes() const
  {
    vector<uint8_t> types;
    types.reserve(m_metadata.size());

    for (auto const & item : m_metadata)
      types.push_back(item.first);

    return types;
  }

  inline bool Empty() const { return m_metadata.empty(); }
  inline size_t Size() const { return m_metadata.size(); }

  template <class TSink>
  void Serialize(TSink & sink) const
  {
    auto const sz = static_cast<uint32_t>(m_metadata.size());
    WriteVarUint(sink, sz);
    for (auto const & it : m_metadata)
    {
      WriteVarUint(sink, static_cast<uint32_t>(it.first));
      utils::WriteString(sink, it.second);
    }
  }

  template <class TSource>
  void Deserialize(TSource & src)
  {
    auto const sz = ReadVarUint<uint32_t>(src);
    for (size_t i = 0; i < sz; ++i)
    {
      auto const key = ReadVarUint<uint32_t>(src);
      utils::ReadString(src, m_metadata[key]);
    }
  }

  inline bool Equals(MetadataBase const & other) const
  {
    return m_metadata == other.m_metadata;
  }

protected:
  map<uint8_t, string> m_metadata;
};

class Metadata : public MetadataBase
{
public:
  /// @note! Do not change values here.
  /// Add new types to the end of list, before FMD_COUNT.
  /// Please also modify MetadataTagProcessor::TypeFromString().
  enum EType : int8_t
  {
    FMD_CUISINE = 1,
    FMD_OPEN_HOURS = 2,
    FMD_PHONE_NUMBER = 3,
    FMD_FAX_NUMBER = 4,
    FMD_STARS = 5,
    FMD_OPERATOR = 6,
    FMD_URL = 7,
    FMD_WEBSITE = 8,
    FMD_INTERNET = 9,
    FMD_ELE = 10,
    FMD_TURN_LANES = 11,
    FMD_TURN_LANES_FORWARD = 12,
    FMD_TURN_LANES_BACKWARD = 13,
    FMD_EMAIL = 14,
    FMD_POSTCODE = 15,
    FMD_WIKIPEDIA = 16,
    FMD_MAXSPEED = 17,
    FMD_FLATS = 18,
    FMD_HEIGHT = 19,
    FMD_MIN_HEIGHT = 20,
    FMD_DENOMINATION = 21,
    FMD_BUILDING_LEVELS = 22,
    FMD_TEST_ID = 23,
    FMD_SPONSORED_ID = 24,
    FMD_PRICE_RATE = 25,
    FMD_RATING = 26,
    FMD_BANNER_URL = 27,
    FMD_LEVEL = 28,
    FMD_COUNT
  };

  /// Used to normalize tags like "contact:phone" and "phone" to a common metadata enum value.
  static bool TypeFromString(string const & osmTagKey, EType & outType);
  static bool IsSponsoredType(EType const & type);

  void Set(EType type, string const & value) { MetadataBase::Set(type, value); }
  void Drop(EType type) { Set(type, string()); }
  string GetWikiURL() const;

  // TODO: Commented code below is now longer neded, but I leave it here
  // as a hint to what is going on in DeserializeFromMWMv7OrLower.
  // Please, remove it when DeserializeFromMWMv7OrLower is no longer neded.
  // template <class TWriter>
  // void SerializeToMWM(TWriter & writer) const
  // {
  //   for (auto const & e : m_metadata)
  //   {
  //     // Set high bit if it's the last element.
  //     uint8_t const mark = (&e == &(*m_metadata.crbegin()) ? 0x80 : 0);
  //     uint8_t elem[2] = {static_cast<uint8_t>(e.first | mark),
  //                        static_cast<uint8_t>(min(e.second.size(), (size_t)kMaxStringLength))};
  //     writer.Write(elem, sizeof(elem));
  //     writer.Write(e.second.data(), elem[1]);
  //   }
  // }

  template <class TSource>
  void DeserializeFromMWMv7OrLower(TSource & src)
  {
    uint8_t header[2] = {0};
    char buffer[kMaxStringLength] = {0};
    do
    {
      src.Read(header, sizeof(header));
      src.Read(buffer, header[1]);
      m_metadata[header[0] & 0x7F].assign(buffer, header[1]);
    } while (!(header[0] & 0x80));
  }

private:
  enum { kMaxStringLength = 255 };
};

class AddressData : public MetadataBase
{
public:
  enum Type { PLACE, STREET, POSTCODE };

  void Add(Type type, string const & s)
  {
    /// @todo Probably, we need to add separator here and store multiple values.
    MetadataBase::Set(type, s);
  }
};

class RegionData : public MetadataBase
{
public:
  enum Type : int8_t
  {
    RD_LANGUAGES,        // list of written languages
    RD_DRIVING,          // left- or right-hand driving (letter 'l' or 'r')
    RD_TIMEZONE,         // UTC timezone offset, floating signed number of hours: -3, 4.5
    RD_ADDRESS_FORMAT,   // address format, re: mapzen
    RD_PHONE_FORMAT,     // list of strings in "+N NNN NN-NN-NN" format
    RD_POSTCODE_FORMAT,  // list of strings in "AAA ANN" format
    RD_PUBLIC_HOLIDAYS,  // fixed PH dates
    RD_ALLOW_HOUSENAMES  // 'y' if housenames are commonly used
  };

  // Special values for month references in public holiday definitions.
  enum PHReference : int8_t
  {
    PH_EASTER = 20,
    PH_ORTHODOX_EASTER = 21,
    PH_VICTORIA_DAY = 22,
    PH_CANADA_DAY = 23
  };

  void Set(Type type, string const & s)
  {
    CHECK_NOT_EQUAL(type, Type::RD_LANGUAGES, ("Please use RegionData::SetLanguages method"));
    MetadataBase::Set(type, s);
  }

  void SetLanguages(vector<string> const & codes);
  void GetLanguages(vector<int8_t> & langs) const;
  bool HasLanguage(int8_t const lang) const;
  bool IsSingleLanguage(int8_t const lang) const;

  void AddPublicHoliday(int8_t month, int8_t offset);
  // No public holidays getters until we know what to do with these.
};
}  // namespace feature

// Prints types in osm-friendly format.
string DebugPrint(feature::Metadata::EType type);