Question about QofIdType assertion in QofCollection

John Ralls jralls at ceridwen.us
Tue Dec 30 19:31:58 EST 2014


> On Dec 30, 2014, at 1:40 AM, Chenxiong Qi <qcxhome at gmail.com> wrote:
> 
> On Tue, Dec 30, 2014 at 1:08 AM, John Ralls <jralls at ceridwen.us> wrote:
>> 
>>> On Dec 29, 2014, at 7:32 AM, Chenxiong Qi <qcxhome at gmail.com> wrote:
>>> 
>>> On Mon, Dec 29, 2014 at 8:22 PM, Derek Atkins <derek at ihtfp.com> wrote:
>>>> Because it's a constant.
>>>> 
>>> 
>>> Thanks!
>>> 
>>> In following scenario, a QofInstance cannot be added into a QofCollection
>>> 
>>> QofIdType type = "some type";
>>> QofInstance inst = g_object_new (QOF_TYPE_INSTANCE, NULL);
>>> inst->e_type = type;
>>> QofCollection coll = qof_collection_new (type);
>>> qof_collection_add_entity (coll, inst);
>>> 
>>> The last call will always fail, because coll's e_type has different
>>> address with the original variable type due to invocation of
>>> static_cast<QofIdType>
>> 
>> Which is a good thing in this case, because otherwise it would crash when you try to test inst->e_type later, after the function has exited and type is no longer valid. It's not the static_cast<QofIdType>() that changes coll->e_type, it's CACHE_INSERT(type), which does a string copy so that the type doesn't go out of scope.
>> 
>> You can either preempt it with
>>  QofIdType type = STRING_CACHE("some type");
>> Once the string is in the cache, all other calls to STRING_CACHE() will return the same pointer, or you could create the collection first and then
>>  inst->e_type = qof_collection_get_type (coll);
>> 
> 
> Thanks Mr. Ralls. I'm writing unit tests for QofCollection C
> interfaces, that allows me to know much details of QofCollection and
> QofInstance. Another question is about qof_collection_add_entity.
> Sorry for not starting a separate thread for this, I think they are
> same things of a kind to QofCollection.
> 
> qof_collection_add_entity does not set collection attribute of a
> QofInstance once it is added, but qof_collection_insert_entity does
> this. This confuses me. By going through the code, I think both of
> them should have same behavior. Am I right, or something I missed and
> understand incorrectly?

Qi,

You have to look at the usage of each call. qof_collection_insert_entity is used by QofInstance to register itself with the book and stores a backlink to the collection so that given an instance the collection can be easily retrieved and searched for something else, while qof_collection_add_entity is used by gncInvoice for a private collection. In the latter case over-writing the collection pointer on the instance would destroy the back link to the book’s collection.

The backlink is redundant since every instance also has a pointer to its book and the book owns the collections in question, so when reimplementing it I wouldn’t include the collection link, but the current design needs it.

When searching for a function name remember to search for scheme wrappers, where underscores are replaced with hyphens. E.g. `grep -r gnc[-_]get[-_]current[-_]book src`. 

Regards,
John Ralls




More information about the gnucash-devel mailing list