MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
env_value.hpp
1#ifndef MSTL_CORE_FILE_ENV_ENV_VALUE_HPP__
2#define MSTL_CORE_FILE_ENV_ENV_VALUE_HPP__
3#include "MSTL/core/container/unordered_map.hpp"
5#include "MSTL/core/interface/istringify.hpp"
7
8MSTL_ERROR_BUILD_FINAL_CLASS(env_exception, value_exception, "ENV Operation Failed.")
9
10
11class env_value;
12class env_variable;
13
14class MSTL_API env_value : public istringify<env_value> {
15public:
16 enum types {
17 Variable
18 };
19
20 virtual ~env_value() = default;
21 MSTL_NODISCARD virtual types type() const noexcept = 0;
22
23 MSTL_NODISCARD virtual const env_variable* as_variable() const noexcept { return nullptr; }
24
25 MSTL_NODISCARD bool is_variable() const noexcept { return type() == Variable; }
26
27 MSTL_NODISCARD string to_string() const;
28 MSTL_NODISCARD string to_document() const;
29};
30
31using env_ptr = unique_ptr<env_value>;
32
33
34class MSTL_API env_variable final : public env_value {
35public:
36 enum quote_type { None, Single, Double };
37
38private:
39 string value_;
40 quote_type quote_type_ = None;
41 bool is_exported_ = false;
42
43public:
44 explicit env_variable(string v, const quote_type qt = None, const bool exported = false) noexcept
45 : value_(_MSTL move(v)), quote_type_(qt), is_exported_(exported) {}
46
47 MSTL_NODISCARD types type() const noexcept override { return Variable; }
48 MSTL_NODISCARD const env_variable* as_variable() const noexcept override { return this; }
49
50 MSTL_NODISCARD const string& get_value() const noexcept { return value_; }
51 void set_value(string v) noexcept { value_ = _MSTL move(v); }
52
53 MSTL_NODISCARD quote_type get_quote_type() const noexcept { return quote_type_; }
54 void set_quote_type(const quote_type qt) noexcept { quote_type_ = qt; }
55
56 MSTL_NODISCARD bool is_exported() const noexcept { return is_exported_; }
57 void set_exported(const bool exported) noexcept { is_exported_ = exported; }
58
59 MSTL_NODISCARD int get_int(int default_value = 0) const noexcept;
60 MSTL_NODISCARD int64_t get_int64(int64_t default_value = 0) const noexcept;
61 MSTL_NODISCARD double get_double(double default_value = 0.0) const noexcept;
62 MSTL_NODISCARD bool get_bool(bool default_value = false) const noexcept;
63};
64
65class MSTL_API env_document final {
66private:
67 unordered_map<string, unique_ptr<env_variable>> variables_;
68 vector<string> comments_;
69
70public:
71 env_document() = default;
72
73 env_document(const env_document&) = delete;
74 env_document& operator =(const env_document&) = delete;
75 env_document(env_document&&) = default;
76 env_document& operator =(env_document&&) = default;
77
78 void add_variable(const string& name, unique_ptr<env_variable> variable) {
79 variables_[name] = _MSTL move(variable);
80 }
81
82 void set_variable(const string& name, const string& value,
83 env_variable::quote_type qt = env_variable::None,
84 bool exported = false) {
85 variables_[name] = make_unique<env_variable>(value, qt, exported);
86 }
87
88 MSTL_NODISCARD const env_variable* get_variable(const string& name) const {
89 const auto it = variables_.find(name);
90 if (it != variables_.end()) return it->second.get();
91 return nullptr;
92 }
93
94 MSTL_NODISCARD env_variable* get_variable(const string& name) {
95 const auto it = variables_.find(name);
96 if (it != variables_.end()) return it->second.get();
97 return nullptr;
98 }
99
100 MSTL_NODISCARD bool has_variable(const string& name) const {
101 return variables_.find(name) != variables_.end();
102 }
103
104 void remove_variable(const string& name) {
105 variables_.erase(name);
106 }
107
108 MSTL_NODISCARD const unordered_map<string, unique_ptr<env_variable>>& get_variables() const noexcept {
109 return variables_;
110 }
111
112 void add_comment(const string& comment) {
113 comments_.push_back(comment);
114 }
115
116 MSTL_NODISCARD const vector<string>& get_comments() const noexcept {
117 return comments_;
118 }
119
120 MSTL_NODISCARD string get_string(const string& name, const string& default_value = "") const {
121 const auto* var = get_variable(name);
122 return var ? var->get_value() : default_value;
123 }
124
125 MSTL_NODISCARD int get_int(const string& name, const int default_value = 0) const {
126 const auto* var = get_variable(name);
127 return var ? var->get_int(default_value) : default_value;
128 }
129
130 MSTL_NODISCARD int64_t get_int64(const string& name, const int64_t default_value = 0) const {
131 const auto* var = get_variable(name);
132 return var ? var->get_int64(default_value) : default_value;
133 }
134
135 MSTL_NODISCARD double get_double(const string& name, const double default_value = 0.0) const {
136 const auto* var = get_variable(name);
137 return var ? var->get_double(default_value) : default_value;
138 }
139
140 MSTL_NODISCARD bool get_bool(const string& name, const bool default_value = false) const {
141 const auto* var = get_variable(name);
142 return var ? var->get_bool(default_value) : default_value;
143 }
144
145 MSTL_NODISCARD string to_string() const;
146};
147
149string MSTL_API env_value_to_string(const env_value* value, const string& key = "");
150string MSTL_API env_document_to_string(const env_document* doc);
152
153MSTL_ALWAYS_INLINE_INLINE string to_string(const env_value* value) {
154 return _INNER env_value_to_string(value);
155}
156MSTL_ALWAYS_INLINE_INLINE string to_string(const env_value& value) {
157 return _INNER env_value_to_string(&value);
158}
159MSTL_ALWAYS_INLINE_INLINE string to_string(const env_ptr& value) {
160 return _INNER env_value_to_string(value.get());
161}
162MSTL_ALWAYS_INLINE_INLINE string to_string(const env_document& doc) {
163 return _INNER env_document_to_string(&doc);
164}
165
166MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string env_value::to_string() const {
167 return _INNER env_value_to_string(this);
168}
169MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string env_value::to_document() const {
170 return _INNER env_value_to_string(this);
171}
172MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string env_document::to_string() const {
173 return _INNER env_document_to_string(this);
174}
175
177#endif // MSTL_CORE_FILE_ENV_ENV_VALUE_HPP__
独占智能指针
MSTL_CONSTEXPR20 pointer get() const noexcept
获取原始指针
long long int64_t
64位有符号整数类型
#define MSTL_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
MSTL_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
变量处理异常
MSTL独占智能指针