Dear all,
I’m thinking about making the base class Packet a value object. In tests it would be much easier to compare value objects (CalitkoMocks makes this automatically) instead of pointers (for which one needs to do a kind of a hack with CalitkoMocks). Some say that Value Objects should be immutable and in Java there is a reason for that. However, the way we have implemented our Packets with reference counting, mutating a Packet object would cause its private data to be copied first and thus would not produce side effects in the objects that used to share private data with us. Another issue I now have with Packet is that it is an abstract base class and thus no instances of it can be created. That is something that sometimes is done in CalitkoMocks under the hood (e.g. when a function argument is a const Packet &). Well, there is a workaround to that but it is a bit tricky - creating a template specialization of a certain class.
So what I’m thinking about is creating the class PacketValue (better suggestions?) derived from Packet, which:
- has a ctor taking a const Packet & and stores a copy of it internally
- implements a copy ctor by means of the virtual copy ctor (the implementation of which essentially just increases the ref count of the private data)
- provides the assignment and comparison operators()
- implementing all pure virtual methods of the base class simply by delegating to the internally stored object (i.e. have it act as a kind of proxy)
- a new function object() will return a reference to the internally stored Packet which we could downcast when necessary.
Then functions returning a Packet * or a Packet auto_ptr will return a PacketValue instead. Similarly when passing a Packet as an argument, we would instead pass a const PacketValue &.
I think the same concept could be applied to all kinds of data derived from an abstract base class (e.g. Ggep).
What do you think? Any better ideas?
Regards,
Peter
