mxnet
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 
19 namespace dmlc {
20 
40 class 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:
120  class ConfigIterator : public std::iterator< std::input_iterator_tag, ConfigEntry > {
121  friend class Config;
122  public:
126  ConfigIterator(const ConfigIterator& other);
136  ConfigIterator operator++(int); // NOLINT(*)
142  bool operator == (const ConfigIterator& rhs) const;
148  bool operator != (const ConfigIterator& rhs) const;
152  ConfigEntry operator * () const;
153 
154  private:
155  ConfigIterator(size_t index, const Config* config);
156  void FindNextIndex();
157 
158  private:
159  size_t index_;
160  const Config* config_;
161  };
162 
163  private:
164  struct ConfigValue {
165  std::vector<std::string> val;
166  std::vector<size_t> insert_index;
167  bool is_string;
168  };
169  void Insert(const std::string& key, const std::string& value, bool is_string);
170 
171  private:
172  std::map<std::string, ConfigValue> config_map_;
173  std::vector<std::pair<std::string, size_t> > order_;
174  const bool multi_value_;
175 };
176 
177 template<class T>
178 void Config::SetParam(const std::string& key, const T& value, bool is_string) {
179  std::ostringstream oss;
180  oss << value;
181  Insert(key, oss.str(), is_string);
182 }
183 
184 } // namespace dmlc
185 
186 #endif // DMLC_CONFIG_H_
void LoadFromStream(std::istream &is)
load the contents from the stream
iterator class
Definition: config.h:120
ConfigIterator end() const
get end iterator
bool IsGenuineString(const std::string &key) const
check whether the configure value given by the key should be wrapped by quotes
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...
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, it will either replace the old value with the given one (in non-multi value mode) or store it directly (in multi-value mode);
Definition: config.h:178
ConfigIterator & operator++()
uni-increment operators
std::string ToProtoString(void) const
transform all the configuration into string recognizable to protobuf
std::pair< std::string, std::string > ConfigEntry
type when extracting from iterator
Definition: config.h:45
namespace for dmlc
Definition: array_view.h:12
Config(bool multi_value=false)
create empty config
class for config parser
Definition: config.h:40
bool operator==(const ConfigIterator &rhs) const
compare operators
ConfigEntry operator*() const
retrieve value from operator
ConfigIterator begin() const
get begin iterator
bool operator!=(const ConfigIterator &rhs) const
compare operators not equal
void Clear(void)
clear all the values
ConfigIterator(const ConfigIterator &other)
copy constructor