cancel
Showing results for 
Search instead for 
Did you mean: 

Access modelListener and model in a singletone class

nico23
Senior III

I have created a singletone class CommonUtils.cpp because I needed various functions to be used in various part of the code.

It is a very simple class like:

.cpp

#include <gui/common/CommonUtils.hpp>

CommonUtils& CommonUtils::getInstance()
{
    static CommonUtils instance;
    return instance;
}

.hpp

 

#ifndef COMMONUTILS_HPP
#define COMMONUTILS_HPP

#include <touchgfx/hal/Types.hpp>
#include <touchgfx/Unicode.hpp>
#include <gui/model/ModelListener.hpp>

class CommonUtils : public ModelListener
{
public:
    // Get singleton instance
    static CommonUtils& getInstance();
    
    // Delete copy constructor and assignment operator
    CommonUtils(const CommonUtils&) = delete;
    CommonUtils& operator=(const CommonUtils&) = delete;
private:
    // Private constructor
    CommonUtils() = default;
    ~CommonUtils() = default;

protected:
    Model* model = nullptr;
};

The class has various public function that can be call in various part of the code with 

 CommonUtils::getInstance().myFunction();

As you may have seen from the code, I have correctly linked the model to my singleton class adding the bind function and in the FrontedApplication.cpp

#include <gui/common/FrontendApplication.hpp>
#include <gui/common/CommonUtils.hpp>

FrontendApplication::FrontendApplication(Model& m, FrontendHeap& heap)
    : FrontendApplicationBase(m, heap)
{
    CommonUtils::getInstance().bindModel(&m);
}

In this why I can access the model->functionInModel 

Now, I would like to be able to access the modelListener as well on my singletone class.

Is there a way to do that?

0 REPLIES 0