inicpp
C++ parser of INI files with schema validation.
config.h
1 #ifndef INICPP_CONFIG_H
2 #define INICPP_CONFIG_H
3 
4 #include <iostream>
5 #include <map>
6 #include <vector>
7 
8 #include "dll.h"
9 #include "exception.h"
10 #include "option.h"
11 #include "schema.h"
12 #include "section.h"
13 
14 
15 namespace inicpp
16 {
18  class schema;
20  template <typename Element> class config_iterator;
21 
22 
28  class INICPP_API config
29  {
30  private:
31  using sections_vector = std::vector<std::shared_ptr<section>>;
32  using sections_map = std::map<std::string, std::shared_ptr<section>>;
33  using sections_map_pair = std::pair<std::string, std::shared_ptr<section>>;
34 
36  sections_vector sections_;
38  sections_map sections_map_;
39 
40  friend class config_iterator<section>;
41  friend class config_iterator<const section>;
42 
43  public:
48 
52  config();
56  config(const config &source);
60  config &operator=(const config &source);
64  config(config &&source);
68  config &operator=(config &&source);
69 
75  void add_section(const section &sect);
81  void add_section(const std::string &section_name);
87  void remove_section(const std::string &section_name);
88 
96  void add_option(const std::string &section_name, const option &opt);
104  template <typename ValueType>
105  void add_option(const std::string &section_name, const std::string &option_name, ValueType value)
106  {
107  auto sect_it = sections_map_.find(section_name);
108  if (sect_it != sections_map_.end()) {
109  option opt(option_name);
110  opt.set<ValueType>(value);
111  sect_it->second->add_option(opt);
112  } else {
113  throw not_found_exception(section_name);
114  }
115  }
116 
125  void remove_option(const std::string &section_name, const std::string &option_name);
126 
131  size_t size() const;
138  section &operator[](size_t index);
145  const section &operator[](size_t index) const;
152  section &operator[](const std::string &section_name);
159  const section &operator[](const std::string &section_name) const;
165  bool contains(const std::string &section_name) const;
166 
173  void validate(const schema &schm, schema_mode mode);
174 
180  bool operator==(const config &other) const;
186  bool operator!=(const config &other) const;
187 
192  iterator begin();
197  iterator end();
202  const_iterator begin() const;
207  const_iterator end() const;
212  const_iterator cbegin() const;
217  const_iterator cend() const;
218 
225  INICPP_API friend std::ostream &operator<<(std::ostream &os, const config &conf);
226  };
227 
228  INICPP_API std::ostream &operator<<(std::ostream &os, const config &conf);
229 
230 
236  template <typename Element> class config_iterator : public std::iterator<std::random_access_iterator_tag, Element>
237  {
238  private:
240  config &container_;
242  size_t position_;
243 
244  public:
245  using typename std::iterator<std::random_access_iterator_tag, Element>::reference;
246  using typename std::iterator<std::random_access_iterator_tag, Element>::pointer;
247 
251  config_iterator() = delete;
255  config_iterator(const config_iterator &src) = default;
259  config_iterator &operator=(const config_iterator &source) = default;
263  config_iterator(config_iterator &&src) = default;
267  config_iterator &operator=(config_iterator &&source) = default;
268 
275  config_iterator(config &source, size_t position) : container_(source), position_(position)
276  {
277  }
282  config_iterator(config &source) : config_iterator(source, 0)
283  {
284  }
285 
291  {
292  ++position_;
293  return *this;
294  }
300  {
301  config_iterator old(*this);
302  operator++();
303  return old;
304  }
305 
311  bool operator==(const config_iterator &second) const
312  {
313  return &container_ == &second.container_ && position_ == second.position_;
314  }
320  bool operator!=(const config_iterator &second) const
321  {
322  return !(*this == second);
323  }
329  bool operator<(const config_iterator &second) const
330  {
331  return position_ < second.position_;
332  }
333 
338  reference operator*()
339  {
340  return *container_.sections_.at(position_);
341  }
342 
347  pointer operator->()
348  {
349  return &(operator*());
350  }
351  };
352 }
353 
354 #endif
config_iterator(config &source)
Definition: config.h:282
config_iterator & operator++()
Definition: config.h:290
bool operator!=(const config_iterator &second) const
Definition: config.h:320
reference operator*()
Definition: config.h:338
void add_option(const std::string &section_name, const std::string &option_name, ValueType value)
Definition: config.h:105
Definition: config.h:15
bool operator==(const config_iterator &second) const
Definition: config.h:311
bool operator<(const config_iterator &second) const
Definition: config.h:329
config_iterator operator++(int)
Definition: config.h:299
pointer operator->()
Definition: config.h:347
config_iterator(config &source, size_t position)
Definition: config.h:275
void set(ValueType value)
Definition: option.h:261