What is hard to understand? The code takes a pointer to a serial object, and a number that defines whether the serial object is a software serial object or a hardware serial object.
It performs an appropriate cast, based on the type.
Both types derive from Stream, so the casts are possible, and logical.