OpenTTD Source 20250312-master-gcdcc6b491d
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
36template <typename Container>
37class ReferenceThroughBaseContainer : public Container {
38public:
39 Container::reference at(size_t pos) { return this->Container::at(pos); }
40 Container::reference at(const ConvertibleThroughBase auto &pos) { return this->Container::at(pos.base()); }
41
42 Container::const_reference at(size_t pos) const { return this->Container::at(pos); }
43 Container::const_reference at(const ConvertibleThroughBase auto &pos) const { return this->Container::at(pos.base()); }
44
45 Container::reference operator[](size_t pos) { return this->Container::operator[](pos); }
46 Container::reference operator[](const ConvertibleThroughBase auto &pos) { return this->Container::operator[](pos.base()); }
47
48 Container::const_reference operator[](size_t pos) const { return this->Container::operator[](pos); }
49 Container::const_reference operator[](const ConvertibleThroughBase auto &pos) const { return this->Container::operator[](pos.base()); }
50};
51
52#endif /* CONVERTIBLE_THROUGH_BASE_HPP */
A sort-of mixin that adds 'at(pos)' and 'operator[](pos)' implementations for 'ConvertibleThroughBase...
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...