The use of DymanicObject


#1

Hi all,

I am expreriencing a problem with the following code snippet

DynamicObject obj;

DynamicObject* obj1 = new DynamicObject;
obj1->setProperty(“Name”,“George”);
obj1->setProperty(“Birth_Day”,“17-05-1900”);
obj.setProperty(“George”,Obj1);

Obj1->clear();
obj1->setProperty(“Name”,“John”);
obj1->setProperty(“Birth_Day”,“10-03-1983”);
obj.setProperty(“John”,Obj1);

//…

var var1 (obj->getProperty(Identifier(“George”)));
String nam = var1.getProperty(“Name”, String(“Not Set”));
String bd = var1.getProperty(“Birth_Day”, String(“Not Set”));

It seems that the var1 object points to the second node of obj object.
The “nam” variable contains “John” while it should contain “George”.
Probably, I misunderstood something but I can’t figure out what’s that;

Any help will be appreciated .
Thanks in advance

George


#2

I assume the capital Obj1 is a copy mistake meaning obj1, otherwise I must ask, what Obj1 is and where it is declared…

The DynamicObject is a ReferenceCountedObject, so I think after assigning obj.setProperty("George",obj1); the obj1 still references the property “George”. So the obj1->clear() gets rid of the Name => George etc. properties and adds new properties “Name” => “John”.


#3

You’re setting the value as a pointer to obj1 – try *obj1

obj.setProperty (“John”, *obj1);

and obj1 probably needs to be a ScopedPointer.

Rail


#4

I don’t think this would work:
The setProperty needs to be able to implicitly cast the obj1 to a var. It uses that operator var& var::operator= (ReferenceCountedObject* object), as DynamicObject inherits ReferenceCountedObject.

obj1 inherits a ReferenceCountedObject. A ScopedPointer to that would probably try to delete it. I think that will cause problems.

I usually populate DynamicObjects like this, but I am interested in more elegant versions:

    DynamicObject obj;

    obj.setProperty ("George", new DynamicObject());
    if (DynamicObject* george = obj.getProperty("George").getDynamicObject()) {
        george->setProperty("Name","George");
        george->setProperty("Birth_Day","17-05-1900");
    }

    obj.setProperty ("John", new DynamicObject());
    if (DynamicObject* john = obj.getProperty("John").getDynamicObject()) {
        john->setProperty("Name","John");
        john->setProperty("Birth_Day","10-03-1983");
    }

    //.......

    var var1 (obj.getProperty(Identifier("George")));
    String nam = var1.getProperty("Name", String("Not Set"));
    String bd = var1.getProperty("Birth_Day", String("Not Set"));

And on a personal note, I made it a habit never to copy a string literal, because it is hard to find errors. You can let the compiler find them by creating a static Identifier, I usually define inside the class:

class MyClass {
public:
    static Identifier paramName;
// ...
};

// and on top of your cpp
Identifier MyClass::paramName ("Name");

// so you can use it
george->setProperty(paramName,"George");

#5

Ok, I’ll try this code and I’ll post the results.
I feel that this is the right way. .

No. I never use string literals.
I always define const static variables either with #define directives or with their type.

Thanks alot for your time and your very good explanation.

George


#6

Ok, I just finished the debugging of my app and everything works fine.

Well, what I have now realized about the use of DynamicObject seems as follows:

  • First of all we must create a “node” inside the DynamicObject giving it an Identifier . This “node” is created as a var object.
  • After that, we can attach to this “node” many properties as we want
  • If we need to export a “node” we can do it as a var object, using the “node” Identfier.

I think this is an understandable scheme to work on.

Thanks a lot for the attention

George