[Ns-developers] Pcap and Ascii Tracing Rework: Mixins
craigdo at ee.washington.edu
Mon Feb 1 14:12:12 PST 2010
There seems to be one major issue left on the pcap and ascii tracing rework.
I would like to implement the new trace functionality as several mixins.
For quite some time, we have frowned on multiple inheritance (at least
gratuitously so) in ns-3. However, we already have a number of instances of
multiple inheritance that have appeared over the years. I don't know if
these are accidental, or things that haven't been caught in reviews, or what
the ultimate story is. However, putting a stamp of approval on mixins will
mean that we are allowing multiple inheritance at least at that level; and
so it should probably be discussed on the list.
Up until today, pybindgen did not support multiple inheritance, but Gustavo
has kindly enabled that for us.
As it stands now, the pcap and ascii tracing code has all of the classes for
the new helper functionality linearized. This makes it, well, ugly and not
very maintainable. Mixins would be the "correct" way to implement such
So, what are these mixin things? For those unfamiliar with mixins, the
following is from Wikipedia (http://en.wikipedia.org/wiki/Mixin):
"In object-oriented programming languages, a mixin is a class that provides
a certain functionality to be inherited by a subclass, while not meant for
instantiation (the generation of objects of that class). Inheriting from a
mixin is not a form of specialization but is rather a means of collecting
functionality. A class may inherit most or all of its functionality from one
or more mixins through multiple inheritance."
"A mixin can also be viewed as an interface with implemented methods. When a
class includes a mixin, the class implements the interface and includes,
rather than inherits, all the mixin's attributes and methods. They become
part of the class during compilation."
So, the idea is to, for some piece of functionality, to design an interface
class and provide a self-contained implementation of that functionality. In
the case of the new trace framework, these mixin classes would have names
class PcapHelper or
class AsciiTraceHelper or
class PcapUserHelperForIpv4 or
These classes would implement the methods that allow a protocol to enable
the various flavors of tracing on Ipv4 protocols. Similar mixins would be
provided for the Ipv6 flavors.
Then, to add tracing functionality to the InternetStackHelper one would
simply inherit from the desired mixins.
class InternetStackHelper : public PcapUserHelperForIpv4, public
public PcapUserHelperForIpv6, public
Without mixins, the you have got to linearize the inheritance graph to get
single inheritance. You end up with AsciiTraceUserHelperForIpv6 inheriting
from PcapUserHelperForIpv6 inheriting from AsciiTraceUserHelperForIpv4
inheriting from PcapUserHelperForIpv6.
In this case, AsciiTraceUserHelperForIpv6 should really be called
PcapAndAsciiTraceHelperForIpv4AndIpv6; PcapTraceuserHelperForIpv6 should
really be called PcapAndAsciiTraceHelperForIpv4AndPcapTraceHelperForIpv6;
AsciiTraceUserHelperForIpv4 should really be called
I find this to be horrifying.
We avoid this nonsense in low-level code by using aggregation. We made a
conscious design decision to avoid low-level mechanisms in helpers wherever
possible and so I think mixins are the only sane answer here.
Now that we have multiple inheritance implemented in pybindgen, I would like
to go ahead and use mixins for this functionality.
Any objections? Comments?
More information about the Ns-developers