views:

89

answers:

1

I've designed a copy-on-write base class. The class holds the default set of data needed by all children in a shared data model/CoW model.

The derived classes also have data that only pertains to them, but should be CoW between other instances of that derived class.

I'm looking for a clean way to implement this. If I had a base class FooInterface with shared data FooDataPrivate and a derived object FooDerived. I could create a FooDerivedDataPrivate.

The underlying data structure would not effect the exposed getters/setters API, so it's not about how a user interfaces with the objects. I'm just wondering if this is a typical MO for such cases or if there's a better/cleaner way?

What peeks my interest, is I see the potential of inheritance between the the private data classes. E.g. FooDerivedDataPrivate : public FooDataPrivate, but I'm not seeing a way to take advantage of that polymorphism in my derived classes.

class FooDataPrivate
{
public:
  Ref ref; // atomic reference counting object
  int a;
  int b;
  int c;

};

class FooInterface
{
public:
  // constructors and such
  // ....

  // methods are implemented to be copy on write.
  void setA(int val);
  void setB(int val);
  void setC(int val);

  // copy constructors, destructors, etc. all CoW friendly
private:
  FooDataPrivate *data;

};

class FooDerived : public FooInterface
{
public:
  FooDerived() : FooInterface() {}

private:
  // need more shared data for FooDerived
  // this is the ???, how is this best done cleanly?
};
+1  A: 

Here's an implementation of Copy-On-Write for C++.

bitc