Re: Using templates to wrap OS APIs
"Scott Meyers"
Balog Pal wrote:
Suppose we had magic to make it did compile. How would I use it on the
client side?
Via typedefs, as others have shown, e.g., use #ifdefs or build tool search
path
configuration so that either
typedef OSWrapper<Windows> OSAPI;
or
typedef OSWrapper<Linux> OSAPI;
is in scope, then makes calls such as
OSAPI::sleepMilliseconds(1000);
In the meantime I thought up a similar way, through namespace alias:
namespace OSAPI = Linux; // Windows, etc
producing similar calls. But selection still needs a #if preprocessor block,
just like the original question was.
And if you use even one such block, you could replace the definition line
with #include, that picks up the *sole* proper file with implementation.
There is no need to use templates at all, and no need to mix stuff in a
single file ever.
As a matter of fact for that route you do not even need #if, just properly
created directoy tree, and either use the -I compiler options to select the
proper root or the version control system to check out the platform-specific
files. Or use a symbolic link.
While this kind of wrapping must include the whole world for everyone.
Not the whole world, just the whole API wrapping world.
What in practice is "gross" ;-) especially if the program is written to
portability, and uses the wrapper to actually get rid of any OS-specific. So
on itself not including windows.h, unistd.h and other such headers. That
have tendency to pollute with a million names and possibly even drop in a
bunch of macros too.
I'm not sure the tradeoff is a good one, call overhead shall not be big
compared to a real OS call's work.
Others have shown how
to make all the OS-dependent names dependent names in the wrapper
functions,
hence not looked up until instantiation, which will never take place for
the
names corresponding to the OS not being compiled for.
I still don't see how want for templates occoured. I use templates when I
have a "common body" with tiny portions of replaceable elements.
Implementation of any OS wrappers I ever made were completely different.
Possibly your question is really about a second wrapper layer, that would
not use the API calls, but the first layer of wrappers?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]