OpenTTD Source: src/misc/history_type.hpp Source File

OpenTTD Source 15.0-RC2

history_type.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef HISTORY_TYPE_HPP
11#define HISTORY_TYPE_HPP
12
13 struct HistoryRange {
14 const HistoryRange *hr;
15 const uint8_t periods;
16 const uint8_t records;
17 const uint8_t first;
18 const uint8_t last;
19 const uint8_t division;
20 const uint8_t total_division;
21
22 explicit constexpr HistoryRange(uint8_t periods) :
23 hr(nullptr), periods(periods), records(this->periods), first(1), last(this->first + this->records), division(1), total_division(1)
24 {
25 }
26
27 constexpr HistoryRange(const HistoryRange &hr, uint8_t division, uint8_t periods) :
28 hr(&hr), periods(periods), records(this->periods - ((hr.periods / division) - 1)), first(hr.last), last(this->first + this->records),
29 division(division), total_division(division * hr.total_division)
30 {
31 }
32};
33
34static constexpr uint8_t HISTORY_PERIODS = 24;
35static constexpr HistoryRange HISTORY_MONTH{HISTORY_PERIODS};
36static constexpr HistoryRange HISTORY_QUARTER{HISTORY_MONTH, 3, HISTORY_PERIODS};
37static constexpr HistoryRange HISTORY_YEAR{HISTORY_QUARTER, 4, HISTORY_PERIODS};
38
40 static constexpr uint8_t HISTORY_MAX_DIVISION = std::max({HISTORY_MONTH.division, HISTORY_QUARTER.division, HISTORY_YEAR.division});
41
43 static constexpr uint8_t HISTORY_RECORDS = HISTORY_YEAR.last;
44
45static constexpr uint8_t THIS_MONTH = 0;
46static constexpr uint8_t LAST_MONTH = 1;
47
52template <typename T>
53 using HistoryData = std::array<T, HISTORY_RECORDS>;
54
56 using ValidHistoryMask = uint64_t;
57
58#endif /* HISTORY_TYPE_HPP */
HistoryData
std::array< T, HISTORY_RECORDS > HistoryData
Container type for storing history data.
Definition history_type.hpp:53
HISTORY_RECORDS
static constexpr uint8_t HISTORY_RECORDS
Total number of records require for all history data.
Definition history_type.hpp:43
ValidHistoryMask
uint64_t ValidHistoryMask
Mask of valid history records.
Definition history_type.hpp:56
HISTORY_MAX_DIVISION
static constexpr uint8_t HISTORY_MAX_DIVISION
Maximum number of divisions from previous history range.
Definition history_type.hpp:40
HistoryRange
Definition history_type.hpp:13
HistoryRange::last
const uint8_t last
Index of last element in history data.
Definition history_type.hpp:18
HistoryRange::total_division
const uint8_t total_division
Number of divisions of the initial history range.
Definition history_type.hpp:20
HistoryRange::division
const uint8_t division
Number of divisions of the previous history range.
Definition history_type.hpp:19
HistoryRange::records
const uint8_t records
Number of records needed for this range.
Definition history_type.hpp:16
HistoryRange::first
const uint8_t first
Index of first element in history data.
Definition history_type.hpp:17
HistoryRange::periods
const uint8_t periods
Number of periods for this range.
Definition history_type.hpp:15