Using a dedicated enumeration type ByteOrder
is a good idea.
CFByteOrder
is (ultimately) a type alias for Int
, so you can
assign arbitrary integers to it, not just CFByteOrderBig/LittleEndian
.
If you don't pass the type in the unpack function then the compiler
has to infer it from the context and either of these would work:
let d1 = unpack(bytes, byteOrder: .BigEndian) as Double
let d2 : Double = unpack(bytes, byteOrder: .BigEndian)
If you pass the type as an argument then the compiler infers
the return type:
let d3 = unpack(bytes, toType: Double.self, byteOrder: .BigEndian)
I think it is a matter of taste which version you choose, I would
prefer passing the type explicitly.
Encapsulating the functions into a type is also a good idea.
But I would use a struct
which is initialized with the byte order,
and make that instance methods and not class methods. The reason
is that you probably will do several calls with the same byte order
and you don't have to pass it to every call:
struct BytePacker {
let byteOrder : ByteOrder
init (byteOrder: ByteOrder) {
self.byteOrder = byteOrder
}
func pack<T>(var value: T) -> [Byte] { ... }
func unpack<T>(valueByteArray: [Byte], toType type: T.Type) -> T { ... }
}
which can be used as
let packer = BytePacker(byteOrder: .BigEndian)
let bytes = packer.pack(12.34)
let d = packer.unpack(bytes, toType: Double.self)
There is a problem with
return UnsafePointer<T>(bytes).memory
in your first version of unpack()
because is not guaranteed that the
elements of a Swift Array
are stored in contiguous memory.
return bytes.withUnsafeBufferPointer {
return UnsafePointer<T>($0.baseAddress).memory
}
as in your second version of unpack()
is the correct solution
because withUnsafeBufferPointer()
calls the block with a pointer to contiguous
storage.
Your code assumes that the host byte order is little-endian,
which is the case for all current OS X and iOS platforms.
On the other hand, Swift is open source now and people already start
to port it to different platforms. I have no idea if Swift will be
ported to a big-endian platform at some time.
I would at least make that assumption explicit by defining
enum ByteOrder {
case BigEndian
case LittleEndian
static var hostByteOrder = ByteOrder.LittleEndian
}
and then replace
return (byteOrder == .LittleEndian) ? valueByteArray : valueByteArray.reverse()
by
return (byteOrder == ByteOrder.hostByteOrder) ? valueByteArray : valueByteArray.reverse()
In addition, I would add an assertion
assert(UInt(littleEndian: 1) == 1)
e.g. to the init
method of BytePacker
.
Alternatively, you can determine the actual host byte order at
runtime:
enum ByteOrder {
case BigEndian
case LittleEndian
static var hostByteOrder = ByteOrder()
init() {
self = (UInt(littleEndian: 1) == 1) ? .LittleEndian : .BigEndian
}
}
but I don't know if that is worth the hassle. (Note that
the static property hostByteOrder
is computed only once in
the lifetime of the app, not each time that it is used.)
Your generic pack/unpack functions can be called with
arguments of any type, and that would give wrong results or crashes for
- reference types, where the instance is just a pointer to the object,
- "complex types" like
Array
or String
, which are a struct
but
use opaque pointers to the actual memory holding the elements.
There is – as far as I know – no protocol which comprises all integer and
floating point types. What you could do is to define a protocol
protocol PackableType { }
and make all types which can safely be packed and unpacked explicitly conform to it:
extension Int : PackableType { }
extension UInt16 : PackableType { }
// More integer types ...
extension Double : PackableType { }
// More floating point types ...
(Unfortunately, there are many integer types and I don't know if the repetition
can be avoided.)
Now you can restrict the type <T>
to <T : PackableType>
.
Alternatively, take a different approach and make the pack/unpack
methods operate on the type itself by defining a protocol extension:
extension PackableType {
func pack(byteOrder byteOrder: ByteOrder) -> [Byte] {
var value = self
let valueByteArray = withUnsafePointer(&value) {
Array(UnsafeBufferPointer(start: UnsafePointer<Byte>($0), count: sizeofValue(value)))
}
return (byteOrder == ByteOrder.hostByteOrder) ? valueByteArray : valueByteArray.reverse()
}
static func unpack(valueByteArray: [Byte], byteOrder: ByteOrder) -> Self {
let bytes = (byteOrder == ByteOrder.hostByteOrder) ? valueByteArray : valueByteArray.reverse()
return bytes.withUnsafeBufferPointer {
return UnsafePointer($0.baseAddress).memory
}
}
}
(Note how the compiler infers the type in return UnsafePointer($0.baseAddress).memory
automatically.)
This would now be used as
let bytes = 12.34.pack(byteOrder: .LittleEndian)
let double = Double.unpack(bytes, byteOrder: .LittleEndian)
and solve your question "how to define the return type in unpack" in a
different way.