Home Help Center

Circular Includes

slypeteslypete Member Pete SylvesterOrganization: Coconaut StudiosProject: iPhone/iPod Game ✭✭
Hello,

Is it possible to use the IceUtil::Handle smart pointer class for circular includes? I cannot seem to figure it out without getting a billion errors. If I use a forward declaration and then declare a smart pointer of that class in my interface, I obviously cannot since it's simply a template class and it will try to instantiate the unknown class in the header. I can probably store a pointer to the smart pointer, but I haven't tried it because that just seems very wrong.

For now as a work around, I just look up what room the player is in each time the player makes a request that requires a room. You can see that I'd much rather just store a smart pointer to the room the player is in.

I'm sure that I'm just misusing something. Can you please shed some light down on me?

Thanks a lot,
Pete

Comments

  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Hi Pete,

    You cannot use IceUtil::Handle for circular includes, as the IceUtil::Handle needs to see the declaration of the class parameter.

    If you're adventurous, you could use IceInternal::Handle, which is very similar to IceUtil::Handle but supports such cyclic includes. From Ice/Handle.h:
    //
    // "Handle" or "smart pointer" class for classes derived from
    // IceUtil::GCShared, IceUtil::Shared, or IceUtil::SimpleShared.
    //
    // In constrast to IceUtil::Handle, IceInternal::Handle requires the
    // declaration of the two global operations IceInternal::incRef(T*)
    // and IceInternal::decRef(T*). The use of global operations allows
    // this template to be used for types which are declared but not
    // defined, provided that the two above mentioned operations are
    // declared.
    //
    

    IceInternal::Handle is also compatible with IceUtil::Handle. However, like any IceInternal type, it's undocumented and can change at anytime.

    Cheers,
    Bernard
  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Hi Pete,

    I should have double-checked this comment before copying it here :(, as it doesn't match the code. IceInternal::Handle needs now a single function, upCast, that upcasts a pointer to the template parameter to an IceUtil::Shared*.

    Cheers,
    Bernard
  • slypeteslypete Member Pete SylvesterOrganization: Coconaut StudiosProject: iPhone/iPod Game ✭✭
    Thanks Bernard!

    This is very useful information, and I doubt that I would have come across it myself. I'm surprised ZeroC doesn't offer this class in the public API. It surely must come up often?

    Thanks again,
    Pete
Sign In or Register to comment.