inicpp
C++ parser of INI files with schema validation.
section.cpp
1 #include "section.h"
2 
3 namespace inicpp
4 {
5  section::section(const section &source) : name_(source.name_)
6  {
7  // we have to do deep copies of options
8  options_.reserve(source.options_.size());
9  for (auto &opt : source.options_) {
10  options_.push_back(std::make_shared<option>(*opt));
11  }
12 
13  // we already have constructed options... now push them into map
14  for (auto &opt : options_) {
15  options_map_.insert(options_map_pair(opt->get_name(), opt));
16  }
17  }
18 
20  {
21  if (this != &source) {
22  // make copy of input source section and swap it with this
23  section new_src(source);
24  std::swap(*this, new_src);
25  }
26 
27  return *this;
28  }
29 
31  {
32  operator=(std::move(source));
33  }
34 
36  {
37  if (this != &source) {
38  options_ = std::move(source.options_);
39  options_map_ = std::move(source.options_map_);
40  name_ = std::move(source.name_);
41  }
42  return *this;
43  }
44 
45  section::section(const std::string &name) : name_(name)
46  {
47  }
48 
49  const std::string &section::get_name() const
50  {
51  return name_;
52  }
53 
54  void section::add_option(const option &opt)
55  {
56  auto add_it = options_map_.find(opt.get_name());
57  if (add_it == options_map_.end()) {
58  std::shared_ptr<option> add = std::make_shared<option>(opt);
59  options_.push_back(add);
60  options_map_.insert(options_map_pair(add->get_name(), add));
61  } else {
62  throw ambiguity_exception(opt.get_name());
63  }
64  }
65 
66  void section::remove_option(const std::string &option_name)
67  {
68  auto del_it = options_map_.find(option_name);
69  if (del_it != options_map_.end()) {
70  // remove from map
71  options_map_.erase(del_it);
72  // remove from vector
73  options_.erase(
74  std::remove_if(options_.begin(),
75  options_.end(),
76  [&](std::shared_ptr<option> opt) { return (opt->get_name() == option_name ? true : false); }),
77  options_.end());
78  } else {
79  throw not_found_exception(option_name);
80  }
81  }
82 
83  size_t section::size() const
84  {
85  return options_.size();
86  }
87 
88  option &section::operator[](size_t index)
89  {
90  if (index >= size()) {
91  throw not_found_exception(index);
92  }
93 
94  return *options_[index];
95  }
96 
97  const option &section::operator[](size_t index) const
98  {
99  if (index >= size()) {
100  throw not_found_exception(index);
101  }
102 
103  return *options_[index];
104  }
105 
106  option &section::operator[](const std::string &option_name)
107  {
108  std::shared_ptr<option> result;
109  try {
110  result = options_map_.at(option_name);
111  } catch (std::out_of_range) {
112  throw not_found_exception(option_name);
113  }
114  return *result;
115  }
116 
117  const option &section::operator[](const std::string &option_name) const
118  {
119  std::shared_ptr<option> result;
120  try {
121  result = options_map_.at(option_name);
122  } catch (std::out_of_range) {
123  throw not_found_exception(option_name);
124  }
125  return *result;
126  }
127 
128  bool section::contains(const std::string &option_name) const
129  {
130  try {
131  options_map_.at(option_name);
132  return true;
133  } catch (std::out_of_range) {
134  return false;
135  }
136  }
137 
138  void section::validate(const section_schema &sect_schema, schema_mode mode)
139  {
140  sect_schema.validate_section(*this, mode);
141  }
142 
143  bool section::operator==(const section &other) const
144  {
145  if (name_ != other.name_) {
146  return false;
147  }
148 
149  return std::equal(options_.begin(),
150  options_.end(),
151  other.options_.begin(),
152  [](const std::shared_ptr<option> &first, const std::shared_ptr<option> &second) {
153  return *first == *second;
154  });
155  }
156 
157  bool section::operator!=(const section &other) const
158  {
159  return !(*this == other);
160  }
161 
163  {
164  return iterator(*this);
165  }
166 
168  {
169  return iterator(*this, options_.size());
170  }
171 
173  {
174  return const_iterator(const_cast<section &>(*this));
175  }
176 
178  {
179  return const_iterator(const_cast<section &>(*this), options_.size());
180  }
181 
183  {
184  return const_iterator(const_cast<section &>(*this));
185  }
186 
188  {
189  return const_iterator(const_cast<section &>(*this), options_.size());
190  }
191 
192  std::ostream &operator<<(std::ostream &os, const section &sect)
193  {
194  os << "[" << sect.get_name() << "]" << std::endl;
195  for (auto &opt : sect.options_) {
196  os << *opt;
197  }
198 
199  return os;
200  }
201 }
bool contains(const std::string &option_name) const
Definition: section.cpp:128
void validate(const section_schema &sect_schema, schema_mode mode)
Definition: section.cpp:138
void remove_option(const std::string &option_name)
Definition: section.cpp:66
size_t size() const
Definition: section.cpp:83
section & operator=(const section &source)
Definition: section.cpp:19
section()=delete
Definition: config.h:15
section_iterator< option > iterator
Definition: section.h:46
const std::string & get_name() const
Definition: option.cpp:67
bool operator!=(const section &other) const
Definition: section.cpp:157
void add_option(const std::string &option_name, ValueType value)
Definition: section.h:89
iterator begin()
Definition: section.cpp:162
void validate_section(section &sect, schema_mode mode) const
bool operator==(const section &other) const
Definition: section.cpp:143
option & operator[](size_t index)
Definition: section.cpp:88
const_iterator cend() const
Definition: section.cpp:187
INICPP_API friend std::ostream & operator<<(std::ostream &os, const section &sect)
Definition: section.cpp:192
const_iterator cbegin() const
Definition: section.cpp:182
section_iterator< const option > const_iterator
Definition: section.h:48
iterator end()
Definition: section.cpp:167
const std::string & get_name() const
Definition: section.cpp:49