libcppa  Version 0.1
Classes | Functions
Copy-on-write optimization.

Classes

class  cppa::cow_ptr< T >
 A copy-on-write smart pointer implementation. More...
class  cppa::tuple< ElementTypes >
 A fixed-length copy-on-write tuple. More...

Functions

template<size_t N, typename T >
const T & get (const tuple<...> &tup)
template<size_t N, typename T >
T & get_ref (tuple<...> &tup)

Detailed Description

libcppa uses a copy-on-write optimization for its message passing implementation.

Tuples should always be used with by-value semantic, since tuples use a copy-on-write smart pointer internally. Let's assume two tuple x and y, whereas y is a copy of x:

 auto x = make_tuple(1, 2, 3);
 auto y = x;

Those two tuples initially point to the same data (the addresses of the first element of x is equal to the address of the first element of y):

 assert(&(get<0>(x)) == &(get<0>(y)));

get<0>(x) returns a const-reference to the first element of x. The function get does not have a const-overload to avoid unintended copies. The function get_ref could be used to modify tuple elements. A call to this function detaches the tuple by copying the data before modifying it if there are two or more references to the data:

 // detaches x from y
 get_ref<0>(x) = 42;
 // x and y no longer point to the same data
 assert(&(get<0>(x)) != &(get<0>(y)));

Function Documentation

template<size_t N, typename T >
const T & get ( const tuple<...> &  tup) [related]

Gets a const-reference to the Nth element of tup.

Parameters:
tupThe tuple object.
Returns:
A const-reference of type T, whereas T is the type of the Nth element of tup.
template<size_t N, typename T >
T & get_ref ( tuple<...> &  tup) [related]

Gets a reference to the Nth element of tup.

Parameters:
tupThe tuple object.
Returns:
A reference of type T, whereas T is the type of the Nth element of tup.
Note:
Detaches tup if there are two or more references to the tuple data.