deep copying

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob@blah.com

    deep copying

    hi,

    I have been tasked with transferring a rather complex, deeply nested
    structure across dll's / memory spaces and effectively I need to deep
    copy the structure in question. Its a C struct and it contains
    pointers. I would have liked to think I could copy construct it, but I
    can't. I need to follow the pointers.

    Does anybody know if there's a quick and easy way to achieve this (as
    opposed to manually following the pointers and fields which go 8-15
    levels deep). I'm trying to see if boost can help me out but so far I
    haven't seen anything.

    Does anybody know whats the best approach to take here? I'm open to
    all options.... i.e. anything but manually follow those 15
    levels!! :) :) I'm fairly sure I'm reinventing the wheel, hence the
    question. The struct in question is a C struct, used in a c++ app
    (visual studio/borland, if its important).

    thanks for any info.

    Graham

  • Victor Bazarov

    #2
    Re: deep copying

    bob@blah.com wrote:
    I have been tasked with transferring a rather complex, deeply nested
    structure across dll's / memory spaces and effectively I need to deep
    copy the structure in question. Its a C struct and it contains
    pointers. I would have liked to think I could copy construct it, but I
    can't. I need to follow the pointers.
    >
    Does anybody know if there's a quick and easy way to achieve this (as
    opposed to manually following the pointers and fields which go 8-15
    levels deep). I'm trying to see if boost can help me out but so far I
    haven't seen anything.
    >
    Does anybody know whats the best approach to take here? I'm open to
    all options.... i.e. anything but manually follow those 15
    levels!! :) :) I'm fairly sure I'm reinventing the wheel, hence the
    question. The struct in question is a C struct, used in a c++ app
    (visual studio/borland, if its important).
    Something in line with

    // for members that are built-in or structs
    template<class Tclass deepcopy {
    static void go(T& dest, T const& src) {
    dest = src;
    }
    };

    // for the members that are pointers
    template<class Tclass deepcopy<T*{
    static void go(T* dest, T const* src) {
    return deepcopy<T>::go (*dest, *src);
    }
    };

    // and you only need to implement a real 'deepcopy' for your
    // top-level struct (everything else should just fall into place)
    void deepcopy(mystru ct& dest, mystruct const& src) {
    deepcopy(dest.m ember_one, src.member_one) ;
    ...
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Mark P

      #3
      Re: deep copying

      Victor Bazarov wrote:
      bob@blah.com wrote:
      >I have been tasked with transferring a rather complex, deeply nested
      >structure across dll's / memory spaces and effectively I need to deep
      >copy the structure in question. Its a C struct and it contains
      >pointers. I would have liked to think I could copy construct it, but I
      >can't. I need to follow the pointers.
      >>
      >Does anybody know if there's a quick and easy way to achieve this (as
      >opposed to manually following the pointers and fields which go 8-15
      >levels deep). I'm trying to see if boost can help me out but so far I
      >haven't seen anything.
      >>
      >Does anybody know whats the best approach to take here? I'm open to
      >all options.... i.e. anything but manually follow those 15
      >levels!! :) :) I'm fairly sure I'm reinventing the wheel, hence the
      >question. The struct in question is a C struct, used in a c++ app
      >(visual studio/borland, if its important).
      >
      Something in line with
      >
      // for members that are built-in or structs
      template<class Tclass deepcopy {
      static void go(T& dest, T const& src) {
      dest = src;
      }
      };
      >
      // for the members that are pointers
      template<class Tclass deepcopy<T*{
      static void go(T* dest, T const* src) {
      return deepcopy<T>::go (*dest, *src);
      }
      };
      >
      // and you only need to implement a real 'deepcopy' for your
      // top-level struct (everything else should just fall into place)
      void deepcopy(mystru ct& dest, mystruct const& src) {
      deepcopy(dest.m ember_one, src.member_one) ;
      ...
      }
      >
      V
      I don't understand this. Should the last line of code that you wrote
      (the implementation of the deepcopy function) be:

      deepcopy<member _type>::go( dest.member_one , src.member_one) ;

      If so, I don't see how this can work in the following situation:

      struct A
      {
      B b;
      };

      struct B
      {
      C* c;
      };

      struct C
      {
      int x;
      };


      According to your prescription, we implement deepcopy for the top-level
      struct A as:

      void deepcopy( A& dest, const A& src)
      {
      deepcopy<B>::go ( dest.b, src.b);
      }

      The internal call to deepcopy leads to dest.b = src.b. But if B does
      not have a deep copy assignment operator defined, we end up with a
      shallow copy of C* c.

      What am I missing here?

      -Mark

      Comment

      • Victor Bazarov

        #4
        Re: deep copying

        Mark P wrote:
        Victor Bazarov wrote:
        >bob@blah.com wrote:
        >>I have been tasked with transferring a rather complex, deeply nested
        >>structure across dll's / memory spaces and effectively I need to
        >>deep copy the structure in question. Its a C struct and it contains
        >>pointers. I would have liked to think I could copy construct it,
        >>but I can't. I need to follow the pointers.
        >>>
        >>Does anybody know if there's a quick and easy way to achieve this
        >>(as opposed to manually following the pointers and fields which go
        >>8-15 levels deep). I'm trying to see if boost can help me out but
        >>so far I haven't seen anything.
        >>>
        >>Does anybody know whats the best approach to take here? I'm open to
        >>all options.... i.e. anything but manually follow those 15
        >>levels!! :) :) I'm fairly sure I'm reinventing the wheel, hence the
        >>question. The struct in question is a C struct, used in a c++ app
        >>(visual studio/borland, if its important).
        >>
        >Something in line with
        >>
        > // for members that are built-in or structs
        > template<class Tclass deepcopy {
        > static void go(T& dest, T const& src) {
        > dest = src;
        > }
        > };
        >>
        > // for the members that are pointers
        > template<class Tclass deepcopy<T*{
        > static void go(T* dest, T const* src) {
        > return deepcopy<T>::go (*dest, *src);
        > }
        > };
        >>
        > // and you only need to implement a real 'deepcopy' for your
        > // top-level struct (everything else should just fall into place)
        > void deepcopy(mystru ct& dest, mystruct const& src) {
        > deepcopy(dest.m ember_one, src.member_one) ;
        > ...
        > }
        >>
        >V
        >
        I don't understand this.
        You actually understood much more than you give yourself credit for.
        Should the last line of code that you wrote
        (the implementation of the deepcopy function) be:
        >
        deepcopy<member _type>::go( dest.member_one , src.member_one) ;
        Right.

        I ought to define one more function:

        template<class Tvoid deepcopy(T &t, T const& tt) {
        return deepcopy<T>::go (t, tt);
        }
        >
        If so, I don't see how this can work in the following situation:
        >
        struct A
        {
        B b;
        };
        >
        struct B
        {
        C* c;
        };
        >
        struct C
        {
        int x;
        };
        >
        >
        According to your prescription, we implement deepcopy for the
        top-level struct A as:
        >
        void deepcopy( A& dest, const A& src)
        {
        deepcopy<B>::go ( dest.b, src.b);
        }
        >
        The internal call to deepcopy leads to dest.b = src.b. But if B does
        not have a deep copy assignment operator defined, we end up with a
        shallow copy of C* c.
        >
        What am I missing here?
        Not too much. You're on the right track, AFAICT.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        Working...