mercredi 9 septembre 2015

Swift. Is it possible to determining property time at runtime

I have the next problem:

I have a class which can contain some data of different type but the type of the data is not known at compile time.

It looks like this

class Data {
    var dataType: PodDataType = PodDataType.None

    var componentsCount: UInt32 = 0

    var stride: UInt32 = 0

    var data // the data type of this field is unknown until dataType property is initialized
}

And I have another class which have multiple Data instances it looks like this:

class Mesh {

    var verticesCount: UInt32 = UInt32.max

    var facesCount: UInt32 = UInt32.max

    var UVWChannelsCount: UInt32 = UInt32.max

    var vertexIndexes: Data?

    var stripLength: UInt32 = UInt32.max

    var stripsCount: UInt32 = UInt32.max

    var vertex: Data?

    var normals: Data?

    var tangents: Data?

    var binormals: Data?

// and so on ...
}

Every time I create an new object of Data it should contain different data type in its 'data' field. What is the best way to solve such issues?

My thoughts: 1) AbstractFactory pattern: I can create base class Data and many subclasses like DataInt, DataFloat etc. but this means that I need to create subclass for every supporting type (int32, int8 and 15 types more).

2) Generics: I can define Data like this class Data, but after that my Mesh class become dependent from the Data class because there is no way to define generic class without type parameter.

3) UnsafePointer I can store data using this pointer but I not sure will the data persist if the NSData object from which I read the info is deallocated. Also what about the performance type casting from UnsafePointer to actual type.

Any other practices of this situation are highly appreciated.

P.S. I am developing parser for Pod file which stores 3D graphics data. POD File Format Specification can be found here as reference.

Aucun commentaire:

Enregistrer un commentaire