T
Tomás Ó hÉilidhe
(I'm simplifying this problem greaty in the following code but you get
the idea)
I have a class something like:
class Processor {
public:
unsigned GetCurrentProcess(void);
unsigned GetCurrentThread(void);
void DoSomethingWithProcess(unsigned process_id);
void DoSomethingWithThread(unsigned thread_id);
};
The function, GetCurrentProcess, returns an unsigned integer value
which represents a process.
The function, GetCurrentThread, returns an unsigned integer value
which represents a thread.
The problem is, that it's very easy to mistakenly take the value from
GetCurrentProcess and pass it as an argument to DoSomethingWithThread.
(I've done it already!). Therefore, I want two different types for these
identifiers, two types that can't convert to each other. At the moment I
have the following:
class Processor {
public:
struct ProcessHandle { unsigned i; };
struct ThreadHandle { unsigned i; };
ProcessHandle GetCurrentProcess(void);
ThreadHandle GetCurrentThread(void);
void DoSomethingWithProcess(ProcessHandle process_id);
void DoSomethingWithThread(ThreadHandle thread_id);
};
Of course, this does the trick. But I'm wondering if there's a more
elegant way of doing it? What other methods are there for making types
incompatible with each other?
(My actual code is far more complicated than this -- obviously I wouldn't
be very competant if I made such a stupid mistake in code as simple as
this).
the idea)
I have a class something like:
class Processor {
public:
unsigned GetCurrentProcess(void);
unsigned GetCurrentThread(void);
void DoSomethingWithProcess(unsigned process_id);
void DoSomethingWithThread(unsigned thread_id);
};
The function, GetCurrentProcess, returns an unsigned integer value
which represents a process.
The function, GetCurrentThread, returns an unsigned integer value
which represents a thread.
The problem is, that it's very easy to mistakenly take the value from
GetCurrentProcess and pass it as an argument to DoSomethingWithThread.
(I've done it already!). Therefore, I want two different types for these
identifiers, two types that can't convert to each other. At the moment I
have the following:
class Processor {
public:
struct ProcessHandle { unsigned i; };
struct ThreadHandle { unsigned i; };
ProcessHandle GetCurrentProcess(void);
ThreadHandle GetCurrentThread(void);
void DoSomethingWithProcess(ProcessHandle process_id);
void DoSomethingWithThread(ThreadHandle thread_id);
};
Of course, this does the trick. But I'm wondering if there's a more
elegant way of doing it? What other methods are there for making types
incompatible with each other?
(My actual code is far more complicated than this -- obviously I wouldn't
be very competant if I made such a stupid mistake in code as simple as
this).