OpenTTD Source 20250613-master-ga1786fa1f4
convertible_through_base.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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef CONVERTIBLE_THROUGH_BASE_HPP
11#define CONVERTIBLE_THROUGH_BASE_HPP
12
18template <typename T>
19concept ConvertibleThroughBase = requires(T const a) {
20 typename T::BaseType;
21 { a.base() } noexcept -> std::convertible_to<int64_t>;
22};
23
29template <typename T, typename TTo>
30concept ConvertibleThroughBaseOrTo = std::is_convertible_v<T, TTo> || ConvertibleThroughBase<T>;
31
37template <typename Container, typename Index>
38class TypedIndexContainer : public Container {
39public:
40 Container::reference at(size_t pos) { return this->Container::at(pos); }
41 Container::reference at(const Index &pos) { return this->Container::at(pos.base()); }
42
43 Container::const_reference at(size_t pos) const { return this->Container::at(pos); }
44 Container::const_reference at(const Index &pos) const { return this->Container::at(pos.base()); }
45
46 Container::reference operator[](size_t pos) { return this->Container::operator[](pos); }
47 Container::reference operator[](const Index &pos) { return this->Container::operator[](pos.base()); }
48
49 Container::const_reference operator[](size_t pos) const { return this->Container::operator[](pos); }
50 Container::const_reference operator[](const Index &pos) const { return this->Container::operator[](pos.base()); }
51};
52
53#endif /* CONVERTIBLE_THROUGH_BASE_HPP */
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific type.
Type is convertible to TTo, either directly or through ConvertibleThroughBase.
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...