Bug 517

Summary: RandomVariable serialization is ugly
Product: ns-3 Reporter: Rajib Bhattacharjea <raj.b>
Component: coreAssignee: ns-bugs <ns-bugs>
Status: RESOLVED WONTFIX    
Severity: normal CC: mathieu.lacage
Priority: P5    
Version: ns-3-dev   
Hardware: All   
OS: All   
Attachments: make random variable serialize cleaner

Description Rajib Bhattacharjea 2009-03-03 15:33:00 UTC
Created attachment 396 [details]
make random variable serialize cleaner

The code for serializing RandomVariables to an ostream (ostream& operator<<(ostream&, const RandomVariable&)) is a series of downcasts testing for which subclass is being serialized, with some per-type code to actually perform the serialization.  Each writer of a new variable type will at some point have to go in to this code and write another downcast, and another chunk of type specific code.

The attached patch inverts the responsibility of serialization to a method in each subclass, adds a pure virtual method in the base class to enforce the existence of this method, and has the serialization code call the virtual method.  This eliminates the downcasts and the need to change the serialization code for each RandomVariable type added to ns-3.
Comment 1 Mathieu Lacage 2009-03-03 15:40:27 UTC
The problem is that deserialization still needs a global function and it's much easier to check for code correctness when the serialization function is close to the deserialization function which is not the case if you move serialization to a per-class method and leave deserialization to a global function.

So, yes, it's ugly but the alternatives don't look really any better.