diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-06-01 01:39:04 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-06-17 00:04:03 +0200 |
| commit | ec06811bae7ed4fc6bd80c3154fd473028ee8e13 (patch) | |
| tree | b398119a1a84d261ee16b57171652fd8714d657a /src/lib/ratio.cc | |
| parent | 21e9bd77598f7bb4588ca9b16a58667e3340f6d9 (diff) | |
Pass Ratio around as an object rather than a pointer.
Diffstat (limited to 'src/lib/ratio.cc')
| -rw-r--r-- | src/lib/ratio.cc | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/src/lib/ratio.cc b/src/lib/ratio.cc index 717c32458..e2baeac63 100644 --- a/src/lib/ratio.cc +++ b/src/lib/ratio.cc @@ -39,7 +39,6 @@ vector<Ratio> Ratio::_ratios; void Ratio::setup_ratios() { - /* This must only be called once as we rely on the addresses of objects in _ratios staying the same */ DCPOMATIC_ASSERT(_ratios.empty()); _ratios.push_back(Ratio(float(1290) / 1080, "119", _("1.19"), {}, "119")); @@ -59,19 +58,15 @@ Ratio::setup_ratios() } -vector<Ratio const *> +vector<Ratio> Ratio::all() { - vector<Ratio const *> pointers; - for (Ratio& ratio: _ratios) { - pointers.push_back(&ratio); - } - return pointers; + return _ratios; } -Ratio const * -Ratio::from_id(string i) +optional<Ratio> +Ratio::from_id_if_exists(string i) { /* We removed the ratio with id 137; replace it with 138 */ if (i == "137") { @@ -84,15 +79,24 @@ Ratio::from_id(string i) } if (j == _ratios.end()) { - return nullptr; + return {}; } - return &(*j); + return *j; } -/** @return Ratio corresponding to a given fractional ratio (+/- 0.01), or 0 */ -Ratio const * +Ratio +Ratio::from_id(string id) +{ + auto ratio = Ratio::from_id_if_exists(id); + DCPOMATIC_ASSERT(ratio); + return *ratio; +} + + +/** @return Ratio corresponding to a given fractional ratio (+/- 0.01), or empty */ +optional<Ratio> Ratio::from_ratio(float r) { auto j = _ratios.begin(); @@ -101,14 +105,14 @@ Ratio::from_ratio(float r) } if (j == _ratios.end()) { - return nullptr; + return {}; } - return &(*j); + return *j; } -Ratio const * +Ratio Ratio::nearest_from_ratio(float r) { vector<Ratio>::const_iterator nearest = _ratios.end(); @@ -124,10 +128,10 @@ Ratio::nearest_from_ratio(float r) DCPOMATIC_ASSERT(nearest != _ratios.end()); - return &(*nearest); + return *nearest; } -vector<Ratio const *> +vector<Ratio> Ratio::containers() { if (Config::instance()->allow_any_container()) { @@ -150,3 +154,17 @@ Ratio::container_nickname() const return *_container_nickname; } + + +bool +operator==(Ratio const& a, Ratio const& b) +{ + return a.id() == b.id(); +} + + +bool +operator!=(Ratio const& a, Ratio const& b) +{ + return a.id() != b.id(); +} |
