Object's attribute state stored into a dictionary


    May 30 2012 | 8:21 am
    HI there,
    I am in need of storing an object attribute into a dictionary. No big deal. The dictionary API provides functions to do just that. However I noticed that the attribute in question will always be cloned, meaning that now I have to manage two instances of the same attribute. Definetely not what I want. Ideally I would like to store in the dictionary a reference to an attribute, so that I will only need to manage one instance.
    Basically, I am trying to save an object's state into a dictionary so that it could be passed around by the dictobj system. However I don't want to manage two copies of the same state.
    The code I have is something like:
    long argc = 0;
    t_symbol **argv = NULL;
    
    object_attr_getnames(x, &argc, &argv);
    if (argc && argv) {
        symbolarray_sort(argc, argv);
        for (i = 0; i < argc; i++) {
            dictionary_appendattribute(d, argv[i], argv[i], (t_object *)x);
        }
        sysmem_freeptr(argv);
    }
    This works but then I will have to manage both the object's state [created by defining attributes in the object's main() function] AND the state saved in the dictionary. I want the object's state saved in the dictionary to directly reference the object's attributes created in main().
    To that end I also tried the following:
    long argc = 0;
    t_symbol **argv = NULL;
    t_object *attr;
    
    object_attr_getnames(x, &argc, &argv);
    if (argc && argv) {
        symbolarray_sort(argc, argv);
        for (i = 0; i < argc; i++) {
            attr = object_attr_get(x, argv[i]);
            dictionary_appendobject_flags(d, argv[i], attr, OBJ_FLAG_REF);
        }
        sysmem_freeptr(argv);
    }
    However it doesn't produce the desired outcome either...
    Is my goal possible to achieve, or am I just going in the wrong direction? Any suggestion is highly appreciated.
    Thanks a lot.
    - Luigi

    • Jun 18 2012 | 4:53 pm
      Hi Luigi,
      Trying to pass around references like this gets extremely complex very quickly and is an invitation to trouble. Especially when the attribute's owner is freed or subject to undo/redo events, etc.
      Using copies is certainly more robust, even if it involves some work to keep the copies in sync (using object_notify() or similar). Sorry to not offer a magic bullet for this scenario.
      best, Tim