]> git.stg.codes - stg.git/blob - include/stg/tariff_conf.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / include / stg / tariff_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 : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #pragma once
22
23 #include "tariff.h"
24 #include "const.h"
25 #include "splice.h"
26
27 #include <string>
28 #include <vector>
29 #include <optional>
30
31 namespace STG
32 {
33 //-----------------------------------------------------------------------------
34 struct DirPriceData
35 {
36     DirPriceData() noexcept
37         : hDay(0),
38           mDay(0),
39           hNight(0),
40           mNight(0),
41           priceDayA(0),
42           priceNightA(0),
43           priceDayB(0),
44           priceNightB(0),
45           threshold(0),
46           singlePrice(0),
47           noDiscount(0)
48     {}
49
50     DirPriceData(const DirPriceData&) = default;
51     DirPriceData& operator=(const DirPriceData&) = default;
52     DirPriceData(DirPriceData&&) = default;
53     DirPriceData& operator=(DirPriceData&&) = default;
54
55     int    hDay;
56     int    mDay;
57     int    hNight;
58     int    mNight;
59     double priceDayA;
60     double priceNightA;
61     double priceDayB;
62     double priceNightB;
63     int    threshold;
64     int    singlePrice; // Do not use day/night division
65     int    noDiscount; // Do not use threshold
66 };
67 //-----------------------------------------------------------------------------
68 struct DirPriceDataOpt
69 {
70     DirPriceDataOpt() = default;
71
72     DirPriceDataOpt(const DirPriceData& data) noexcept
73         : hDay(data.hDay),
74           mDay(data.mDay),
75           hNight(data.hNight),
76           mNight(data.mNight),
77           priceDayA(data.priceDayA),
78           priceNightA(data.priceNightA),
79           priceDayB(data.priceDayB),
80           priceNightB(data.priceNightB),
81           threshold(data.threshold),
82           singlePrice(data.singlePrice),
83           noDiscount(data.noDiscount)
84     {}
85
86     DirPriceDataOpt(const DirPriceDataOpt&) = default;
87     DirPriceDataOpt& operator=(const DirPriceDataOpt&) = default;
88     DirPriceDataOpt(DirPriceDataOpt&&) = default;
89     DirPriceDataOpt& operator=(DirPriceDataOpt&&) = default;
90
91     DirPriceDataOpt & operator=(const DirPriceData& rhs) noexcept
92     {
93         hDay        = rhs.hDay;
94         mDay        = rhs.mDay;
95         hNight      = rhs.hNight;
96         mNight      = rhs.mNight;
97         priceDayA   = rhs.priceDayA;
98         priceNightA = rhs.priceNightA;
99         priceDayB   = rhs.priceDayB;
100         priceNightB = rhs.priceNightB;
101         threshold   = rhs.threshold;
102         singlePrice = rhs.singlePrice;
103         noDiscount  = rhs.noDiscount;
104         return *this;
105     }
106
107     void splice(const DirPriceDataOpt & rhs) noexcept
108     {
109         STG::splice(hDay, rhs.hDay);
110         STG::splice(mDay, rhs.mDay);
111         STG::splice(hNight, rhs.hNight);
112         STG::splice(mNight, rhs.mNight);
113         STG::splice(priceDayA, rhs.priceDayA);
114         STG::splice(priceNightA, rhs.priceNightA);
115         STG::splice(priceDayB, rhs.priceDayB);
116         STG::splice(priceNightB, rhs.priceNightB);
117         STG::splice(threshold, rhs.threshold);
118         STG::splice(singlePrice, rhs.singlePrice);
119         STG::splice(noDiscount, rhs.noDiscount);
120     }
121
122     DirPriceData get(const DirPriceData& defaultValue) const noexcept
123     {
124         DirPriceData res;
125         res.hDay = hDay.value_or(defaultValue.hDay);
126         res.mDay = mDay.value_or(defaultValue.mDay);
127         res.hNight = hNight.value_or(defaultValue.hNight);
128         res.mNight = mNight.value_or(defaultValue.mNight);
129         res.priceDayA = priceDayA.value_or(defaultValue.priceDayA);
130         res.priceNightA = priceNightA.value_or(defaultValue.priceNightA);
131         res.priceDayB = priceDayB.value_or(defaultValue.priceDayB);
132         res.priceNightB = priceNightB.value_or(defaultValue.priceNightB);
133         res.threshold = threshold.value_or(defaultValue.threshold);
134         res.singlePrice = singlePrice.value_or(defaultValue.singlePrice);
135         res.noDiscount = noDiscount.value_or(defaultValue.noDiscount);
136         return res;
137     }
138
139     std::optional<int>    hDay;
140     std::optional<int>    mDay;
141     std::optional<int>    hNight;
142     std::optional<int>    mNight;
143     std::optional<double> priceDayA;
144     std::optional<double> priceNightA;
145     std::optional<double> priceDayB;
146     std::optional<double> priceNightB;
147     std::optional<int>    threshold;
148     std::optional<int>    singlePrice;
149     std::optional<int>    noDiscount;
150 };
151 //-----------------------------------------------------------------------------
152 struct TariffConf
153 {
154     double             fee;
155     double             free;
156     Tariff::TraffType  traffType;
157     double             passiveCost;
158     std::string        name;
159     Tariff::Period     period;
160     Tariff::ChangePolicy changePolicy;
161     time_t changePolicyTimeout;
162
163     TariffConf() noexcept
164         : fee(0),
165           free(0),
166           traffType(Tariff::TRAFF_UP_DOWN),
167           passiveCost(0),
168           period(Tariff::MONTH),
169           changePolicy(Tariff::ALLOW),
170           changePolicyTimeout(0)
171     {}
172
173     explicit TariffConf(const std::string & n) noexcept
174         : fee(0),
175           free(0),
176           traffType(Tariff::TRAFF_UP_DOWN),
177           passiveCost(0),
178           name(n),
179           period(Tariff::MONTH),
180           changePolicy(Tariff::ALLOW),
181           changePolicyTimeout(0)
182     {}
183
184     TariffConf(const TariffConf&) = default;
185     TariffConf& operator=(const TariffConf&) = default;
186     TariffConf(TariffConf&&) = default;
187     TariffConf& operator=(TariffConf&&) = default;
188 };
189 //-----------------------------------------------------------------------------
190 struct TariffConfOpt
191 {
192     TariffConfOpt() = default;
193     TariffConfOpt(const TariffConf& data) noexcept
194         : fee(data.fee),
195           free(data.free),
196           traffType(data.traffType),
197           passiveCost(data.passiveCost),
198           name(data.name),
199           period(data.period),
200           changePolicy(data.changePolicy),
201           changePolicyTimeout(data.changePolicyTimeout)
202     {}
203     TariffConfOpt& operator=(const TariffConf & tc) noexcept
204     {
205         fee         = tc.fee;
206         free        = tc.free;
207         traffType   = tc.traffType;
208         passiveCost = tc.passiveCost;
209         name        = tc.name;
210         period      = tc.period;
211         changePolicy = tc.changePolicy;
212         changePolicyTimeout = tc.changePolicyTimeout;
213         return *this;
214     }
215
216     TariffConfOpt(const TariffConfOpt&) = default;
217     TariffConfOpt& operator=(const TariffConfOpt&) = default;
218     TariffConfOpt(TariffConfOpt&&) = default;
219     TariffConfOpt& operator=(TariffConfOpt&&) = default;
220
221     TariffConf get(const TariffConf& defaultValue) const noexcept
222     {
223         TariffConf res;
224         res.fee = fee.value_or(defaultValue.fee);
225         res.free = free.value_or(defaultValue.free);
226         res.traffType = traffType.value_or(defaultValue.traffType);
227         res.passiveCost = passiveCost.value_or(defaultValue.passiveCost);
228         res.name = name.value_or(defaultValue.name);
229         res.period = period.value_or(defaultValue.period);
230         res.changePolicy = changePolicy.value_or(defaultValue.changePolicy);
231         res.changePolicyTimeout = changePolicyTimeout.value_or(defaultValue.changePolicyTimeout);
232         return res;
233     }
234
235     std::optional<double>             fee;
236     std::optional<double>             free;
237     std::optional<Tariff::TraffType>  traffType;
238     std::optional<double>             passiveCost;
239     std::optional<std::string>        name;
240     std::optional<Tariff::Period>     period;
241     std::optional<Tariff::ChangePolicy> changePolicy;
242     std::optional<time_t>             changePolicyTimeout;
243 };
244 //-----------------------------------------------------------------------------
245 struct TariffData
246 {
247     TariffConf                tariffConf;
248     std::vector<DirPriceData> dirPrice;
249
250     TariffData() noexcept
251         : dirPrice(DIR_NUM)
252     {}
253
254     explicit TariffData(const std::string& name) noexcept
255         : tariffConf(name),
256           dirPrice(DIR_NUM)
257     {}
258
259     TariffData(const TariffData&) = default;
260     TariffData& operator=(const TariffData&) = default;
261     TariffData(TariffData&&) = default;
262     TariffData& operator=(TariffData&&) = default;
263 };
264 //-----------------------------------------------------------------------------
265 struct TariffDataOpt
266 {
267     TariffConfOpt                tariffConf;
268     std::vector<DirPriceDataOpt> dirPrice;
269
270     TariffDataOpt()
271         : dirPrice(DIR_NUM)
272     {}
273
274     TariffDataOpt(const TariffData& data) noexcept
275         : tariffConf(data.tariffConf),
276           dirPrice(DIR_NUM)
277     {
278         for (size_t i = 0; i < DIR_NUM; ++i)
279             dirPrice[i] = data.dirPrice[i];
280     }
281
282     TariffDataOpt& operator=(const TariffData& td) noexcept
283     {
284         tariffConf = td.tariffConf;
285         for (size_t i = 0; i < DIR_NUM; ++i)
286             dirPrice[i] = td.dirPrice[i];
287         return *this;
288     }
289
290     TariffDataOpt(const TariffDataOpt&) = default;
291     TariffDataOpt& operator=(const TariffDataOpt&) = default;
292     TariffDataOpt(TariffDataOpt&&) = default;
293     TariffDataOpt& operator=(TariffDataOpt&&) = default;
294
295     TariffData get(const TariffData& defaultValue) const noexcept
296     {
297         TariffData res;
298         res.tariffConf = tariffConf.get(defaultValue.tariffConf);
299         for (size_t i = 0; i < DIR_NUM; ++i)
300             res.dirPrice[i] = dirPrice[i].get(defaultValue.dirPrice[i]);
301         return res;
302     }
303 };
304 //-----------------------------------------------------------------------------
305 }