Debian Codegraph @

src/dmlc-core/include/dmlc/config.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file config.h
4 * \brief defines config parser class
5 */
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 <sstream>
14 #include <string>
15 #include <utility>
16 #include <vector>
17
18 /*! \brief namespace for dmlc */
19 namespace dmlc {
20
21 /*!
22 * \brief class for config parser
23 *
24 * Two modes are supported:
25 * 1. non-multi value mode: if two same keys in the configure file, the later one will replace the
26 * ealier one; when using iterator, the order will be the "last effective insersion" order
27 * 2. multi value mode: multiple values with the same key could co-exist; when using iterator, the
28 * order will be the insersion order.
29 *
30 * [Basic usage]
31 *
32 * Config cfg(file_input_stream);
33 * for(Config::ConfigIterator iter = cfg.begin(); iter != cfg.end(); ++iter) {
34 * ConfigEntry ent = *iter;
35 * std::string key = ent.first;
36 * std::string value = ent.second;
37 * do_something_with(key, value);
38 * }
39 */
40 class Config {
41 public:
42 /*!
43 * \brief type when extracting from iterator
44 */
45 typedef std::pair<std::string, std::string> ConfigEntry;
46
47 /*!
48 * \brief iterator class
49 */
50 class ConfigIterator;
51
52 /*!
53 * \brief create empty config
54 * \param multi_value whether the config supports multi value
55 */
56 explicit Config(bool multi_value = false);
57 /*!
58 * \brief create config and load content from the given stream
59 * \param is input stream
60 * \param multi_value whether the config supports multi value
61 */
62 explicit Config(std::istream &is, bool multi_value = false); // NOLINT(*)
63 /*!
64 * \brief clear all the values
65 */
66 void Clear(void);
67 /*!
68 * \brief load the contents from the stream
69 * \param is the stream as input
70 */
71 void LoadFromStream(std::istream &is); // NOLINT(*)
72 /*!
73 * \brief set a key-value pair into the config; if the key already exists in the configure file,
74 * it will either replace the old value with the given one (in non-multi value mode) or
75 * store it directly (in multi-value mode);
76 * \param key key
77 * \param value value
78 * \param is_string whether the value should be wrapped by quotes in proto string
79 */
80 template <class T>
81 void SetParam(const std::string &key, const T &value, bool is_string = false);
82
83 /*!
84 * \brief get the config under the key; if multiple values exist for the same key,
85 * return the last inserted one.
86 * \param key key
87 * \return config value
88 */
89 const std::string &GetParam(const std::string &key) const;
90
91 /*!
92 * \brief check whether the configure value given by the key should be wrapped by quotes
93 * \param key key
94 * \return whether the configure value is represented by string
95 */
96 bool IsGenuineString(const std::string &key) const;
97
98 /*!
99 * \brief transform all the configuration into string recognizable to protobuf
100 * \return string that could be parsed directly by protobuf
101 */
102 std::string ToProtoString(void) const;
103
104 /*!
105 * \brief get begin iterator
106 * \return begin iterator
107 */
108 ConfigIterator begin() const;
109
110 /*!
111 * \brief get end iterator
112 * \return end iterator
113 */
114 ConfigIterator end() const;
115
116 public:
117 /*!
118 * \brief iterator class
119 */
120 class ConfigIterator {
121 friend class Config;
122
123 public:
124 using iterator_category = std::input_iterator_tag;
125 using value_type = ConfigEntry;
126 using difference_type = std::ptrdiff_t;
127 using pointer = ConfigEntry *;
128 using reference = ConfigEntry &;
129 /*!
130 * \brief copy constructor
131 */
132 ConfigIterator(const ConfigIterator &other);
133 /*!
134 * \brief uni-increment operators
135 * \return the reference of current config
136 */
137 ConfigIterator &operator++();
138 /*!
139 * \brief uni-increment operators
140 * \return the reference of current config
141 */
142 ConfigIterator operator++(int); // NOLINT(*)
143 /*!
144 * \brief compare operators
145 * \param rhs the other config to compare against
146 * \return the compared result
147 */
148 bool operator==(const ConfigIterator &rhs) const;
149 /*!
150 * \brief compare operators not equal
151 * \param rhs the other config to compare against
152 * \return the compared result
153 */
154 bool operator!=(const ConfigIterator &rhs) const;
155 /*!
156 * \brief retrieve value from operator
157 */
158 ConfigEntry operator*() const;
159
160 private:
161 ConfigIterator(size_t index, const Config *config);
162 void FindNextIndex();
163
164 private:
165 size_t index_;
166 const Config *config_;
167 };
168
169 private:
170 struct ConfigValue {
171 std::vector<std::string> val;
172 std::vector<size_t> insert_index;
173 bool is_string;
174 };
175 void Insert(const std::string &key, const std::string &value, bool is_string);
176
177 private:
178 std::map<std::string, ConfigValue> config_map_;
179 std::vector<std::pair<std::string, size_t>> order_;
180 const bool multi_value_;
181 };
182
183 template <class T>
184 void Config::SetParam(const std::string &key, const T &value, bool is_string) {
185 std::ostringstream oss;
186 oss << value;
187 Insert(key, oss.str(), is_string);
188 }
189
190 } // namespace dmlc
191
192 #endif // DMLC_CONFIG_H_