aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/PropertyTree.hpp
blob: 613bcc478656bdbe89b83610889c89ccc4b404de (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
#pragma once

#include "Base.hpp"
#include "String.hpp"

#include <unordered_map>

namespace cru {
class PropertyTree;

class CRU_BASE_API PropertySubTreeRef {
 public:
  static String CombineKey(StringView left, StringView right);

  explicit PropertySubTreeRef(PropertyTree* tree, String path = {});

  CRU_DEFAULT_COPY(PropertySubTreeRef);
  CRU_DEFAULT_MOVE(PropertySubTreeRef);

  CRU_DEFAULT_DESTRUCTOR(PropertySubTreeRef);

 public:
  PropertyTree* GetTree() const { return tree_; }

  String GetPath() const { return path_; }
  void SetPath(String path) { path_ = std::move(path); }

  PropertySubTreeRef GetParent() const;
  PropertySubTreeRef GetChild(const String& key) const;

  String GetValue(const String& key) const;
  void SetValue(const String& key, String value);
  void DeleteValue(const String& key);

 private:
  PropertyTree* tree_;
  String path_;
};

class CRU_BASE_API PropertyTree {
 public:
  static String CombineKey(StringView left, StringView right);

  PropertyTree() = default;
  explicit PropertyTree(std::unordered_map<String, String> values);

  CRU_DELETE_COPY(PropertyTree);
  CRU_DELETE_MOVE(PropertyTree);

  CRU_DEFAULT_DESTRUCTOR(PropertyTree);

 public:
  String GetValue(const String& key) const;
  void SetValue(const String& key, String value);
  void DeleteValue(const String& key);

  PropertySubTreeRef GetSubTreeRef(const String& path);

 private:
  std::unordered_map<String, String> values_;
};

}  // namespace cru