soandos
soandos

Reputation: 5146

Implementing copy on write

How can this be done for a class that holds a generic member, something like:

template<typename T> class SP
{
private:
    T* data;
    reference* ref;
    public:
        //Some methods here to access data
};

Upvotes: 0

Views: 358

Answers (1)

evpo
evpo

Reputation: 2531

I found two different approaches to copy on write (COW):

COW Poiner

COWPtr<Object> cow(&obj);
const COWPtr<Object> &cow_ref = cow;
std::cout << cow_ref->name; // operator->() doesn't copy the object because its const overload is used
cow->name = "my object"; // here non const operator->() copies the object
(*cow).name // operator*() also copies the underlying object

WRITE method from Adobe stlab

COW<Object> cow(&obj);
std::cout << cow->name; // the object is not copied
cow.write().name = "my object"; // the object is copied here

Upvotes: 1

Related Questions