ValueTree is not static friendly

Since the JUCEApplication itself is a singleton, it is a perfect place to store application wide data with a deterministic lifetime. Just add a static function to your application. For that purpose it is advisable to separate it in a MyApplication.cpp/MyApplication.h to simplify includes:

class MyApplication : public JUCEApplication
{
public:
    void initialise (/* yada yada */)
    {
        // restore settings
    }
    ValueTree& getSettings()
    {
        return settings;
    }

private:
    ValueTree settings { "Settings" };
};

namespace Settings
{
    static ValueTree& getSettings()
    {
        auto* myApplication = dynamic_cast<MyApplication*>(JUCEApplication::getInstance());
        jassert (myApplication);
        return myApplications->getSettings();
    }
}

// anywhere now:
Settings::getSettings().getProperty (/* yada yada */);

Simple, secure, no additional statics :wink:

2 Likes