# ValueTree-backed objects and ComponentBuilder

**URL:** <https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071>\
**Category:** General JUCE discussion\
**Created:** [January 22, 2018, 5:34pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071 "2018-01-22T17:34:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [January 22, 2018, 5:34pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/1 "2018-01-22T17:34:11Z")

</div>

This is a question related to @drowaudio’s [ADC 2017 talk on `ValueTree`-backed apps.](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&cad=rja&uact=8&ved=0ahUKEwjZoqWqjezYAhUNNKwKHRx2BH8QtwIISzAF&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3IaMjH5lBEY&usg=AOvVaw00K8H5PuVEj7MwvCMx1xCb)

I’m starting work on a mid/large sized audio app for my final-final university project, and plan to use a giant `ValueTree` as my model/controller to store the entire app state and generate the view (as detailed in the talk).

My big question is, how does `ComponentBuilder` fit into the realm of the talk? It seems like it does _a lot_ of the same things as the `ValueTreeItem` class in the talk’s example code, with slightly less control since it doesn’t use direct `ValueTree::Listener` callbacks, instead using the `updateComponentFromState` method(?).

Any enlightenment on how these pieces fit together or a more concrete example of when to use and when not to use `ComponentBuilder` would be much appreciated.

EDIT: Doh, just ran across [Is anyone using the ComponentBuilder class?](https://forum.juce.com/t/is-anyone-using-the-componentbuilder-class/24712)

Guess I’ll make my own `ValueTree` management classes then - which makes sense I suppose because not every `ValueTree` backed thing will necessarily be a `Component` - sometimes you just want a `ValueTree` backed object for interfacing with the `ValueTree`.

---

<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:** [January 22, 2018, 5:51pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/2 "2018-01-22T17:51:16Z")

</div>

To be honest I would steer clear of the `ComponentBuilder`. It’s more hassle than it’s worth as you’ll spend your whole time writing property binders and actions. Using a ValueTreeObjectList is far more flexible and probably quicker in the end.

---

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [January 22, 2018, 5:58pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/3 "2018-01-22T17:58:55Z")

</div>

Is `ValueTreeObjectList` some internal Tracktion type or something? I don’t see reference to it anywhere…

---

<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:** [January 22, 2018, 6:22pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/4 "2018-01-22T18:22:31Z")

</div>

We developed it at Tracktion but there’s a version on my GitHub: [https://github.com/drowaudio/presentations/blob/master/ADC%202017%20-%20Using%20JUCE%20ValueTrees%20and%20Modern%20C%2B%2B%20to%20Build%20Large%20Scale%20Applications/Examples/shared/drow\_ValueTreeObjectList.h](https://github.com/drowaudio/presentations/blob/master/ADC%202017%20-%20Using%20JUCE%20ValueTrees%20and%20Modern%20C%2B%2B%20to%20Build%20Large%20Scale%20Applications/Examples/shared/drow_ValueTreeObjectList.h)

The main reason it never made it in to JUCE is that there’s several potential versions you might want of it. The one I’ve posted is probably the most generic, it gives you full control over the object lifetime (so you can use reference counting if you like) and has callbacks for objects being added/removed. I can see use cases for automatically managed lists (i.e. an `OwnedArray<ObjectType>` or `std::vector<std::unique_ptr<ObjectType>>` backed version) and even value typed lists (i.e. `Array<ObjectType>` or `std::vector<ObjectType>`). Then there’s the level of thread safety you want to involve etc.

It’s also fairly lightweight so easily copyable for the projects you need it in.

I’d just copy the one on my GitHub perhaps adapting it to suit your own needs.

---

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [January 22, 2018, 7:13pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/5 "2018-01-22T19:13:53Z")

</div>

Wow cool! These give me a great starting point. I’m also taking a look at the actual Tracktion project format for additional inspiration/guidance on how to structure things. Looking forward to sharing my code once it’s done.

---

<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:** [January 22, 2018, 8:15pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/6 "2018-01-22T20:15:35Z")

</div>

Good stuff. Don’t take the Tracktion project format as the absolute best way to do things. There’s some legacy support in there (that format has largely been around for over 10 years, way before ValueTree existed).

---

<div class="post-metadata">

**Author:** ![mchagneux](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mchagneux](https://forum.juce.com/u/mchagneux)\
**Post date:** [January 29, 2022, 1:06pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/7 "2022-01-29T13:06:32Z")

</div>

Hi, I was wondering if what was said back then is still true today ? Like, is the ValueTreeObjectList still a better way to deal with that kind of stuff ? I assume it is since I haven’t found indications of a major update to the ComponentBuilder or anything related in JUCE release notes (between Jan 2018 - now).

I am just starting with JUCE so trying to go for the best / up-to-date practices, and there’s a portion of my app for which the ValueTreeObjectList seemed completely relevant so I used that and ignored the ComponentBuilder.

---

<div class="post-metadata">

**Author:** ![LiamG](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/liamg/32/6844_2.png) [@LiamG](https://forum.juce.com/u/LiamG)\
**Post date:** [January 29, 2022, 1:16pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/8 "2022-01-29T13:16:06Z")

</div>

To the best of my knowledge, this is still very much up-to-date advice. `ValueTree` is very useful and very popular, and building wrappers around `ValueTree` is, IMO, the best way to use them.

As Dave said, it might be best to take a look at how his `ValueTreeObjectList` works and then build your own. IIRC, his original class handles its own resources manually, including calling `delete`, and it’s probably better to use smart pointers.

My experience has been that it’s best to use one of juce’s own Array classes to handle the resources instead of `std::vector`. This is because `ValueTree::Listener` functions will usually give you indices, and you can map these directly onto the `juce::Array` classes very easily. If you use `std::vector`, you have to mess around with iterators, which in this case is a bit more tedious.

So the main options are  
`juce::OwnedArray<MyClass>`  
`juce::ReferenceCountedArray<MyClass>`  
`juce::Array<std::unique_ptr<MyClass>>`  
`juce::Array<std::shared_ptr<MyClass>>`

personally I prefer the last two, but it really depends on your use case.

---

<div class="post-metadata">

**Author:** ![mchagneux](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mchagneux](https://forum.juce.com/u/mchagneux)\
**Post date:** [January 29, 2022, 11:14pm UTC](https://forum.juce.com/t/valuetree-backed-objects-and-componentbuilder/26071/9 "2022-01-29T23:14:10Z")

</div>

Alright, thanks a lot, super clear !
