C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can templates help with platform-specific specialisations

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matt Young
Guest





PostPosted: Wed Apr 27, 2005 8:23 am    Post subject: Can templates help with platform-specific specialisations Reply with quote



What's the best way to specialise a class for one of several different
platforms at compile time (rather than run time)?

For example, suppose I am writing a class to be used on two different
platforms. Some of the member functions are common across both, but
some are platform-specific. If I were writing a library, I would use
public inheritance and virtual member functions, which means an extra
look-up is done at run-time to get the right platform-specific code.
But this seems inefficient given that:
- I am only compiling for one platform at a time
- I already know which platform I'm compiling for at compile time
- I am happy to recompile all the users of the class for each
platform

Is there a way to rewrite the following code so that the look-up of
specialize() is done at compile-time rather than run-time? Is there
for example some way to write Base as a template and use the platform
as a policy parameter?

// Compiled for both platform A and platform B
class Base {
...
void common() {
...
this->specialize()
}
virtual void specialize() = 0;
}

// Compiled only in the platform A build
class PlatformA : public Base {
...
virtual void specialise() { ... }
}

// Compiled only in the platform B build
class PlatformB : public Base {
...
virtual void specialise() { ... }
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Thu Apr 28, 2005 1:10 am    Post subject: Re: Can templates help with platform-specific specialisation Reply with quote



On Wed, 27 Apr 2005 12:23:45 +0400, Matt Young <palaso (AT) gmail (DOT) com> wrote:

Quote:
What's the best way to specialise a class for one of several different
platforms at compile time (rather than run time)?

IMO, the best way is to declare a platform independent API in a header and
make several platform dependent implementation files. Having put those
implementation files in folders lile ./src/linux/impl.cpp and
../src/win32/impl.cpp, you only have to choose the right folder in your
build script, no platform #ifdef's required in files that use the API
declared in the header.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maciej Sobczak
Guest





PostPosted: Thu Apr 28, 2005 12:28 pm    Post subject: Re: Can templates help with platform-specific specialisation Reply with quote



Hi,

Matt Young wrote:

Quote:
What's the best way to specialise a class for one of several different
platforms at compile time (rather than run time)?

Put the implementation of the class for each platform in a separate
directory and select the aproppriate one at compile-time from the
Makefile - this is the earliest place where you know the target platform.

If the differences between implementations are really minor, the #ifdef
here and there might be enough. Note that this usually swells to become
an unmanageable spaghetti.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Thu Apr 28, 2005 12:42 pm    Post subject: Re: Can templates help with platform-specific specialisation Reply with quote

In article <22585373.0504260633.4ffd5d19 (AT) posting (DOT) google.com>, Matt
Young <palaso (AT) gmail (DOT) com> wrote:

Quote:
What's the best way to specialise a class for one of several different
platforms at compile time (rather than run time)?

For example, suppose I am writing a class to be used on two different
platforms. Some of the member functions are common across both, but
some are platform-specific. If I were writing a library, I would use
public inheritance and virtual member functions, which means an extra
look-up is done at run-time to get the right platform-specific code.
But this seems inefficient given that:
- I am only compiling for one platform at a time
- I already know which platform I'm compiling for at compile time
- I am happy to recompile all the users of the class for each
platform

Is there a way to rewrite the following code so that the look-up of
specialize() is done at compile-time rather than run-time? Is there
for example some way to write Base as a template and use the platform
as a policy parameter?

I assume you have some preprocessor variable that deternines the

actual platform. any compiler tiime long constant will do.

template <long PlatformId> struct Specialize;

// specialize this for each platform each containing
// at least void specialize() as a method.

template <class Special>
struct Chooser
{
void common() { this->specialize();}
}:

typedef Chooser<Specialize Process;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
unspammable@gmail.com
Guest





PostPosted: Fri Apr 29, 2005 1:41 am    Post subject: Re: Can templates help with platform-specific specialisation Reply with quote

I wouldn't use inheritence the way you did to customize a class for a
specific platform. Ask yourself, does the relation "PlatformA is-a
Base" really hold?
What not user #ifdefs?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Johan Nilsson
Guest





PostPosted: Fri Apr 29, 2005 1:44 am    Post subject: Re: Can templates help with platform-specific specialisation Reply with quote

Matt Young wrote:
Quote:
What's the best way to specialise a class for one of several
different
platforms at compile time (rather than run time)?

The best way might be to do as Maxim Yegorushkin outlined in his
answer. I'm just offering a different spin on the subject as you
mentioned templates and policies, see code below.

Quote:

For example, suppose I am writing a class to be used on two different
platforms. Some of the member functions are common across both, but
some are platform-specific. If I were writing a library, I would use
public inheritance and virtual member functions, which means an extra
look-up is done at run-time to get the right platform-specific code.
But this seems inefficient given that:
- I am only compiling for one platform at a time
- I already know which platform I'm compiling for at compile time
- I am happy to recompile all the users of the class for each
platform

Is there a way to rewrite the following code so that the look-up of
specialize() is done at compile-time rather than run-time? Is there
for example some way to write Base as a template and use the platform
as a policy parameter?

// Compiled for both platform A and platform B
class Base {
...
void common() {
...
this->specialize()
}
virtual void specialize() = 0;
}

// Compiled only in the platform A build
class PlatformA : public Base {
...
virtual void specialise() { ... }
}

// Compiled only in the platform B build
class PlatformB : public Base {
...
virtual void specialise() { ... }
}


[the names Base and Platform no longer makes 100% sense in the below
examples, I just kept to them for reference]

Policy-based:

template<typename Platform>
struct Base : public Platform
{
void common()
{ this->specialize(); }
};

.... or perhaps CRTP ...

template<typename T>
struct Base
{
void common()
{ static_cast<T*>(this)->specialize(); }
};

struct Platform
: Base<Platform>
{
void specialize() {}
};

.... but you'd still have to select the actual "Platform" using other
means (e.g. preprocessor conditionals) if you're compiling the same
code for multiple platforms. I guess it would also be possible to
select the implementation based on some clever meta-programming and
platform-defined macros (e.g. WIN32), but have never tried it.

// Johan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Philipp Bachmann
Guest





PostPosted: Fri Apr 29, 2005 3:53 am    Post subject: Re: Can templates help with platform-specific specialisation Reply with quote

Quote:
What's the best way to specialise a class for one of several different
platforms at compile time (rather than run time)?

For example, suppose I am writing a class to be used on two different
platforms. Some of the member functions are common across both, but
some are platform-specific. If I were writing a library, I would use
public inheritance and virtual member functions, which means an extra
look-up is done at run-time to get the right platform-specific code.
But this seems inefficient given that:
- I am only compiling for one platform at a time
- I already know which platform I'm compiling for at compile time
- I am happy to recompile all the users of the class for each
platform

Is there a way to rewrite the following code so that the look-up of
specialize() is done at compile-time rather than run-time? Is there
for example some way to write Base as a template and use the platform
as a policy parameter?

// Compiled for both platform A and platform B
class Base {
...
void common() {
...
this->specialize()
}
virtual void specialize() = 0;
}

// Compiled only in the platform A build
class PlatformA : public Base {
...
virtual void specialise() { ... }
}

// Compiled only in the platform B build
class PlatformB : public Base {
...
virtual void specialise() { ... }
}

Yes, and I do so. Either you follow Maxim's suggestion or you do the
following:

struct Linux {
};

template< class OS > class Base {
...
inline void common(void);
};

template<> void Base< Linux >::common(void) {
...
}

typedef Linux OS;

Base< OS > base;

I.e. you make "Base" a class template, don't implement (some of) its
member
functions in a general way, but implement specializations for your OS.
The only
difference you've between your different operating systems is the
"typedef" line.
Note that not the whole class template gets specialized, but only the
member
functions.

Both approaches lead to the same goal: The interface of "Base" is
guaranteed to
be the same on all platforms.

There's a pattern around: Wrapperfacade from the POSA#2 book (which
doesn't
discuss how this assurance can be implemented, however).

Cheers,
Philipp.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.