C ++ class "Automatic" proxy

I need to allow the user to modify the elements of two data structures of the same type at the same time. For instance:

struct Foo { int a, b; }

Foo a1 = {1,2}, a2 = {3,4};
dual(a1,a2)->a = 5;
// Now a1 = {5,2} and a2 = {5,2}

      

I have a class that works and that changes first a1 and then copies a1 to a2. It's ok if:

  • the class copied is small
  • the user doesn't mind anything that gets copied, not just the changed part.

Is there a way to get this behavior:

dual(a1,a2)->a = 5;
// Now a1 = {5,2} and a2 = {5,4}

      

I open up alternative syntax, but they should stay simple and I would like to avoid things like:

set_members(a1, a2, &Foo::a, 5);
members(a1, a2, &Foo::a) = 5;

      

or anything with a specific indication &Foo::

[change]

I have to be more precise. The point is to work with the graph library. The library works on a directed graph, but usage dictates that for two vertices v1 and v2, if there is an edge v1-> v2, then there will be an edge v2-> v1. And these two edges very often (but not always) have the same properties. Thus, the current implementation now allows:

G.edge(v1,v2)->b = 5; // Only v1->v2 is modified
G.arc(v1,v2)->a = 10;
// Now G.edge(v2,v1) is set to G.edge(v1,v2) after the modification a = 10 (i.e. b = 5 too)

      

And I would like the designation to mean changed only a

.

+2


a source to share


5 answers


A relatively simple solution with Boost.Lambda:

#include <boost/lambda/lambda.hpp>

using namespace boost::lambda;

template<typename T, typename U, typename V>
void dual(const T& functor, U& a1, V& a2)
{
    functor(a1);
    functor(a2);
}

struct Foo
{
    int a;
};

struct Bar
{
    char a;
};

int main()
{
    Foo a1;
    Bar a2;

    dual(_1 = 5, a1.a, a2.a);
}

      



Extending dual () with variation patterns /Boost.Preprocessor shenanigans is left as an exercise for the reader.

+2


a source


// to get the desired syntax

template<class T>
class SetPropertyProxy
{
public:
   SetPropertyProxy(T& _v1, T& _v2)
     : a(_v1, _v2) {}

   class A_Property_Proxy
   {
   public:
       A_Property_Proxy(T& _v1, T& _v2): v1(_v1), v2(_v2) {}
       A_Property_Proxy& operator = (T::A_Property_Type val)
       {
           v1.a = val;
           v2.a = val;
           return *this;
       }
   private:
       T& v1;
       T& v2;
   }
   //public member "a"
   A_Property_Proxy a;
};
//helper function
template<class T>
SetPropertyProxy<T> dual(T& a , T& b)
{ return SetPropertyProxy<T>(a,b); }
//usage
dual(a,b).a = 5; //calls A_Property_Proxy::operator =

      



Can be improved further by creating the A_Property_Proxy class reusable by parameterizing by property type and referencing properties instead of referencing property containers (in this case edges)

   template<class U>
   class Property_Proxy
   {
   public:
       Property_Proxy(U& _v1prop, U& _v2prop): v1prop(_v1prop), v2prop(_v2prop) {}
       Property_Proxy& operator = (U val)
       {
           v1prop = val;
           v2prop = val;
           return *this;
       }
   private:
       U& v1prop;
       U& v2prop;
   }

      

+1


a source


Edit (by putting this here because comments have no formatting)

So you say that your current code has a lot of this:

G.edge(v3,v4)->a = 2;
G.edge(v3,v4)->b = 2;
G.edge(v4,v5)->a = 6;
G.edge(v4,v5)->b = 6;

      

And very little of this:

G.edge(v5,v6)->a = 4;
G.edge(v5,v6)->b = 7;

      

And your goals [1] make it easier to identify those special cases [2] with less verbose code?

----- original answer, maybe out of date now -----

Here's the general idea, there are many possible improvements:

class MagicBag
{
private:
    // you could make the whole class a template 
    // instead of hard-coding Foo..
    vector<Foo *> m_vec;

public:
    // store references to the items
    void Add(Foo *f) { m_vec->push_back(f); }

    // you can do overloads instead of these setters...
    void set_a(int val) {
        for (vector<Foo>::iterator i = m_vec.start(); i != m_vec.end(); ++i)
            (*i)->a = val;
    }

    void set_b(int val) {
        for (vector<Foo>::iterator i = m_vec.start(); i != m_vec.end(); ++i)
            (*i)->b = val;
    }
}

      

Using:

Foo a1 = {1,2}, a2 = {3,4};
MagicBag mb;

mb.Add(&a1);
mb.Add(&a2);
mb.set_a(5); // now a1.a = 5 and a2.a = 5
// etc.

      

It's easier semantically in languages ​​that support properties like C #. There's the last syntax:

mb.a = 5;

      

0


a source


By misusing templates I can get most of the syntax I want. This compiles and works, but there are no guarantees in it. This requires adding some macros to the structure being used and requires using set_ * instead of direct assignment.

#include <iostream>

#define PROPERTY_MAP(ClassName) \
    struct hidden_Mapper {      \
        ClassName * m_d1;       \
        ClassName * m_d2;       \
        hidden_Mapper(Data * d1, Data * d2) : \
            m_d1(d1), m_d2(d2) {} 

#define DECLARE_PROPERTY(name)\
    template <typename ValueType> \
    void set_##name(const ValueType & value) \
    { m_d1->name = value; m_d2->name = value; } \

#define END_PROPERTY_MAP };


template<typename ClassType>
typename ClassType::hidden_Mapper dual(ClassType & d1, ClassType & d2)
{
    return typename ClassType::hidden_Mapper(&d1, &d2);
}

struct Data
{
    int a;
    float b;

    PROPERTY_MAP(Data)
        DECLARE_PROPERTY(a)
        DECLARE_PROPERTY(b);
    END_PROPERTY_MAP
};

int main()
{
    Data d1, d2;
    dual(d1, d2).set_a(5);
    dual(d1, d2).set_b(5.7);
    std::cout << d1.a << d2.a << d1.b << d2.b <<std::endl;
}

      

0


a source


struct proxy {
  struct column {
     column(T &a, T &b);
     column& operator=(T);
     T &a, &b;
  };
  proxy(U &A, U &B);
  column operator[](int i) { return column(A[i], B[i]; }
  U &A, &B;
};

proxy(A, B)[0] = 5;
// or you could be evil, overload ",", and get this syntax
(A, B)[0] = 5;

      

or some option

-1


a source







All Articles