0

I have a startup class as such which sets up a SystemControllerin the construct:

m_systemController(new SystemController(this,
                                                 Provider::getSettingsAsSingleton())),

Essentially this satisfies:

public:
        explicit SystemController(QObject *parent,
                                  Settings& config);

I've recently though wanted to use this class in a threaded class, which doesn't like me passing me the SystemController as the threaded class doesn't have a parent. I tried passing it as such:

public:
explicit DataTestWorker(QObject *parent=0);//,
                                //SystemController &sysCtrl); //Obviously in my setup, it would complain about this

Where the DataTestWorker class is in fact the threaded class, initialised in a DataTest class. The reason I want to pass the memory location of the SystemController, is the Controller class has important data already setup in the class, which I wish to access without having to run all my initialisation methods within it again.

Is there a way to somehow pass it some sort of "shared" pointer - in which not only this class - but other classes can access it. The only way around this I can think of is I could use signals and slots between the DataTest and the DataTestWorker class to manipulate the values in the SystemController, but this would be long and verbose.

Any ideas? If you need anymore information, just ask.

fiz
  • 906
  • 3
  • 14
  • 38
  • don't use singletons. Bad idea in 99.9% of cases. – UmNyobe Sep 29 '14 at 08:38
  • @UmNyobe - any ideas on what to do then? – fiz Sep 29 '14 at 08:39
  • `I've recently though wanted to use this class in a threaded class, which doesn't like me passing me the SystemController as the threaded class doesn't have a parent` This line is quite hard to understand – UmNyobe Sep 29 '14 at 08:40
  • The `DataTestWorker `class does not have a parent. If I pass the `SystemController` into it, the compiler complains that I cannot pass it the address of the `SystemController` as `DataTestWorker` has no parent. It works if I create a new instance of `SystemController` in `DataTestWorker` but that defeats the purpose of what I'm trying to do. Is that clear? – fiz Sep 29 '14 at 08:45
  • So how would you do it - if you wanted just one instance of the `Settings` class and just one instance of the `SystemController` ? I'm reading about shared pointers at the moment - but I'm unsure if this is the right direction. – fiz Sep 29 '14 at 08:46

2 Answers2

0

You can shape your SystemController to Singleton and put QMutex guards to access SystemController's private data used in concurrent threads.

Community
  • 1
  • 1
Max Go
  • 2,092
  • 1
  • 16
  • 26
  • I'll try this again, but I remember when I did try it, the compiler threw up an error because it used the `Settings` singleton in its constructor. I'll give it another go. I was just curious to see if there was an alternate/better method than that – fiz Sep 29 '14 at 08:18
0

Is It Possible to Use a Shared Pointer for the Parent of a QObject?

No. There is no shared pointer which is a subclass of QObject. And I don't see why it will make sense.

The reason I want to pass the memory location of the SystemController, is the Controller class has important data already setup in the class, which I wish to access without having to run all my initialisation methods within it again.

Why don't you separate the notion of SystemController settings into another POD class\struct. Then you can have either

class SystemController : public QObject, public SystemSettings

or

class SystemController : public QObject
{
   SystemSettings m_settings;
   SystemSettings systemSettings() {m_settings};

}

and play around with the settings as you wish.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Actually I think I just realised what you meant. You mean I should create a SystemController settings class which would hold my variables in a simple POD? – fiz Sep 29 '14 at 09:10
  • 1
    yes, a class just to regroup thoses variables. No signals. no slots. no complex ***logic***. this `SystemSettings` class could be sent in a signal by SystemController with the second design choice. – UmNyobe Sep 29 '14 at 09:11
  • @UmNyobe, I think you should add "SystemSettings class could be sent in a signal by SystemController" to your answer – Max Go Sep 29 '14 at 09:53