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
14namespace 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
22 iterator begin() { return m_params.begin(); }
23 iterator end() { return m_params.end(); }
24 const_iterator begin() const { return m_params.begin(); }
25 const_iterator end() const { return m_params.end(); }
26
27 bool empty() const { return m_params.empty(); }
28 int size() const { return int(m_params.size()); }
29 void clear() { return m_params.clear(); }
30
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
43 std::string& set(const char* name, const char* value) {
44 return m_params[name] = value;
45 }
46
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>
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 template<>
76 inline const bool Params::get_as<bool>(const char* name) const {
77 bool value = false;
78 auto it = m_params.find(name);
79 if (it != m_params.end()) {
80 value = (it->second == "1" ||
81 it->second == "true");
82 }
83 return value;
84 }
85
86} // namespace app
87
88#endif
89