]> git.stg.codes - stg.git/blob - include/stg/service_conf.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / include / stg / service_conf.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #pragma once
22
23 #include "splice.h"
24
25 #include <string>
26 #include <optional>
27 #include <cstdint>
28
29 namespace STG
30 {
31
32 struct ServiceConf
33 {
34     ServiceConf()
35         : cost(0), payDay(0)
36     {}
37     explicit ServiceConf(const std::string & n)
38         : name(n), cost(0), payDay(0)
39     {}
40     ServiceConf(const std::string & n, double c)
41         : name(n), cost(c), payDay(0)
42     {}
43     ServiceConf(const std::string & n, double c, unsigned p)
44         : name(n), cost(c), payDay(static_cast<uint8_t>(p))
45     {}
46     ServiceConf(const std::string & n, double c,
47                  unsigned p, const std::string & com)
48         : name(n), comment(com), cost(c), payDay(static_cast<uint8_t>(p))
49     {}
50
51     ServiceConf(const ServiceConf&) = default;
52     ServiceConf& operator=(const ServiceConf&) = default;
53     ServiceConf(ServiceConf&&) = default;
54     ServiceConf& operator=(ServiceConf&&) = default;
55
56     bool operator==(const ServiceConf& rhs) const noexcept { return name == rhs.name; }
57
58     std::string name;
59     std::string comment;
60     double      cost;
61     uint8_t     payDay;
62 };
63
64 struct ServiceConfOpt
65 {
66     ServiceConfOpt() = default;
67
68     explicit ServiceConfOpt(const ServiceConf& rhs)
69         : name(rhs.name), comment(rhs.comment),
70           cost(rhs.cost), payDay(rhs.payDay)
71     {}
72
73     ServiceConfOpt(const ServiceConfOpt&) = default;
74     ServiceConfOpt& operator=(const ServiceConfOpt&) = default;
75     ServiceConfOpt(ServiceConfOpt&&) = default;
76     ServiceConfOpt& operator=(ServiceConfOpt&&) = default;
77
78     ServiceConfOpt& operator=(const ServiceConf& conf)
79     {
80         name = conf.name;
81         comment = conf.comment;
82         cost = conf.cost;
83         payDay = conf.payDay;
84         return *this;
85     }
86
87     void splice(const ServiceConfOpt& rhs)
88     {
89         STG::splice(name, rhs.name);
90         STG::splice(comment, rhs.comment);
91         STG::splice(cost, rhs.cost);
92         STG::splice(payDay, rhs.payDay);
93     }
94
95     ServiceConf get(const ServiceConf& defaultValue) const noexcept
96     {
97         ServiceConf res;
98         res.name = name.value_or(defaultValue.name);
99         res.comment = comment.value_or(defaultValue.comment);
100         res.cost = cost.value_or(defaultValue.cost);
101         res.payDay = payDay.value_or(defaultValue.payDay);
102         return res;
103     }
104
105     std::optional<std::string> name;
106     std::optional<std::string> comment;
107     std::optional<double>      cost;
108     std::optional<uint8_t>     payDay;
109 };
110
111 }