Medial Code Documentation
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
1
6#ifndef DMLC_CONFIG_H_
7#define DMLC_CONFIG_H_
8
9#include <cstring>
10#include <iostream>
11#include <iterator>
12#include <map>
13#include <vector>
14#include <utility>
15#include <string>
16#include <sstream>
17
19namespace dmlc {
20
40class Config {
41 public:
45 typedef std::pair<std::string, std::string> ConfigEntry;
46
50 class ConfigIterator;
51
56 explicit Config(bool multi_value = false);
62 explicit Config(std::istream& is, bool multi_value = false); // NOLINT(*)
66 void Clear(void);
71 void LoadFromStream(std::istream& is); // NOLINT(*)
80 template<class T>
81 void SetParam(const std::string& key, const T& value, bool is_string = false);
82
89 const std::string& GetParam(const std::string& key) const;
90
96 bool IsGenuineString(const std::string& key) const;
97
102 std::string ToProtoString(void) const;
103
108 ConfigIterator begin() const;
109
114 ConfigIterator end() const;
115
116 public:
121 friend class Config;
122 public:
123 using iterator_category = std::input_iterator_tag;
124 using value_type = ConfigEntry;
125 using difference_type = std::ptrdiff_t;
126 using pointer = ConfigEntry*;
127 using reference = ConfigEntry&;
131 ConfigIterator(const ConfigIterator& other);
141 ConfigIterator operator++(int); // NOLINT(*)
147 bool operator == (const ConfigIterator& rhs) const;
153 bool operator != (const ConfigIterator& rhs) const;
157 ConfigEntry operator * () const;
158
159 private:
160 ConfigIterator(size_t index, const Config* config);
161 void FindNextIndex();
162
163 private:
164 size_t index_;
165 const Config* config_;
166 };
167
168 private:
169 struct ConfigValue {
170 std::vector<std::string> val;
171 std::vector<size_t> insert_index;
172 bool is_string;
173 };
174 void Insert(const std::string& key, const std::string& value, bool is_string);
175
176 private:
177 std::map<std::string, ConfigValue> config_map_;
178 std::vector<std::pair<std::string, size_t> > order_;
179 const bool multi_value_;
180};
181
182template<class T>
183void Config::SetParam(const std::string& key, const T& value, bool is_string) {
184 std::ostringstream oss;
185 oss << value;
186 Insert(key, oss.str(), is_string);
187}
188
189} // namespace dmlc
190
191#endif // DMLC_CONFIG_H_
iterator class
Definition config.h:120
ConfigIterator & operator++()
uni-increment operators
Definition config.cc:236
ConfigEntry operator*() const
retrieve value from operator
Definition config.cc:258
bool operator!=(const ConfigIterator &rhs) const
compare operators not equal
Definition config.cc:254
bool operator==(const ConfigIterator &rhs) const
compare operators
Definition config.cc:250
class for config parser
Definition config.h:40
ConfigIterator begin() const
get begin iterator
Definition config.cc:203
void Clear(void)
clear all the values
Definition config.cc:138
bool IsGenuineString(const std::string &key) const
check whether the configure value given by the key should be wrapped by quotes
Definition config.cc:172
Config(std::istream &is, bool multi_value=false)
create config and load content from the given stream
ConfigIterator end() const
get end iterator
Definition config.cc:207
void SetParam(const std::string &key, const T &value, bool is_string=false)
set a key-value pair into the config; if the key already exists in the configure file,...
Definition config.h:183
const std::string & GetParam(const std::string &key) const
get the config under the key; if multiple values exist for the same key, return the last inserted one...
Definition config.cc:165
std::string ToProtoString(void) const
transform all the configuration into string recognizable to protobuf
Definition config.cc:191
void LoadFromStream(std::istream &is)
load the contents from the stream
Definition config.cc:143
std::pair< std::string, std::string > ConfigEntry
type when extracting from iterator
Definition config.h:45
namespace for dmlc
Definition array_view.h:12