inicpp
C++ parser of INI files with schema validation.
schema.h
1 #ifndef INICPP_SCHEMA_H
2 #define INICPP_SCHEMA_H
3 
4 #include <iostream>
5 #include <vector>
6 
7 #include "config.h"
8 #include "dll.h"
9 #include "exception.h"
10 #include "option_schema.h"
11 #include "section_schema.h"
12 
13 
14 namespace inicpp
15 {
17  class config;
18 
24  class INICPP_API schema
25  {
26  private:
27  using sect_schema_vector = std::vector<std::shared_ptr<section_schema>>;
28  using sect_schema_map = std::map<std::string, std::shared_ptr<section_schema>>;
29  using sect_schema_map_pair = std::pair<std::string, std::shared_ptr<section_schema>>;
30 
32  sect_schema_vector sections_;
34  sect_schema_map sections_map_;
35 
36  public:
40  schema();
44  schema(const schema &source);
48  schema &operator=(const schema &source);
52  schema(schema &&source);
56  schema &operator=(schema &&source);
57 
63  void add_section(const section_schema &sect_schema);
70  void add_section(const section_schema_params &arguments);
71 
79  void add_option(const std::string &section_name, const option_schema &opt_schema);
88  template <typename ArgType>
89  void add_option(const std::string &section_name, option_schema_params<ArgType> &arguments)
90  {
91  auto sect_it = sections_map_.find(section_name);
92  if (sect_it != sections_map_.end()) {
93  option_schema opt_schema(arguments);
94  sect_it->second->add_option(opt_schema);
95  } else {
96  throw not_found_exception(section_name);
97  }
98  }
99 
104  size_t size() const;
111  section_schema &operator[](size_t index);
118  const section_schema &operator[](size_t index) const;
125  section_schema &operator[](const std::string &section_name);
132  const section_schema &operator[](const std::string &section_name) const;
138  bool contains(const std::string &section_name) const;
139 
146  void validate_config(config &cfg, schema_mode mode) const;
147 
154  INICPP_API friend std::ostream &operator<<(std::ostream &os, const schema &schm);
155  };
156 
157  INICPP_API std::ostream &operator<<(std::ostream &os, const schema &schm);
158 }
159 
160 #endif
Definition: config.h:15
void add_option(const std::string &section_name, option_schema_params< ArgType > &arguments)
Definition: schema.h:89