By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Classes with the same functionality but not convertible

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

On a project I'm working on we need a few of ID classes for various sub
systems. Each ID class (e.g. EntityID, ClassID, PhysicsID, AnimationID)
does exactly the same thing, but we would like to prevent conversion
from different types of IDs (e.g. boost::is_convertible< EntityID,
ClassID >::value is false), to avoid stupid errors.

So far we have come up with to methods for achieving this.

Method 1:

Create a macro called ORG_CREATE_ID_TYPE which takes a class name as an
argument, then when you need a new ID class, you just include the header
where the macro is defined and add

ORG_CREATE_ID_TYPE( MyID )

where you need MyID defined

Method 2:

enum IDTag { EntityIDTag, ClassIDTag, ..., MyIDTag };

template< IDTag T >
class BaseID
{
....
};

typedef BaseID< EntityIDTag > EntityID;
typedef BaseID< ClassIDTag > ClassID;
....
typedef BaseID< MyIDTag > MyID;

The template method is preferred but it is 17% slower to compile, which
given the size of the project and the number of developers is going to
add up to too much time over the next couple of years.

Is there another option I'm overlooking or some techniques you can think
of to make the template method faster?

Thanks in advance.

Cheers,

Tom Howard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCB++5w1G4ZUM7KZoRArioAJ9ypL+ABKdb4zMeEgfjbb 0VpftgEQCfQKXD
gCi//HEnMUDEkqjC0XWLYs0=
=NEZj
-----END PGP SIGNATURE-----
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Tom Howard wrote:
Create a macro called ORG_CREATE_ID_TYPE which takes a class name as an argument, then when you need a new ID class, you just include the header where the macro is defined and add

ORG_CREATE_ID_TYPE( MyID )


What does this macro do?

Jul 23 '05 #2

P: n/a

Shezan Baig wrote:
Tom Howard wrote:
Create a macro called ORG_CREATE_ID_TYPE which takes a class name
as an
argument, then when you need a new ID class, you just include the

header
where the macro is defined and add

ORG_CREATE_ID_TYPE( MyID )


What does this macro do?


Probably something along the lines of

#define ORG_CREATE_ID_TYPE(MyID) \
struct #MyID { \
//... \
}

Another option you should consider is a script (e.g., Perl) that
generates new ID-type components. This should reduce the burden on your
compiler. /david

Jul 23 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Shezan Baig wrote:
Tom Howard wrote:
Create a macro called ORG_CREATE_ID_TYPE which takes a class name as


an
argument, then when you need a new ID class, you just include the


header
where the macro is defined and add

ORG_CREATE_ID_TYPE( MyID )

What does this macro do?


#define ORG_CREATE_ID_TYPE( id_name ) \
class id_name \
{ \
....
};

It does the trick, but we would like to avoid the macro usage if
possible. One of the reasons being that Intellisense in VC++ doesn't
play with the macro generated classes.

Templates are logically the way to go, because with templates each id
class is an instance of the id template which is conceptually exactly
what we want, however we can't find a way to overcome the speed hiccup.

Cheers,

Tom Howard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCCvodw1G4ZUM7KZoRAoMxAKCSH7+XJea/uK80er2tMFpaISEGsQCfchPl
fsTtXefotRpBzAA9Ogvh7IU=
=5Qhg
-----END PGP SIGNATURE-----
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.