[ValueRef] Interest in a new library for immutable values with internal sharing?

Hi, I'd like to gauge whether there might be any interest in a new Boost library comprising a generic, non-intrusive, wrapper class for creating immutable/const values with sharing of the underlying object via internal reference counting. This new class would be called ValueRef (or possibly just Value), and it would effectively provide an assignable/swappable 'reference'/handle to shared, const values. It can be considered an effective alternative to the commonly used shared-pointer-to-const idiom, providing cleaner and more explicit value semantics, whilst also hiding calls to 'new' as an internal implementation detail. In a nutshell, ValueRef adapts a regular type T to give a new type ValueRef<T> with const T value semantics and uses reference counting of an underlying object on the heap internally to reduce copying overhead. ValueRef will spawn off a new copy of the wrapped object (if necessary) if the ValueRef is assigned another ValueRef or another object of type T. Const functions of T do not incur any overhead of counter access or copy and are invoked by either * or -> syntax. Forwarding constructors are provided which permit the wrapped value to be constructed in place, and conversion operator allows inter-operation with values of the unwrapped type. ValueRef also provides the common relational operators. I have so far implemented a rough (C++03-only so far) version of this (based on a stripped down version of Adobe::copy_on_write library) and a test suite and it seems to work okay. Example usage: /* type of ValueRef wrapped std::vector<int> */ typedef ValueRef<std::vector<int> > ValueRefVecType; /* create vector of 10 elements via forwarding constructor */ ValueRefVecType valueRefVector1(10,1); /* non-mutating/const op */ assert( valueRefVector1->size() == 10 ); /* non-mutating/const op */ assert( (*valueRefVector1)[0] == 1 ); /* error - compilation failure occurs calling a non-const op */ (*valueRefVector)[0] = 2; /* copy ctor */ ValueRefVecType valueRefVector2(valueRefVector1); /* test for equality */ assert( valueRefVector1 == valueRefVector2 ); /* test for equality (implicit conversion) */ assert( valueRefVector2 == std::vector<int>(10,1) ); /* valueRefVector1 and valueRefVector2 share same std::vector */ assert( valueRefVector1.identity(valueRefVector2) ); /* ref count is no longer one */ assert( !valueRefVector1.unique_instance() ); /* new value */ valueRefVector1 = std::vector<int>(5); /* non-mutating/const */ assert( valueRefVector1->size() == 5 ); /* test for equality - value semantics */ assert( !(valueRefVector1 == valueRefVector2) ); /* valueRefVector1 and valueRefVector2 do not share same std::vector */ assert( !valueRefVector1.identity(valueRefVector2) ); I am not aware of anything else current that provides this functionality. Regards, Tom

On Monday 20 April 2015 11:04:22 THOMAS JORDAN wrote:
Hi, I'd like to gauge whether there might be any interest in a new Boost library comprising a generic, non-intrusive, wrapper class for creating immutable/const values with sharing of the underlying object via internal reference counting.
Is this similar to Boost.Flyweight?

I had a similar idea for a new smart pointer that would aim to obsolete the shared_ptr-to-const idiom. The problem I found with that idiom is that it only guarantees that you can't change it. There's no promise that somebody else doesn't have a non-const ref to the data and so it could in the worst case completely change from under your feet (even if you know it won't get destroyed). This smart pointer would provide non-const access to it if, and only if, you are currently the only person holding the object (ie, shared_ptr::unique is true). This allows you to build up data and, once complete, share it among the system as a const object that everybody knows will not change because they know that as they have a hold of it nobody else can change it. I've spectacularly failed to find an appropriate name for this kind of smart pointer. Other's at work have joked it should be called a schrodinger_ptr... On 20 April 2015 at 11:22, Andrey Semashev <andrey.semashev@gmail.com> wrote:
On Monday 20 April 2015 11:04:22 THOMAS JORDAN wrote:
Hi, I'd like to gauge whether there might be any interest in a new Boost library comprising a generic, non-intrusive, wrapper class for creating immutable/const values with sharing of the underlying object via internal reference counting.
Is this similar to Boost.Flyweight?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (3)
-
Andrey Semashev
-
Sam Kellett
-
THOMAS JORDAN