cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello_computed_value.h
1/*
2 Copyright (c) 2025 Brett g Porter
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in all
10 copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 SOFTWARE.
18*/
19
20#pragma once
21
22#include "cello_object.h"
23#include "cello_value.h"
24
25namespace cello
26{
27
56template <typename T> class ComputedValue : public ValueBase
57{
58public:
62 using SetImplFn = std::function<void (const T&)>;
63 using GetImplFn = std::function<T ()>;
64
65 ComputedValue (Object& object, const juce::Identifier& id, GetImplFn getImpl = nullptr, SetImplFn setImpl = nullptr)
66 : ValueBase { id }
67 , getImpl { getImpl }
68 , setImpl { setImpl }
69 , object { object }
70 {
71 }
72
82 ComputedValue& operator= (const T& val)
83 {
84 set (val);
85 return *this;
86 }
87
97 void set (const T& val)
98 {
99 if (setImpl != nullptr)
100 setImpl (val);
101 else
102 jassertfalse;
103 }
104
110 operator T () const { return get (); }
111
118 T get () const
119 {
120 if (getImpl != nullptr)
121 return getImpl ();
122 else
123 jassertfalse;
124 return {};
125 }
126
127 GetImplFn getImpl;
128 SetImplFn setImpl;
129
130private:
131 Object& object;
132};
133
134} // namespace cello
135
145// clang-format off
146#define MAKE_COMPUTED_VALUE_MEMBER(type, name, ...) \
147 static const inline juce::Identifier name##Id { #name }; \
148 cello::ComputedValue<type> name { *this, name##Id, __VA_ARGS__ };
149// clang-format on
T get() const
Get the current computed value.
Definition cello_computed_value.h:118
void set(const T &val)
Set the value of the computed value.
Definition cello_computed_value.h:97
std::function< void(const T &)> SetImplFn
The type of the function that will be called to set the value of the computed value.
Definition cello_computed_value.h:62
ComputedValue & operator=(const T &val)
Assignment operator for the computed value.
Definition cello_computed_value.h:82
Definition cello_object.h:34
const juce::Identifier id
identifier of this value/property.
Definition cello_value.h:54
ValueBase(const juce::Identifier &id_)
ctor is protected – you can't create an object of type ValueBase directly, this only exists so we hav...
Definition cello_value.h:47