Populate C++ model from external source in Qt - c++

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).

Related

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

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?

Tensorflow Modify OpKernelConstruction Attributes

In kernel operations, an OpKernelConstruction pointer is given to the OpKernel constructor, for example
I would like to change some attributes in the context before passing it to the parent constructor.
For example, something like
explicit StreamQueueOp(OpKernelConstruction* context) :
TypedQueueOp(private_modifying_method(context))
But it appears in the description of OpKernelConstruction, that no modifying method exists. Is there a way to modify it?
EDIT:
I want to modify the attributes because I am attempting to make a FIFOQueue that pulls data from a database. I only want the user to have to specify the data stream id and then the queue would hit the database for the shape and type of the data. As such, I want to modify the context to add the shape and type info after I get it from the DB, but before I pass the context onto the base class. The base class makes use of shape and type, so I need to make sure it is in correspondence with the DB.

Firemonkey: How to iterate through all forms in the application using TScreen.Forms

I am attempting to iterate through the forms I have open in my application. I have found the documentation for FMX.Forms.TScreen.Forms, which looks like it can be used to accomplish my goal. However, I am confused on how I am supposed to use it.
At first I tried this in a function within my form's CPP file:
ShowMessage( Forms::TScreen::FormCount );
This produced the error 'Member TScreen::FormCount cannot be used without an object'
I figured that to mean that I need to attempt to access this property from my form, or from the global Application variable. I tried both
this->Forms...
Application->Forms...
and
this->TScreen...
Application->TScreen...
However, neither Forms nor TScreen exist within either of these objects.
How do I go about accessing Forms.TScreen.Forms?
The error gives you a clue:
Member TScreen::FormCount cannot be used without an object
TScreen is a class, not an object. FormCount is not a static member of the class, so you need an object instance of the TScreen class. And such an object is provided for you - the global Screen object:
ShowMessage( Screen->FormCount );
This is stated in the documentation:
FMX.Forms.TScreen
There is a global variable, Screen, of type TScreen, which is instantiated for use by any application with a GUI. Use Screen to obtain information about the current state of the screen in an application.
FMX.Forms.Screen
extern DELPHI_PACKAGE TScreen* Screen;
Here's what works well:
ShowMessage(Screen->FormCount);
Screen is a global object, like Application. The compiler said that FormCount is not static method or smth.

loading classes with jodd and using them in drools

I am working on a system that uses drools to evaluate certain objects. However, these objects can be of classes that are loaded at runtime using jodd. I am able to load a file fine using the following function:
public static void loadClassFile(File file) {
try {
// use Jodd ClassLoaderUtil to load class into the current ClassLoader
ClassLoaderUtil.defineClass(getBytesFromFile(file));
} catch (IOException e) {
exceptionLog(LOG_ERROR, getInstance(), e);
}
}
Now lets say I have created a class called Tire and loaded it using the function above. Is there a way I can use the Tire class in my rule file:
rule "Tire Operational"
when
$t: Tire(pressure == 30)
then
end
Right now if i try to add this rule i get an error saying unable to resolve ObjectType Tire. My assumption would be that I would somehow need to import Tire in the rule, but I'm not really sure how to do that.
Haven't use Drools since version 3, but will try to help anyway. When you load class this way (dynamically, in the run-time, no matter if you use e.g. Class.forName() or Jodd), loaded class name is simply not available to be explicitly used in the code. I believe we can simplify your problem with the following sudo-code, where you first load a class and then try to use its name:
defineClass('Tire.class');
Tire tire = new Tire();
This obviously doesn't work since Tire type is not available at compile time: compiler does not know what type you gonna load during the execution.
What would work is to have Tire implementing some interface (e.g. VehiclePart). So then you could use the following sudo-code:
Class tireClass = defineClass('Tire.class');
VehiclePart tire = tireClass.newInstance();
System.out.println(tire.getPartName()); // prints 'tire' for example
Then maybe you can build your Drools rules over the interface VehiclePart and getPartName() property.
Addendum
Above make sense only when interface covers all the properties of dynamically loaded class. In most cases, this is not a valid solution: dynamically loaded classes simply do not share properties. So, here is another approach.
Instead of using explicit class loading, this problem can be solved by 'extending' the classloader class path. Be warn, this is a hack!
In Jodd, there is method: ClassLoaderUtil.addFileToClassPath() that can add a file or a path to the classloader in the runtime. So here are the steps that worked for me:
1) Put all dynamically created classes into some root folder, with the respect of their packages. For example, lets say we want to use a jodd.samples.TestBean class, that has two properties: number (int) and a value (string). We then need to put it this class into the root/jodd/samples folder.
2) After building all dynamic classes, extend the classloaders path:
ClassLoaderUtil.addFileToClassPath("root", ClassLoader.getSystemClassLoader());
3) load class and create it before creating KnowledgeBuilder:
Class testBeanClass = Class.forName("jodd.samples.TestBean");
Object testBean = testBeanClass.newInstance();
4) At this point you can use BeanUtils (from Jodd, for example:) to manipulate properties of the testBean instance
5) Create Drools stuff and add insert testBean into session:
knowledgeSession.insert(testBean);
6) Use it in rule file:
import jodd.samples.TestBean;
rule "xxx"
when
$t: TestBean(number == 173)
then
System.out.println("!!!");
end
This worked for me. Note that on step #2 you can try using different classloader, but you might need it to pass it to the KnowledgeBuilderFactory via KnowledgeBuilderConfiguration (i.e. PackageBuilderConfiguration).
Another solution
Another solution is to simply copy all object properties to a map, and deal with the map in the rules files. So you can use something like this at step #4:
Map map = new HashMap();
BeanTool.copy(testBean, map);
and later (step #5) add a map to Drools context instead of the bean instance. In this case it would be even better to use defineClass() method to explicitly define each class.

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)