I want to provide a common platform agnostic function declaration
which references different implementations for different hardware
platforms.
What is the most type safe way to do this in 'C'?
The term for what you want to do is "wrapping". To properly wrap
platform-specific APIs, do the following:
1. Understand what you want to do. Ask yourself questions like: "In
an ideal world, how would the rest of my code interface with the
desired functionality?", "What information would the wrapper code need
to successfully accomplish the things it needs to accomplish?", and
"How would I express what I'm trying to do in plain English to a
fellow programmer who is not familiar with the particulars?"
2. Understand the platform-specific APIs on the platforms you care
about. How do they work? Have you written test cases or prototypes
to drive the desired functionality?
3. Sort the implementations into categories. Sometimes this falls
into per-platform, per-OS, per library, etc.
4. Create a seperate file for each category you came up with in step
3. In each file, write different, platform-specific implementations
of the same function.
Using your example, you would have two files: FileOpenWin32.c and
FileOpenOther.c. Both files implement the function FileOpen().
The rest of your code can just call FileOpen() without worrying about
the details of how FileOpen() works. You can call the function the
same exact name on both platforms because it's impossible to have them
both compiled in at once.
Mark F. Haigh
(e-mail address removed)