object hierarchy, parent-child links, etc



I am trying to construct an object hierarchy for some relatively simple
objects I am trying to expose to COM / Automation for use in scripting.
Some of the child objects need to have links back to their parents or
roots. (Microsoft doesn't seem to have a good example of doing this, or
if they do then I can't find it.)

Initially I used CComObjectEmbed as discussed in
http://www.mvps.org/vcfaq/com/12.htm for one of my parent-child pairs,
in which the parent has one statically-created child member. Seems to
work fine & I think I understand the rationale behind how it does ref.
counting. Great.

Then I went to adapt this to another one of my parent-child pairs.
(parent class = Ivariables/Cvariables, which owns a collection of child
class = Ivariable/Cvariable. For the purposes of this question,
however, I will call it Iparent/Cparent and Ichild/Cchild.) This time I
could not get it to compile. The problem comes when I try to create a
new instance dynamically of the child class via
CComObjectEmbed<Cchild>::CreateInstance (instead of
CComObject<Cchild>::CreateInstance as in my old version where the child
did not have a link back to the parent.) The compiler complains about
not knowing the GUID associated with my class.

Is this the right way to obtain a pointer to a new child class? If not,
how do I change things? Can I still use the CComObjectEmbed template or
do I need to do something completely different?


related Q's:
I think I may be misunderstanding IDispatch. I was under the impression
that anytime I return an object to a method that's going to be called
in my script, I have to QueryInterface into an (IDispatch **) rather
than, say, an (Ichild **), since the scripting languages know about
IDispatches and VARIANTS but have no idea how to handle my object. Does
the IDispatch dual interface break the CComObjectEmbed template? If so,
is there a workaround?


The FAQ referenced above also says:
>It's only pertinent to note here that if the child object needs services from
>its parent not exposed though COM interfaces, it would be necessary
>to store a pointer to the raw C++ class of the parent instead. Similarly,
>if the nested objects in your hierarchy need services from the root object
>of the hierarchy no matter the level of nesting, a pointer to the root C++
>object should be passed when they are initialized.

But I hit a stumbling block when I do this: the parent needs to hold a
CComObjectEmbed<Cchild> and the child needs to hold a
CComObject<Cparent>. This seems to lead to a circular include (each C++
class needs to know about the other). I know how to solve this w/
forward declarations for "plain" C++ classes but I can't seem to get it
to work with COM-template-abstracted C++ classes (or whatever Cchild
actually is since I can't instantiate a Cchild by itself).

Any help appreciated... These situations must occur pretty often, I've
looked around for a few hours but I can't seem to find an existing
example/tutorial.

.



Relevant Pages

  • Re: SQL, related records (quotes)
    ... Let's say for the hierarchy itself we decide on ... child parent ... child B to reference for referential integrity and what meaning the ... possible with a vertex involving itself as a root node of a hierarchy. ...
    (comp.databases.theory)
  • Re: [RFC][PATCH 1/2] memcg: res_counter hierarchy
    ... While several policy of hierarchy can be considered, ... create a child. ... prepare enough room in parent. ... One way to manage hierarchies other than via limits is to use shares (please see ...
    (Linux-Kernel)
  • Re: Re: [RFC][PATCH 1/2] memcg: res_counter hierarchy
    ... While several policy of hierarchy can be considered, ... there are no shared resource ... create a child. ... prepare enough room in parent. ...
    (Linux-Kernel)
  • [RFC][PATCH 1/2] memcg: res_counter hierarchy
    ... This patch tries to implements _simple_ 'hierarchy policy' in res_counter. ... dynamic hierarchy resource usage management in the kernel is not necessary ... create a child. ... prepare enough room in parent. ...
    (Linux-Kernel)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)