Best practice passing PropertiesFile pointer around Components


#1

I've just started using the PropertiesFile class in my app, and want to share settings among components. 

I am thinking of warpping it in a class that inherits ReferenceCountedObject so can safely pass a pointer around.

I'm planning to initialise it in MainContentComponent and then pass the pointer along as a constructor argument when subcomponents are created. This is how I'm currently sharing other objects across components. 

Is this good practice or is there a more suitable method? 

 


#2

if you don't mind c++11, my advice is to use std::shared_ptr


#3

Personally, for a singleton type object like a PropertiesFile I would use a SharedResourcePointer. Just keep one alive in your JUCEApplication subclass and then create one whenever you need it. This saves storing and passing reference counted pointers around. 


#4

Thanks guys will check out the shared pointers... dont mind C++11 at all its got some nice neat stuff !


#5

For anyone else wanting to do this, I've just found the code in Introjucer that allows Components and other classes to reference the main app class:

 

class MyApp : public JUCEApplication

{

...

    static MyApp& getApp()

    {

        MyApp* const app = dynamic_cast<MyApp*> (JUCEApplication::getInstance());

        jassert (app != nullptr);

        return *app;

    }

...

}

 


#6

I also reorganised the code like the Introjucer, putting the contents of Main.cpp into Application.h, apart from the last line which is then the only thing in Main.cpp:

#include "Application.h"

START_JUCE_APPLICATION (MyApp)

This allows including the header in other classes so they can access the shared pointers in the JUCEApplication instance. 

I think it would be good to have a project template like this as I guess its a common pattern... as soon as you want multiple views/components sharing data, having everything going on in MainContentComponent doesnt make sense..