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