Classes with the same functionality but not convertible

T

Tom Howard

-----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+ABKdb4zMeEgfjbb0VpftgEQCfQKXD
gCi//HEnMUDEkqjC0XWLYs0=
=NEZj
-----END PGP SIGNATURE-----
 
S

Shezan Baig

Tom said:
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?
 
D

davidrubin

Shezan said:
as

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top