OpenTTD Source 20250221-master-gcc93699459
source_type.h
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 SOURCE_TYPE_H
11#define SOURCE_TYPE_H
12
13#include "company_type.h"
14#include "industry_type.h"
15#include "town_type.h"
16
18enum class SourceType : uint8_t {
19 Industry = 0,
20 Town = 1,
21 Headquarters = 2,
22};
23
24using SourceID = uint16_t;
25static_assert(sizeof(SourceID) >= sizeof(CompanyID));
26static_assert(sizeof(SourceID) >= sizeof(IndustryID));
27static_assert(sizeof(SourceID) >= sizeof(TownID));
28
30struct Source {
31public:
32 static constexpr SourceID Invalid = 0xFFFF;
33
36
37 Source() = default;
38 Source(ConvertibleThroughBase auto id, SourceType type) : id(id.base()), type(type) {}
40
41 constexpr CompanyID ToCompanyID() const { assert(this->type == SourceType::Headquarters); return static_cast<CompanyID>(this->id); }
42 constexpr IndustryID ToIndustryID() const { assert(this->type == SourceType::Industry); return static_cast<IndustryID>(this->id); }
43 constexpr TownID ToTownID() const { assert(this->type == SourceType::Town); return static_cast<TownID>(this->id); }
44
45 constexpr void MakeInvalid() { this->id = Source::Invalid; }
46 constexpr void SetIndex(SourceID index) { this->id = index; }
47 constexpr void SetIndex(ConvertibleThroughBase auto index) { this->id = index.base(); }
48
49 constexpr bool IsValid() const noexcept { return this->id != Source::Invalid; }
50 auto operator<=>(const Source &source) const = default;
51};
52
53#endif /* SOURCE_TYPE_H */
Types related to companies.
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Types related to the industry.
uint16_t SourceID
Contains either industry ID, town ID or company ID (or Source::Invalid)
Definition source_type.h:24
SourceType
Types of cargo source and destination.
Definition source_type.h:18
@ Industry
Source/destination is an industry.
@ Headquarters
Source/destination are company headquarters.
@ Town
Source/destination is a town.
Defines the internal data of a functional industry.
Definition industry.h:63
A location from where cargo can come from (or go to).
Definition source_type.h:30
static constexpr SourceID Invalid
Invalid/unknown index of source.
Definition source_type.h:32
SourceID id
Index of industry/town/HQ, Source::Invalid if unknown/invalid.
Definition source_type.h:34
SourceType type
Type of source_id.
Definition source_type.h:35
Town data structure.
Definition town.h:52
Types related to towns.