C++ design question (using Model Presenter View with protected member) - c++

I use touchGFX which is a C++ hmi for embedded devcices. I have the following problem and I am not sure how to solve it most effective. I need to access functions in the various StatusBar instances from MyModelListener (see below).
System description:
There is a model which holds the data, the views for the hmi and some presenters which interact between the model and the views. The base classes are automatically generated, I am allowed to modify all class with "My...". All My...Pesenters are linked with the corresponding My...Views by references in both directions.
I have some Presenters:
class MyAPresenter : public MyModelListener;
...
class MyZPresenter : public MyModelListener;
I have some Views belonging to the presenters:
class MyAView: public AViewBase;
...
class MyZView: public ZViewBase;
There is a status bar which should be used in each of the views:
class MyStatusbar : public Statusbar;
AViewBase, ..., ZViewBase each have a protected member instance of class MyStatusbar.
MyAPresenter, ..., MyZPresenter have a reference to MyAView, ..., MyZView.
How can I access (many) functions of MyStatusBar instances (inside the MyAView...) from MyModelListener without introducing as many functions into each MyAView... MyZView?
The data path how status bar can be reached is:
MyModelListener -> MyAPresenter -> MyAView -> AViewBase -> MyStatusbar
Problems:
I cannot access MyStatusbar instances from MyModelListener or MyAView via the AViewBase because MyStatusbar instances are declared protected within AViewBase.
I cannot modifiy AViewBase
I do not want to access MyStatusBar from MyAView..MyZView because I do not want to have all those many functions in each My...View.
I do not want to write all the needed functions in each of MyAPresenter...MyZPresenter.
(All the classes are virtual)
My solution:
Make functions getStatusPtr() returning the address of the Statusbar instances in each of MyAView to MyZView. This is possible because StatusBar is defined protected but not private.
Make virtual getStatusPtr() in each of MyAPresenter to MyZPresenter which calls the getStatusPtr() function in MyAView to MyZView via the owned references.
Define a virtual getStatusPtr() returning 0 in MyModelListener.
Access the StatusBar from MyModelListener if the pointer is not 0. This should deliver the the status bar of A if the MyModelListener functions are called from a MyAPresenter object.
If the View is changed both view and presenter are destroyed and the status bar too. The statusbar thus has to be rebuilt but this is acceptable.
Is there a better/alternative solution?

Related

How to add an AudioSource based class to a MixerAudioSource

I’ve created a class for managing the audio of my synth:
class midiSource : public juce::AudioSource
I would like to add it to my mixer. However, the MixerAudioSource class accepts only AudioSource as input. How can I pass midiSource as an input source to my mixer?
(e.g.: mixer.addInputSource(mySource, false);)
Since juce::MixerAudioSource::addInputSource() expects a pointer to a juce::AudioSource, or a subclass of juce::AudioSource, you can just pass &mySource to addInputSource().

Populate C++ model from external source in Qt

I need to populate c++ model from a external source (for example USB). I have registered the model in main.cpp like this,
qmlRegisterType<HardwareDataModel>("org.example", 1, 0, "HardwareDataModel");
I need to change the private member of this HardwareDataModel class from the external source. Because I would like to change the the private data member of this model and fire signal to qml to change the UI. I got stuck how to proceed as I do not know how to populate those private data members. Do I need to instatiate HardwareDataModel in main.cpp? If yes, how to connect the instatiated HardwareDataModel object and one that is qmlRegisteredType (one shown above).

Set check of CMFCRibbonCheckBox

The MFC CMFCRibbonCheckBox class only supports IsChecked().
How do I change check status of ribbon check box in program code?
Is there way to set check status like CButton::SetCheck().
you can copy code from CMFCRibbonCmdUI::SetCheck. CMFCRibbonBaseElement::m_bIsPressed is a protected member so you need to expose it via a derived class. CMFCRibbonCmdUI has a backdoor to it through class friendship.

Should instance fields access be synchronized in a Tapestry page or component?

If a page or component class has one instance field which is a non-synchronized object, f.ex. an ArrayList, and the application has code that structurally modifies this field, should the access to this field be synchronized ?
F.ex.:
public class MyPageOrComponent
{
#Persist
private List<String> myList;
void setupRender()
{
if (this.myList == null)
{
this.myList = new ArrayList<>();
}
}
void afterRender(MarkupWriter writer)
{
// Should this be synchronized ?
if (someCondition)
{
this.myList.add(something);
}
else
{
this.myList.remove(something);
}
}
}
I'm asking because I seem to understand that Tapestry creates only one instance of a page or component class and it uses this instance for all the connected clients (but please correct me if this is not true).
In short the answer is no, you don't have to because Tapestry does this for you. Tapestry will transform your pages and classes for you at runtime in such a way that wherever you interact with your fields, they will not actually be working on the instance variable but on a managed variable that is thread safe. The full inner workings are beyond me, but a brief reference to the transformation can be found here.
One warning, don't instantiate your page/component variables at decleration. I have seen some strange behaviour around this. So don't do this:
private List<String> myList = new ArrayList<String>;
Tapestry uses some runtime byte code magic to transform your pages and components. Pages and components are singletons but the properties are transformed so that they are backed by a PerThreadValue. This means that each request gets it's own copy of the value so no synchronization is required.
As suggested by #joostschouten you should never initialize a mutable property in the field declaration. The strange behaviour he discusses is caused beacause this will be shared by all requests (since the initializer is only fired once for the page/component singleton). Mutable fields should instead be initialized in a render method (eg #SetupRender)

Can Django pre_save signal work for all derived classes

I have a model class "Action" that get's extended by several other classes. I am new to django and assumed that if I called pre_save.connect(actionFunc, sender=Action) that actionFunc would get called anytime the save method in the Action class was called (including by any derived class).
My observation is that this function only gets triggered when the instance is a direct match of the Class type defined in Sender. Is there anyway to get this to recieve signals for all derived instances of Action as well?
No, you have to call the pre_save.connect as many number of times.
However, you can use python to get all the classes that extend the class of your interest, and loop over the pre_save connect statement.
Say, if the extended classes of the Action are all in a given file, you can do the following:
global_dict = globals().copy()
[el for el in global_dict.values() if getattr(el,'__base__',None)==Action]
one thing you can do is modify the signal sender in django so that instead of matching against a specific type it instead does
if isinstance(sender, filter):
send_signal()
(pseudocode)