Skip to content

YAMLNode.hpp

  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
#pragma once

#include "doxide.hpp"

/**
 * Node in a YAML document.
 * 
 * @ingroup developer
 */
class YAMLNode {
public:
  using key_type = std::string;
  using value_type = std::string;
  using sequence_type = std::list<std::shared_ptr<YAMLNode>>;
  using mapping_type = std::unordered_map<key_type,std::shared_ptr<YAMLNode>>;
  using variant_type = std::variant<value_type,sequence_type,mapping_type>;

  /**
   * Does this have a given key?
   */
  bool has(const key_type& key) const;

  /**
   * Is this a value?
   */
  bool isValue() const;

  /**
   * Is there a value at a given path?
   * 
   * @param key Key.
   * @param keys... Keys.
   */
  template<class... Args>
  bool isValue(const key_type& key, Args&&... keys) const {
    return has(key) &&
        mapping().at(key)->isValue(std::forward<Args>(keys)...);
  }

  /**
   * Is this a sequence?
   */
  bool isSequence() const;

  /**
   * Is there a sequence at a given path?
   * 
   * @param key Key.
   * @param keys... Keys.
   */
  template<class... Args>
  bool isSequence(const key_type& key, Args&&... keys) const {
    return has(key) &&
        mapping().at(key)->isSequence(std::forward<Args>(keys)...);
  }

  /**
   * Is this a mapping?
   */
  bool isMapping() const;

  /**
   * Is there a mapping at a given path?
   * 
   * @param key Key.
   * @param keys... Keys.
   */
  template<class... Args>
  bool isMapping(const key_type& key, Args&&... keys) const {
    return has(key) &&
        mapping().at(key)->isMapping(std::forward<Args>(keys)...);
  }

  /**
   * Get this as a value.
   */
  value_type& value();

  /**
   * Get this as a value.
   */
  const value_type& value() const;

  /**
   * Get the value at a given path.
   * 
   * @param key Key.
   * @param keys... Keys.
   * 
   * @return The value.
   */
  template<class... Args>
  value_type& value(const key_type& key, Args&&... keys) {
    return mapping().at(key)->value(std::forward<Args>(keys)...);
  }

  /**
   * Get the value at a given path.
   * 
   * @param key Key.
   * @param keys... Keys.
   * 
   * @return The value.
   */
  template<class... Args>
  const value_type& value(const key_type& key, Args&&... keys) const {
    return mapping().at(key)->value(std::forward<Args>(keys)...);
  }

  /**
   * Get this as a sequence.
   */
  sequence_type& sequence();

  /**
   * Get this as a sequence.
   */
  const sequence_type& sequence() const;

  /**
   * Get the sequence at a given path.
   * 
   * @param key Key.
   * @param keys... Keys.
   * 
   * @return The sequence.
   */
  template<class... Args>
  sequence_type& sequence(const key_type& key, Args&&... keys) {
    return mapping().at(key)->sequence(std::forward<Args>(keys)...);
  }

  /**
   * Get the sequence at a given path.
   * 
   * @param key Key.
   * @param keys... Keys.
   * 
   * @return The sequence.
   */
  template<class... Args>
  const sequence_type& sequence(const key_type& key, Args&&... keys) const {
    return mapping().at(key)->sequence(std::forward<Args>(keys)...);
  }

  /**
   * Get this as a mapping.
   */
  mapping_type& mapping();

  /**
   * Get this as a mapping.
   */
  const mapping_type& mapping() const;

  /**
   * Get the mapping at a given path.
   * 
   * @param key Key.
   * @param keys... Keys.
   * 
   * @return The mapping.
   */
  template<class... Args>
  mapping_type& mapping(const key_type& key, Args&&... keys) {
    return mapping().at(key)->mapping(std::forward<Args>(keys)...);
  }

  /**
   * Get the mapping at a given path.
   * 
   * @param key Key.
   * @param keys... Keys.
   * 
   * @return The mapping.
   */
  template<class... Args>
  const mapping_type& mapping(const key_type& key, Args&&... keys) const {
    return mapping().at(key)->mapping(std::forward<Args>(keys)...);
  }

  /**
   * Set the value.
   */
  void set(const variant_type& contents);

  /**
   * Set as a sequence.
   */
  void setSequence();

  /**
   * Set as a mapping.
   */
  void setMapping();

  /**
   * For a sequence, push an uninitialized node onto the end and return it.
   */
  YAMLNode& push();

  /**
   * For a mapping, insert a key with an uninitialized value and return the
   * value.
   */
  YAMLNode& insert(const key_type& key);

private:
  /**
   * Contents.
   */
  variant_type contents;
};