summaryrefslogtreecommitdiff
path: root/contrib/utility/Documentation/Thoughts/Enum/EnumCount
blob: be8b252d4ded3d1c1645770fa1c72f8aa39316bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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$