1 // Aseprite
2 // Copyright (C) 2001-2017  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_COMMANDS_PARAMS_H
8 #define APP_COMMANDS_PARAMS_H
9 
10 #include <map>
11 #include <string>
12 #include <sstream>
13 
14 namespace app {
15 
16   class Params {
17   public:
18     typedef std::map<std::string, std::string> Map;
19     typedef Map::iterator iterator;
20     typedef Map::const_iterator const_iterator;
21 
begin()22     iterator begin() { return m_params.begin(); }
end()23     iterator end() { return m_params.end(); }
begin()24     const_iterator begin() const { return m_params.begin(); }
end()25     const_iterator end() const { return m_params.end(); }
26 
empty()27     bool empty() const { return m_params.empty(); }
size()28     int size() const { return int(m_params.size()); }
clear()29     void clear() { return m_params.clear(); }
30 
has_param(const char * name)31     bool has_param(const char* name) const {
32       return m_params.find(name) != m_params.end();
33     }
34 
35     bool operator==(const Params& params) const {
36       return m_params == params.m_params;
37     }
38 
39     bool operator!=(const Params& params) const {
40       return m_params != params.m_params;
41     }
42 
set(const char * name,const char * value)43     std::string& set(const char* name, const char* value) {
44       return m_params[name] = value;
45     }
46 
get(const char * name)47     std::string get(const char* name) const {
48       auto it = m_params.find(name);
49       if (it != m_params.end())
50         return it->second;
51       else
52         return std::string();
53     }
54 
55     void operator|=(const Params& params) {
56       for (const auto& p : params)
57         m_params[p.first] = p.second;
58     }
59 
60     template<typename T>
get_as(const char * name)61     const T get_as(const char* name) const {
62       T value = T();
63       auto it = m_params.find(name);
64       if (it != m_params.end()) {
65         std::istringstream stream(it->second);
66         stream >> value;
67       }
68       return value;
69     }
70 
71   private:
72     Map m_params;
73   };
74 
75 } // namespace app
76 
77 #endif
78