FireSTARR
Loading...
Searching...
No Matches
FuelLookup.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_FUELLOOKUP_H
3#define FS_FUELLOOKUP_H
4#include "stdafx.h"
5#include "Cell.h"
6#include "FireWeather.h"
7#include "FuelType.h"
8#include "Util.h"
9namespace fs
10{
11class FuelLookupImpl;
12string simplify_fuel_name(const string_view fuel);
17{
18public:
19 ~FuelLookup() = default;
24 explicit FuelLookup(const char* filename);
25 FuelLookup(const FuelLookup& rhs) noexcept = default;
26 FuelLookup(FuelLookup&& rhs) noexcept = default;
27 FuelLookup& operator=(const FuelLookup& rhs) noexcept = default;
28 FuelLookup& operator=(FuelLookup&& rhs) noexcept = default;
35 [[nodiscard]] const FuelType* codeToFuel(FuelSize value, FuelSize nodata) const;
39 void listFuels() const;
45 [[nodiscard]] FuelSize fuelToCode(const FuelType* fuel) const;
52 [[nodiscard]] const FuelType* operator()(FuelSize value, FuelSize nodata) const;
57 [[nodiscard]] set<const FuelType*> usedFuels() const;
63 [[nodiscard]] const FuelType* byName(const string_view name) const;
69 [[nodiscard]] const FuelType* bySimplifiedName(const string_view name) const;
73 static const array<const FuelType*, NUMBER_OF_FUELS> Fuels;
74
75private:
79 shared_ptr<FuelLookupImpl> impl_{nullptr};
80};
86[[nodiscard]] constexpr const FuelType* fuel_by_code(const FuelCodeSize& code)
87{
88 return FuelLookup::Fuels.at(code);
89}
95[[nodiscard]] constexpr const FuelType* check_fuel(const Cell& cell)
96{
97 return fuel_by_code(cell.fuelCode());
98}
104[[nodiscard]] constexpr bool is_null_fuel(const FuelType* fuel)
105{
106 return INVALID_FUEL_CODE == FuelType::safeCode(fuel);
107}
113[[nodiscard]] constexpr bool is_null_fuel(const Cell& cell)
114{
115 return is_null_fuel(fuel_by_code(cell.fuelCode()));
116}
118{
119public:
120 using LazyPath::LazyPath;
121 const FuelLookup& lookup() const
122 {
123 // HACK: pretend this is const because it only gets assigned once
124 if (nullptr == fuel_lookup_)
125 {
126 fuel_lookup_ = std::make_unique<FuelLookup>(canonical());
127 logging::check_fatal(nullptr == fuel_lookup_, "Fuel lookup table has not been loaded");
128 }
129 return *fuel_lookup_;
130 }
131 LazyFuelLookup& operator=(const LazyFuelLookup& rhs) noexcept
132 {
133 LazyPath::operator=(rhs);
134 fuel_lookup_ = nullptr;
135 return *this;
136 }
137 LazyFuelLookup& operator=(LazyFuelLookup&& rhs) noexcept
138 {
139 LazyPath::operator=(rhs);
140 fuel_lookup_ = std::move(rhs.fuel_lookup_);
141 rhs.fuel_lookup_ = nullptr;
142 return *this;
143 }
144 LazyFuelLookup& operator=(const string& path) noexcept
145 {
146 LazyPath::operator=(path);
147 fuel_lookup_ = nullptr;
148 return *this;
149 }
150
151protected:
152 mutable unique_ptr<FuelLookup> fuel_lookup_{nullptr};
153};
154}
155#endif
Provides ability to look up a fuel type based on name or code.
Definition FuelLookup.h:17
const FuelType * byName(const string_view name) const
Look up a FuelType based on the given name.
Definition FuelLookup.cpp:642
FuelLookup(const char *filename)
Construct by reading from a file.
Definition FuelLookup.cpp:647
FuelSize fuelToCode(const FuelType *fuel) const
Look up the original code for the given FuelType.
Definition FuelLookup.cpp:633
const FuelType * operator()(FuelSize value, FuelSize nodata) const
Look up a FuelType ba1ed on the given code.
Definition FuelLookup.cpp:637
static const array< const FuelType *, NUMBER_OF_FUELS > Fuels
Array of all FuelTypes available to be used in simulations.
Definition FuelLookup.h:73
const FuelType * codeToFuel(FuelSize value, FuelSize nodata) const
Look up a FuelType based on the given code.
Definition FuelLookup.cpp:628
set< const FuelType * > usedFuels() const
Retrieve set of FuelTypes that are used in the lookup table.
Definition FuelLookup.cpp:641
const FuelType * bySimplifiedName(const string_view name) const
Look up a FuelType based on the given simplified name.
Definition FuelLookup.cpp:643
shared_ptr< FuelLookupImpl > impl_
Implementation class for FuelLookup.
Definition FuelLookup.h:79
void listFuels() const
List all fuels and their codes.
Definition FuelLookup.cpp:632
An FBP fuel type.
Definition FuelType.h:42
static constexpr FuelCodeSize safeCode(const FuelType *fuel)
Convert FuelType to its code, or 0 if nullptr.
Definition FuelType.h:53
Definition FuelLookup.h:118
Definition Util.h:338