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

place_node.hpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e3f6a101c6a4e63634f2f35caac9aa9c262a95c2 (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
#pragma once

#include <memory>
#include <utility>
#include <vector>

namespace generator
{
// This is a tree node, where each node can have many children. Now it is used to build a hierarchy
// of places.
template <typename Data>
class PlaceNode
{
public:
  using Ptr = std::shared_ptr<PlaceNode>;
  using WeakPtr = std::weak_ptr<PlaceNode>;
  using PtrList = std::vector<Ptr>;

  explicit PlaceNode(Data && data) : m_data(std::move(data)) {}

  void AddChild(Ptr child) { m_children.push_back(child); }
  void SetParent(Ptr parent) { m_parent = parent; }
  void SetChildren(PtrList && children) { m_children = std::move(children); }
  void RemoveChildren() { m_children.clear(); }

  bool HasChildren() const { return !m_children.empty(); }
  bool HasParent() const { return m_parent.lock() != nullptr; }

  PtrList const & GetChildren() const { return m_children; }
  PtrList & GetChildren() { return m_children; }
  Ptr GetParent() const { return m_parent.lock(); }
  Data & GetData() { return m_data; }
  Data const & GetData() const { return m_data; }

  void ShrinkToFitChildren()
  {
    if (m_children.capacity() != m_children.size())
      PtrList(m_children).swap(m_children);
  }

private:
  Data m_data;
  PtrList m_children;
  WeakPtr m_parent;
};

template <typename Data, typename Visitor>
void Visit(std::shared_ptr<PlaceNode<Data>> const & tree, Visitor && visitor)
{
  visitor(tree);
  for (auto const & subtree : tree->GetChildren())
    Visit(subtree, visitor);
}
}  // namespace generator