cesium-native 0.52.0
Loading...
Searching...
No Matches
DerivedValue.h
1#pragma once
2
3#include <CesiumUtility/Assert.h>
4#include <CesiumUtility/Library.h>
5
6#include <optional>
7#include <type_traits>
8
9namespace CesiumUtility {
10
25template <typename TInput, typename TDerivation>
26class CESIUMUTILITY_API DerivedValue {
27public:
31 using TOutput = std::invoke_result_t<TDerivation, TInput>;
32
37 DerivedValue(const TDerivation& derivation)
38 : _derivation(derivation), _lastInput(), _lastOutput() {}
39
44 DerivedValue(TDerivation&& derivation)
45 : _derivation(std::move(derivation)), _lastInput(), _lastOutput() {}
46
57 template <typename T> TOutput operator()(T&& input) {
58 if (input != this->_lastInput) {
59 this->_lastInput.emplace(std::forward<T>(input));
60 this->_lastOutput.emplace(this->_derivation(*this->_lastInput));
61 }
62
63 CESIUM_ASSERT(this->_lastOutput);
64 return *this->_lastOutput;
65 }
66
67private:
68 TDerivation _derivation;
69 std::optional<TInput> _lastInput;
70 std::optional<TOutput> _lastOutput;
71};
72
78template <typename TInput, typename TDerivation>
79auto makeDerivedValue(TDerivation&& derivation)
81 static_assert(
82 std::is_invocable_r_v<
83 std::invoke_result_t<std::decay_t<TDerivation>, TInput>,
84 std::decay_t<TDerivation>,
85 TInput>,
86 "Callable provided to makeDerivedValue is not invocable with the "
87 "specified TInput.");
89 std::forward<TDerivation>(derivation));
90}
91
92} // namespace CesiumUtility
A function object that caches the last result of a derivation function based on its input....
TOutput operator()(T &&input)
Gets or computes the derived value.
DerivedValue(TDerivation &&derivation)
Constructs a new instance that will use the given derivation function.
std::invoke_result_t< TDerivation, TInput > TOutput
The type returned by the derivation function.
DerivedValue(const TDerivation &derivation)
Constructs a new instance that will use the given derivation function.
Utility classes for Cesium.
auto makeDerivedValue(TDerivation &&derivation) -> DerivedValue< TInput, std::decay_t< TDerivation > >
Helper factory to construct a DerivedValue while specifying only the input type explicitly and lettin...
STL namespace.