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