# CachedValue::getPropertyAsValue() doesn't take default into account

**URL:** <https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888>\
**Category:** General JUCE discussion\
**Created:** [December 14, 2018, 6:17pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888 "2018-12-14T18:17:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [December 14, 2018, 6:17pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/1 "2018-12-14T18:17:50Z")

</div>

`CachedValue::getPropertyAsValue()` doesn’t take default into account. Seems odd that `CachedValue::get()` and `CachedValue::getPropertyAsValue().getValue()` can return different values.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [December 14, 2018, 7:22pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/2 "2018-12-14T19:22:30Z")

</div>

Yeah, this is a bit of a pain.  
There’s two ways to deal with it, one is to flush the default to the state before calling `getPropertyAsValue`.

The other that I haven’t’ tried is creating a custom `ValueSource` that returns the default if the property is missing. That’s fairly verbose though and could be error prone if the default it changed etc.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [December 14, 2018, 7:23pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/3 "2018-12-14T19:23:31Z")

</div>

Maybe if `CachedValue` was `WeakReference`able we could get around that.

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [December 14, 2018, 7:31pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/4 "2018-12-14T19:31:54Z")

</div>

Like [ValueWithDefault](https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_data_structures/values/juce_ValueWithDefault.h)?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [December 14, 2018, 7:37pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/5 "2018-12-14T19:37:36Z")

</div>

Yeah but you’d still need to wrap that in a `ValueSource` to be able to attach it to a `Slider` or similar?

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [December 14, 2018, 8:05pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/6 "2018-12-14T20:05:08Z")

</div>

Yeah there are a few ValueSource implementations in the property components, like the RemapperValueSourceWithDefault [here](https://github.com/WeAreROLI/JUCE/blob/develop/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp).

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [December 15, 2018, 12:14pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/7 "2018-12-15T12:14:28Z")

</div>

Here’s two possible implementations:

```auto
struct ValueSourceWithDefault : public Value::ValueSource
{
    ValueSourceWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um, const var& defaultToUse)
        : value (tree, propertyID, um, defaultToUse)
    {}

    template<typename Type>
    ValueSourceWithDefault (const CachedValue<Type>& cachedValue, UndoManager* um)
        : ValueSourceWithDefault (cachedValue.getValueTree(), cachedValue.getPropertyID(), um, cachedValue.getDefault())
    {}

    var getValue() const override { return value.get(); }
    void setValue (const var& newValue) override { value = newValue; }

    ValueWithDefault value;
};

```

and

```auto
template<typename Type>
struct CachedValueSource : public Value::ValueSource
{
    CachedValueSource (ValueTree& tree, const Identifier& propertyID, UndoManager* um, const var& defaultToUse)
        : value (tree, propertyID, um, defaultToUse)
    {}

    CachedValueSource (const CachedValue<Type>& valueToUse, UndoManager* um)
        : value (valueToUse.getValueTree(), valueToUse.getPropertyID(), um, valueToUse.getDefault())
    {}

    var getValue() const override { return value.get(); }
    void setValue (const var& newValue) override { value = newValue; }

    CachedValue<Type> value;
};

```

Requests:

- Add a `CachedValue::getUndoManager()` method so we don’t have to pass this in the constructor. This seems like a simple omission.
- Add a `JUCE_DECLARE_WEAK_REFERENCEABLE` to `CachedValue` so I don’t have to keep a copy of the CachedValue which will save a bit of listener overhead and means if the source default changes we can update it

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [December 18, 2018, 2:25pm UTC](https://forum.juce.com/t/cachedvalue-getpropertyasvalue-doesnt-take-default-into-account/30888/8 "2018-12-18T14:25:47Z")

</div>

> [@dave96](#):
>
> Requests:

Sure, I’ll get these added.
