views:

97

answers:

4

There are dozens of network protocols and file formats (WAV, TCP, BMP, etc. etc.)

Is there a solution available to create an abstraction layer between the implementation of a protocol and the code that uses the resulting data?

Take a WAV file. A software component could contain logic to identify chucks and parse them into classes. The user of this component would not need to know whether the number of channels is stored in two or four bytes.

The WAV file format specification is not all the different from hundreds of other formats and protocols, so why not make a generic solution? To create an even better abstraction, the protocol specification could be stored in XML. I've had no luck finding anything like this. Can you point me in the right direction?

EDIT: This clearly is not a problem that is easily communicated over a post. The current answers are not really what I'm looking for, but I'd like to thank everyone for at least giving it a shot.

+1  A: 

If you abstract something too far it gets to be useless and you have to bust open the black box to get anything done.

A "generic" container that holds sound files, images, and spreadsheets is going to be useless, because you are going to have code that deals with stuff specific to those types of data anyway, but now you're straddled with additional generic container nonsense.

whatsisname
Parsing a file would not require knowing anything about it's contents. Reading a BMP header is similar to reading a TCP header: just cast a certain amount of bytes to a struct. Now whether this is useful is a different question, but not one I asked :)
Mathijs
@Mathijs no you would need to know what the file contains if you want to reasonably parse it. You'll need to know its structure, you can't just guess whether you have 2 32bit ints or 1 64bit if you know nothing about what kind of file you are looking at.
whatsisname
+2  A: 

Unless this is for academic purposes, I do not think that such an abstraction layer would be a good idea.

Depending on the format and amount of data it would significantly degrade performance to the point where it would be better to go for a native implementation. Also, Using XML for the specification would make your performance problems even worse.

Adrian Grigore
How can you possibly conclude that performance will be unacceptable, just from the fact that there will be an abstraction layer? First, surely, there must be a definition of what "acceptable" performance is?
Mathijs
The OP is quoting WAV as one of the formats. Most professional audio programs are heavily optimized for good reason - musicians usually have to play many different audio tracks at the same time and sooner or later the CPU is going to be maxed out. Having an extra abstraction layer that transcodes the audio stream would certainly not help. For video formats it gets even worse.
Adrian Grigore
+1  A: 

Most things can be wrapped in protocols - just think how much has been wrapped up in URLs. But that doesn't always mean that the structure is really abstracted into a meaningful layer. I think the variety of protocols and formats indicates that there are probably categories for which you could abstract, but not one abstraction to rule them all.

For instance, there might be differences between accessing an IMAP mailbox and opening a ZIP file so significant that there is very little common ground.

Cade Roux
While I was not trying to get everything in one go, your remark about categories is correct. I think it's the most sensible response to my question and therefor deserves a green check.
Mathijs
+3  A: 

This sounds a lot like generalisation gone too far. Most of these formats and protocols are so different from eachother that they have little more in common than being stored in a file.

I.e. there are far more differences commonalities between these formats, and most commonalities are already abstracted away by standard libraries and/or the OS. Thus this doesn't exactly look like a very practical idea.

Edit: PS Have a look at http://www.codinghorror.com/blog/2004/12/it-came-from-planet-architecture.html because this feels very much like trying to play the architecture astronaut.

Giel