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: 3a082f32ebac7514f7604870bf4654503efa6e80 (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
#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 Metadata
  {
  public:
    /// @note! Do not change values here.
    /// Add new types to the end of list, before FMD_COUNT.
    enum EType
    {
      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_COUNT
    };

    static_assert(FMD_COUNT <= 255, "Meta types count is limited to one byte.");

    /// Empty value drops (clears) corresponding type.
    void Set(EType 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;
      }
    }

    /// Synonym of Set(type, "").
    void Drop(EType type)
    {
      Set(type, "");
    }

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

    vector<EType> GetPresentTypes() const
    {
      vector<EType> 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(); }

    string GetWikiURL() const;

    template <class ArchiveT> void SerializeToMWM(ArchiveT & ar) 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))};
        ar.Write(elem, sizeof(elem));
        ar.Write(e.second.data(), elem[1]);
      }
    }

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

    template <class ArchiveT> void Serialize(ArchiveT & ar) const
    {
      uint8_t const sz = m_metadata.size();
      WriteToSink(ar, sz);
      for (auto const & it : m_metadata)
      {
        WriteToSink(ar, static_cast<uint8_t>(it.first));
        utils::WriteString(ar, it.second);
      }
    }

    template <class ArchiveT> void Deserialize(ArchiveT & ar)
    {
      uint8_t const sz = ReadPrimitiveFromSource<uint8_t>(ar);
      ASSERT_LESS_OR_EQUAL(sz, FMD_COUNT, ());

      for (size_t i = 0; i < sz; ++i)
      {
        EType const key = ToType(ReadPrimitiveFromSource<uint8_t>(ar));
        string value;
        utils::ReadString(ar, value);
        m_metadata.insert(make_pair(key, value));
      }
    }

  private:
    static EType ToType(uint8_t key)
    {
      ASSERT(key > 0 && key < FMD_COUNT, (key));
      return static_cast<EType>(key);
    }

    enum { kMaxStringLength = 255 };
    map<EType, string> m_metadata;
  };
}