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

serdes.cpp « binary « ugc - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdecacaff897345c9e81f0868338a96c4b146ee7 (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
#include "ugc/binary/serdes.hpp"

#include <set>

using namespace std;

namespace ugc
{
namespace binary
{
namespace
{
class BaseCollector
{
public:
  virtual ~BaseCollector() = default;

  virtual void VisitRating(float const f, char const * /* name */ = nullptr) {}
  virtual void operator()(string const & /* s */, char const * /* name */ = nullptr) {}
  virtual void operator()(Sentiment const /* sentiment */, char const * /* name */ = nullptr) {}
  virtual void operator()(Time const /* time */, char const * /* name */ = nullptr) {}
  virtual void operator()(TranslationKey const & tk, char const * /* name */ = nullptr) {}
  virtual void operator()(Text const & text, char const * /* name */ = nullptr) {}

  template <typename T>
  void operator()(vector<T> const & vs, char const * /* name */ = nullptr)
  {
    for (auto const & v : vs)
      (*this)(v);
  }

  template <typename R>
  typename enable_if<is_fundamental<R>::value>::type operator()(R const & r,
                                                                char const * /* name */ = nullptr)
  {
  }

  template <typename R>
  typename enable_if<!is_fundamental<R>::value>::type operator()(R const & r,
                                                                 char const * /* name */ = nullptr)
  {
    r.Visit(*this);
  }
};

// Collects all translation keys from UGC.
class TranslationKeyCollector : public BaseCollector
{
public:
  using BaseCollector::operator();

  explicit TranslationKeyCollector(set<TranslationKey> & keys) : m_keys(keys) {}

  void operator()(TranslationKey const & tk, char const * /* name */ = nullptr) override
  {
    m_keys.insert(tk.m_key);
  }

private:
  set<TranslationKey> & m_keys;
};

// Collects all texts from UGC.
class TextCollector : public BaseCollector
{
public:
  using BaseCollector::operator();

  TextCollector(vector<Text> & texts) : m_texts(texts) {}

  void operator()(Text const & text, char const * /* name */ = nullptr) override
  {
    m_texts.push_back(text);
  }

private:
  vector<Text> & m_texts;
};
}  // namespace

void UGCSeriaizer::CollectTranslationKeys()
{
  ASSERT(m_keys.empty(), ());

  set<TranslationKey> keys;
  TranslationKeyCollector collector(keys);
  for (auto const & p : m_ugcs)
    collector(p.m_ugc);
  m_keys.assign(keys.begin(), keys.end());
}

void UGCSeriaizer::CollectTexts()
{
  ASSERT(m_texts.empty(), ());
  for (auto const & p : m_ugcs)
  {
    m_texts.emplace_back();
    TextCollector collector(m_texts.back());
    collector(p.m_ugc);
  }
}
}  // namespace binary
}  // namespace ugc