Boost logo

Proto :

Subject: Re: [proto] Deep copy while keeping references in terminals
From: Eric Niebler (eric_at_[hidden])
Date: 2011-07-03 13:07:25


On 7/2/2011 4:44 PM, Bart Janssens wrote:
> Hello,
>
> In the framwork I'm developing, I need to deep-copy expressions to
> store them in some sort of wrapper objects. This works fine in most
> cases, but when terminals are copied the value they refer to seems to
> be copied as well. In the following expression, "tau" is a POD struct
> with some coefficients that need to be computed, and compute_tau is a
> proto terminal, where the grammar is used to give meaning to the
> operator() :
> compute_tau(u, tau)
>
> The problem is that I use this tau in other expressions that are
> defined later on, so every expression needs to refer to the same tau,
> but it seems that after proto::deep_copy, each expression has his own
> tau.
>
> Is there an easy way around this? Note that I also like to use things
> like tau.ps, with ps a double in the tau struct directly in
> expressions.

You can do pretty much anything -- including reimplement
proto::deep_copy with slight variations -- with Proto transforms. From
your mail, it's a little unclear if you want the terminal nodes
/themselves/ held by reference, or if you want the values referred to by
the terminals held by reference but the terminal nodes held by value. I
implemented the second:

// Hold all intermediate nodes by value. If a terminal is holding
// a value by reference, leave it as a reference.
struct DeepCopy1
  : proto::or_<
        proto::terminal<_>,
        proto::nary_expr<
            _,
            proto::vararg<
                proto::when<DeepCopy1, proto::_byval(DeepCopy1)>
>
>
>
{};

HTH,

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Proto list run by eric at boostpro.com