]> git.stg.codes - stg.git/blob - include/stg/service_conf.h
Public interfaces: part 1
[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 "stg/optional.h"
24
25 #include <string>
26 #include <cstdint>
27
28 namespace STG
29 {
30
31 struct ServiceConf
32 {
33     ServiceConf()
34         : cost(0), payDay(0)
35     {}
36     explicit ServiceConf(const std::string & n)
37         : name(n), cost(0), payDay(0)
38     {}
39     ServiceConf(const std::string & n, double c)
40         : name(n), cost(c), payDay(0)
41     {}
42     ServiceConf(const std::string & n, double c, unsigned p)
43         : name(n), cost(c), payDay(static_cast<uint8_t>(p))
44     {}
45     ServiceConf(const std::string & n, double c,
46                  unsigned p, const std::string & com)
47         : name(n), comment(com), cost(c), payDay(static_cast<uint8_t>(p))
48     {}
49
50     ServiceConf(const ServiceConf&) = default;
51     ServiceConf& operator=(const ServiceConf&) = default;
52     ServiceConf(ServiceConf&&) = default;
53     ServiceConf& operator=(ServiceConf&&) = default;
54
55     bool operator==(const ServiceConf& rhs) const noexcept { return name == rhs.name; }
56
57     std::string name;
58     std::string comment;
59     double      cost;
60     uint8_t     payDay;
61 };
62
63 struct ServiceConfOpt
64 {
65     ServiceConfOpt() = default;
66
67     explicit ServiceConfOpt(const ServiceConf& rhs)
68         : name(rhs.name), comment(rhs.comment),
69           cost(rhs.cost), payDay(rhs.payDay)
70     {}
71
72     ServiceConfOpt(const ServiceConfOpt&) = default;
73     ServiceConfOpt& operator=(const ServiceConfOpt&) = default;
74     ServiceConfOpt(ServiceConfOpt&&) = default;
75     ServiceConfOpt& operator=(ServiceConfOpt&&) = default;
76
77     ServiceConfOpt& operator=(const ServiceConf& conf)
78     {
79         name = conf.name;
80         comment = conf.comment;
81         cost = conf.cost;
82         payDay = conf.payDay;
83         return *this;
84     }
85
86     void splice(const ServiceConfOpt& rhs)
87     {
88         name.splice(rhs.name);
89         comment.splice(rhs.comment);
90         cost.splice(rhs.cost);
91         payDay.splice(rhs.payDay);
92     }
93
94     ServiceConf get(const ServiceConf& defaultValue) const noexcept
95     {
96         ServiceConf res;
97         res.name = name.get(defaultValue.name);
98         res.comment = comment.get(defaultValue.comment);
99         res.cost = cost.get(defaultValue.cost);
100         res.payDay = payDay.get(defaultValue.payDay);
101         return res;
102     }
103
104     Optional<std::string> name;
105     Optional<std::string> comment;
106     Optional<double>      cost;
107     Optional<uint8_t>     payDay;
108 };
109
110 }