diff options
Diffstat (limited to 'ACE/contrib/utility/Documentation/Thoughts/Enum')
3 files changed, 157 insertions, 0 deletions
diff --git a/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumCount b/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumCount new file mode 100644 index 00000000000..be8b252d4de --- /dev/null +++ b/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumCount @@ -0,0 +1,95 @@ +From: Gianni Mariani <gi2nospam@mariani.ws> +Date: 26 Jul 2003 04:52:43 GMT +Newsgroups: comp.lang.c++ +Subject: Re: enum count + +Clive wrote: +> If you have an enum, is there any way during execution to find the number of +> values in the enum? +> Say I have, +> +> enum great { five, ten, fifteen }; +> +> How could I get the number 3 from that? +> +> + +replace the enums with objects that report themselves to a registry. + + +I have done it in the past using a template ... + +templace <typename base> +class ExposedEnum : public base +{ + public: + int enum_val; + ExposedEnum( int number ) + : enum_val( number ) + { + ExposedEnumRegister<base>::Register( *this ); + } + + ExposedEnum( int number ) + : enum_val( ExposedEnumRegister<base>::GetNextNumber() ) + { + ExposedEnumRegister<base>::Register( *this ); + } + + + +// some more stuff ... + + operator int () const + { + return enum_val; + } + + explicit ExposedEnum( const ExposedEnum & foo ); +}; + + +template <typename base> +class ExposedEnumRegister +{ + + static int GetNextNumber .... + + static void Register .... + + static int Count .... + +} + + +Now you can forward declare them... + +extern ExposedEnum< great > five; + +extern ExposedEnum< great > ten; + +extern ExposedEnum< great > fifteen; + + + + +In a cpp file you can instantiate them. + +ExposedEnum< great > five( 5 ); + +ExposedEnum< great > ten( 10 ); + +ExposedEnum< great > fifteen; + + +Now, if you want to know how many you have : + +ExposedEnumRegister< great >::Count(); + + + +Disclaimer - it's an outline only, yes it's incomplete. + +G + +$Id$ diff --git a/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumCount2 b/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumCount2 new file mode 100644 index 00000000000..d23e790b6d4 --- /dev/null +++ b/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumCount2 @@ -0,0 +1,17 @@ +From: Alexander Terekhov <terekhov@web.de> +Date: Sat, 26 Jul 2003 19:17:52 +0200 +Newsgroups: comp.lang.c++ +Subject: Re: enum count + + +Steve Pinard wrote: +> +> Wouldn't that be nice? And wouldn't it be nice to be able to get the +> strings "five", "ten", and "fifteen" too? + +http://lcgapp.cern.ch/project/architecture/XTI_accu.pdf + +regards, +alexander. + +$Id$ diff --git a/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumInfo b/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumInfo new file mode 100644 index 00000000000..e59a27c0820 --- /dev/null +++ b/ACE/contrib/utility/Documentation/Thoughts/Enum/EnumInfo @@ -0,0 +1,45 @@ +From: "Klaus Eichner" <klaus_gb@yahoo.com> +Date: Sat, 26 Jul 2003 14:53:23 +0100 +Newsgroups: comp.lang.c++ +Subject: Re: enum count + +"Clive" <clive@clive.clive> wrote in message +news:3f21e5cc$0$23611$5a62ac22@freenews.iinet.net.au... +> If you have an enum, is there any way during execution to find the number +of +> values in the enum? +> Say I have, +> +> enum great { five, ten, fifteen }; +> +> How could I get the number 3 from that? + +You could get the number 3 from 'great' with your own, user-defined +'Enum_Info' template: + +cout << "The number of values in enum great is " + << Enum_Info<great>::number_of_elements + << endl; + +The 'Enum_Info' template is defined as follows. +Suppose you have the following enums: + +enum great { five, ten, fifteen }; +enum greater { none, one, fourtytwo, fourtythree, fourtyfour }; +enum even_greater { minusone, minustwo, minusthree, minusfour, minusfive, +minussix, minusseven }; + +You could build a template class 'Enum_Info' which uses specialisation to +register the number of elements in each enum + +template <class T> class Enum_Info { }; +template <> class Enum_Info<great> { static const int number_of_elements = +3; }; +template <> class Enum_Info<greater> { static const int number_of_elements = +5; }; +template <> class Enum_Info<even_greater> { static const int +number_of_elements = 7; }; + + + +$Id$ |