No struct extending?

Steve Horne stephenwantshornenospam100 at aol.com
Mon Sep 11 12:22:11 PDT 2006


OK - I have my solution. It's not perfect, but it's easy enough and
it's good enough.

For the branch nodes and leaf nodes and their common part, the branch
and leaf node can just use property getter/setter functions to give
shorthand access to the shared parts. This didn't occur to me before
because I resist adding methods to structs.

It still doesn't handle the mix-in layers issue, though. I don't know
which layers were mixed in previously, so I can't know which shortcuts
to provide.

But I can say that each member is always at the same offset in a
struct, once the struct is composed, even if that struct gets further
extensions. Non-member functions can take void pointers to the
buffers. They can be passed down through mix-in layers using the
'mixin' keyword...

  template t_Keys_Mixin_Layer (alias PL, class c_Key)
  {
    mixin PL;  //  Mix in definitions from previous layer

    struct c_Leaf
    {
      PL.c_Leaf               m_Previous;
      c_Key[PL.m_Array_Size]  m_Keys;
    }

    c_Key[] Node_Keys (void* p_Node)
    {
      if (Node_Is_Leaf (p_Node))  //  Defined in previous layer
      {
        c_Leaf* l_Leaf = cast(c_Leaf*) p_Node;

        //  Don't know what previous layers defined - depends which
        //  layers the user decided to mix in - but doesn't matter
        //  since I know what is needed now...

        return l_Leaf.m_Keys;
      }

      throw new Exception ("Node is not a leaf");
    }
  }

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list