OpenTTD Source: src/core/convertible_through_base.hpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef CONVERTIBLE_THROUGH_BASE_HPP
11#define CONVERTIBLE_THROUGH_BASE_HPP
12
18template <typename T>
19 concept 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>
30 concept ConvertibleThroughBaseOrTo = std::is_convertible_v<T, TTo> || ConvertibleThroughBase<T>;
31
37template <typename Container, typename Index>
38 class 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 */
TypedIndexContainer
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific type.
Definition convertible_through_base.hpp:38
ConvertibleThroughBaseOrTo
Type is convertible to TTo, either directly or through ConvertibleThroughBase.
Definition convertible_through_base.hpp:30
ConvertibleThroughBase
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Definition convertible_through_base.hpp:19