Codechange: Use std::unique_ptr for link graph schedule handlers. (#12988)
[openttd-github.git] / src / linkgraph / linkgraphschedule.h
blob1a63fbb8805303473ce2bdd6cd8cffbef7e8f586
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 */
8 /** @file linkgraphschedule.h Declaration of link graph schedule used for cargo distribution. */
10 #ifndef LINKGRAPHSCHEDULE_H
11 #define LINKGRAPHSCHEDULE_H
13 #include "linkgraph.h"
15 class LinkGraphJob;
17 /**
18 * A handler doing "something" on a link graph component. It must not keep any
19 * state as it is called concurrently from different threads.
21 class ComponentHandler {
22 public:
23 /**
24 * Destroy the handler. Must be given due to virtual Run.
26 virtual ~ComponentHandler() = default;
28 /**
29 * Run the handler. A link graph handler must not read or write any data
30 * outside the given component as that would create a potential desync.
31 * @param job Link graph component to run the handler on.
33 virtual void Run(LinkGraphJob &job) const = 0;
36 class LinkGraphSchedule {
37 private:
38 LinkGraphSchedule();
39 ~LinkGraphSchedule();
40 typedef std::list<LinkGraph *> GraphList;
41 typedef std::list<LinkGraphJob *> JobList;
42 friend SaveLoadTable GetLinkGraphScheduleDesc();
44 protected:
45 std::array<std::unique_ptr<ComponentHandler>, 6> handlers{}; ///< Handlers to be run for each job.
46 GraphList schedule; ///< Queue for new jobs.
47 JobList running; ///< Currently running jobs.
49 public:
50 /* This is a tick where not much else is happening, so a small lag might go unnoticed. */
51 static const uint SPAWN_JOIN_TICK = 21; ///< Tick when jobs are spawned or joined every day.
52 static LinkGraphSchedule instance;
54 static void Run(LinkGraphJob *job);
55 static void Clear();
57 void SpawnNext();
58 bool IsJoinWithUnfinishedJobDue() const;
59 void JoinNext();
60 void SpawnAll();
61 void ShiftDates(TimerGameEconomy::Date interval);
63 /**
64 * Queue a link graph for execution.
65 * @param lg Link graph to be queued.
67 void Queue(LinkGraph *lg)
69 assert(LinkGraph::Get(lg->index) == lg);
70 this->schedule.push_back(lg);
73 /**
74 * Remove a link graph from the execution queue.
75 * @param lg Link graph to be removed.
77 void Unqueue(LinkGraph *lg) { this->schedule.remove(lg); }
80 void StateGameLoop_LinkGraphPauseControl();
81 void AfterLoad_LinkGraphPauseControl();
83 #endif /* LINKGRAPHSCHEDULE_H */