 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu May 25, 2006 8:21 pm Post subject: Wrapping enums |
|
|
Hi !
Once uppon a time, I was working on some UI library and felt the need
to define symbolic
constants to represent the keyboard keys. I naturally thought of some
thing like:
// Encloses all declarations of the library
namespace ui_lib {
enum key
{
a=0, // The A key
b=1, // The B key
//...
shift= .., // The shift key
enter= ..,
escape= ..,
// .. etc
};
}
One problem with enums is that they export the symbolic names in their
enclosing namespace,
Thus after the previous declaration very common names such as 'a' or
'enter' gets exported
into the enclosing namespace (i.e in ui_lib). Further, when someone
writes ui_lib::shift, it is not clear that
shift is actually a member of some ui_lib::key enum.
In a perfect world, we'd be able to write :
// 1- Use ui_lib::key normally as a type, but still access individual
// elements of the enum throght a key:: prefix
ui_lib::key k=ui_lib::key::a;
// 2- Symbolic constants not exported but 'enclosed' inside their enum
declaration
namespace ui_lib {
enum key { shift };
enum key_mods {shift}; // OK !
int shift=0; // OK !
}
// Notice how ui_lib::key::shift do not conflict with
ui_lib::key_mods::shift
I already thought of some sollutions :
-1 Name symbolic constants by prefixing them with the enum name :
enum key { key_a, key_b, .. key_shift, ... };
+ Seems to solve both problems
- Really ugly (especially when you want to rename the enum type)
-2 Enclose all enums in their *own namespaces*:
namespace key {
enum type {
a, b, ...
};
}
Now write ui_lib::key::type t=ui_lib::key::a;
- This convention is rather cumbersome, since it is no longer
possible to use 'key' as a type (one has to write key::type)
-3 Enclose all enums in their *own classes*:
class key {
enum {
a, b, c, d
} value;
operator=, ....
};
This one looks good. But it demands a greate amount of code for each
single
enum type (well, except if we can build a reusable sollution, where
the compiler would generate the class for us....).
Also we need to specify how convertions between the enclosed enum and
the
enclosing class will occur.
If you have suggestions, comments or anything, I'd be glad to hear
them.
Thanks in advance,
-- Anis Benyelloul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
johnchx2@yahoo.com Guest
|
Posted: Tue May 30, 2006 2:35 pm Post subject: Re: Wrapping enums |
|
|
psyko wrote:
| Quote: | johnchx2 (AT) yahoo (DOT) com wrote:
Typedefs provide the familiar "_t" forms for naming the enum types.
What !? There is a convention of naming enums by appending a '_t'?
|
No, there is a convention for naming *typedefs* by appending _t.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
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
|
|