Is there any way to convert a parent object to one of its child objects such that the object retains the same old information (including the old key). I have implemented concrete inheritance. Thanks.
Related
Consider two EObjects, both created through some factory method
EObject parent = instantiateAParent();
EObject child = instantiateAChild();
I want child's parent to be parent. Is this possible to do this without relying on the exact type of child and parent? If yes, how?
Yes this is possible. What you want to do is the following:
Retrieve the type (in the Ecore sense) of the parent (parent.eClass())
Find all its EReferences with isContainment to true (eClass.getEAllContainments())
Among these containment references, you want to find at least one whose type is compatible with the type of child (eReference.getEReferenceType().isInstance(child))
Since you may have several containment references at this point, you may use any algorithm to select one (maybe asking the user, etc.)
Set the value of the reference you chose (parent.eSet(eReference, child)). Note that if the containment reference you chose is single-valued, this may have a significant impact on your model.
Is on_delete=models.CASCADE representing composition and models.PROTECT aggregation?
Since one of the main differences between aggregation and composition is that with composition if the composed class is deleted, all the objects of the componing classes are delated by consequence, and since this doesn't happen with aggregation, is the statement above correct?
Both CASCADE and PROTECT represent composition, since both imply that the child object cannot exist if the parent object is deleted. PROTECT prevents you from deleting the parent if a child object exists and as you said CASCADE will delete all child objects
SET_NULL, SET_DEFAULT and SET represent aggregation since in each case the "child" model object can continue existing when the parent is deleted
So I have this class (the parent) that creates a dialog box and fills it with controls. Within that class is another class (the child) that creates a timer that links/passes a function call to an event system to update the contents of some of the controls at regular intervals. The problem comes from the timer update function needing to know the objects it needs to update.
I would like to simply pass the pointer of the base class to the child some how in a way that it's all self contained. But I can't figure out a way to make that happen. Anyone have any ideas?
I've searched for answers but all I got was accessing variables from inside the base class from within the child class. That would work but it seems unnecessary to fire an event for every individual control... (I guess I could create all the controls in an structure but that seems unnecessary)
the nested classes look a bit like this:
class CreateDialog{
class timer{
}
}
you can try to declare a static member variable in the parent class and set it to the pointer of parent within the constructor of parent class.
I'm developing a game for a course at my school. One of the assignments is to enable saving the game to a file and later load the game from the same file.
The problem I'm having are pointers. I'm not allocating anything on the stack (due to ownership of an item for example) so all the classes have pointers to whatever they want a reference to.
While this is quite easy to solve (assign an ID to each object and store that id instead of the pointer) the real problem comes with multiple inheritance.
Let's take the class Actor for example. It looks like this: class Actor : public Object, public ItemOwner where Object is a Obj-C-style base class which has a retain count and release, retain and autorelease methods. ItemOwner is simply an Interface which has some methods such as virtual bool add(Item *item) = 0;, virtual void remove(Item *item) = 0; and virtual bool transfer_ownership(Item *item, ItemOwner *new_owner) = 0;
Now the real question comes, which class(es?) should have ID's. And Item has a pointer to an ItemOwner while a Room has a pointer to an Actor.
The actor should only be saved once.
I've thought about assigning ID's to each superclass (Object, ItemOwner, etc) but if I have a pointer to an Actor will that actor always have the same adress as the Object it contains (Actor *foo = new Actor(); foo == (Object *)foo)? If not every single class in the game will need to have an ID.
Any thoughts or suggestions would be greatly appreciated.
By using UIDs, you create a mapping:
serialized_object -> UID -> deserialized_object.
Why bothering? You already have UIDs, and these are pointers to objects. By using this mapping:
&serialized_object -> &deserialized_object
you drop a level of indirection, UIDs as created automatically, and all you have to do is to deduce this mapping in deserealization process.
The simpliest way is to serialize the objects with all the pointers as is, and to store together with each object its address. This will also help you check if an object was serialized alraedy.
While serialization would be simple, deserialization will have its tricks. You'll have to be carefull to update each old pointer (remember? they contained adresses that are no more valid) with the correct object address.
One option is to define a class who's purpose in life is to hold an ID, and make it a virtual base class of every class you intend to save to disk.
I would put a single per object instance, for the actor, its ID should be the same for the Object and ItemOwner, because they are all the same instance.
Also, instead of using pointers you can think about using handlers, like described here: http://gamesfromwithin.com/managing-data-relationships
Sorry for the dumb question, but I'm working with Qt and C++ for the first time, and working through the tutorial and some samples.
One thing that was mentioned was that Qt stuff doesn't need to be explicitly deleted. So, main question, does this also apply to collections of Qt stuff? Like say I want a dynamic number of MyWidgets, so I keep a vector or whatever of them. Are they still taken care of for me?
As a side question, what is going on to make it so I don't have to worry about destructors?
The Qt memory management model is based upon a parent-child relationship. Qt classes take an optional parent as a parameter of their constructor. The new instance registers with this parent such that it is deleted when the parent is deleted. If you are using a Qt collection (e.g. QList), I believe you can set the list as the parent of its entries. If you're using an std::vector or other collection type, you will not get "automatic" memory management.
The Qt model makes a lot of sense in a UI hierarchy where it matches one-to-one with the UI hierarchy. In other cases, it doesn't always map as cleanly and you need to evaluate whether using the Qt system makes sense for the particular situation. The normal C++ tools still work: you can use std::tr1::shared_ptr or any of the other smart pointer classes to help you manage object lifetime. Qt also includes QPointer, a guarded pointer, and the QSharedPointer/QWeakPointer pair that implement a reference-couting smart pointer and weak-reference pair.
Qt has an interesting object model for sure. When I first started it made me uneasy that there were so many new Foo calls and no deletes.
http://qt.nokia.com/doc/4.6/object.html Is a good place to start reading up on the object model.
Things of interest:
QObject subclasses have their assignment and copy-ctor methods disabled. The chain of object child-parents is maintained internally by QObject.
Generally when instantiating a QObject subclass (if you don't plan on managing its pointer yourself) you will provide another QObject pointer as the parent. This 'parent' then takes over the management of the child you just made. You can call setParent() on a QObject to change who "owns" it. There are very few methods in Qt that will change the parent of an object, and they all explicitly state that they do in the docs.
So to answer your specific question: it depends on how you made all of your MyWidget instances.
If you made each one with a parent, then no you don't have to delete them. The parent will delete them when it gets deleted.
If you're keeping a QList<MyWidget*> collection of them, and you didn't give them a parent, then you should delete them yourself.