Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > one enum is a subset of another

Reply
Thread Tools

one enum is a subset of another

 
 
Christopher
Guest
Posts: n/a
 
      01-31-2012
A developer before me created a large enumeration we can call
EnumType1.
He then, right under it, typedef-ed another

typedef EnumType1 EnumType2

He then created a comment "alias, should only include x types", where
x is a rule defining a subset of EnumType1

This is crap imo. Any function or method declared to take EnumType2,
would happily take a value from EnumType1 that does not meet the x
criteria.

Since I know the subset from EmunType1 that meets the x criteria, how
can I define a subset EnumType2 that only includes those enums without
typing the whole darn subset twice?

Example
enum Fruit
{
APPLE = 0,
BANANA,
ORANGE,
TANGERINE,
GRAPEFRUIT,
NUM_FRUITS
};

typedef Fruit Citrus; // This is crap and I want to fix it



 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      01-31-2012
On Jan 31, 10:56*pm, Christopher <cp...@austin.rr.com> wrote:
> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
>
> typedef EnumType1 EnumType2
>
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
>
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from *EnumType1 that does not meet the x
> criteria.
>
> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?
>
> Example
> enum Fruit
> {
> * *APPLE = 0,
> * *BANANA,
> * *ORANGE,
> * *TANGERINE,
> * *GRAPEFRUIT,
> * *NUM_FRUITS
>
> };
>
> typedef Fruit Citrus; * // This is crap and I want to fix it


I'm not sure you can do what you want. In view of which, what your
developer has done doesn't seem entirely stupid - the compiler won't
catch any errors but things are more obvious to a human reader. For
instance:

typedef Fruit Apple_or_Banana;

Apple_or_Banana x;

x = ORANGE;

- the compiler won't spot the error, but you can.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-31-2012
On Tue, 2012-01-31, Paul N wrote:
> On Jan 31, 10:56*pm, Christopher <cp...@austin.rr.com> wrote:
>> A developer before me created a large enumeration we can call
>> EnumType1.
>> He then, right under it, typedef-ed another
>>
>> typedef EnumType1 EnumType2
>>
>> He then created a comment "alias, should only include x types", where
>> x is a rule defining a subset of EnumType1
>>
>> This is crap imo. Any function or method declared to take EnumType2,
>> would happily take a value from *EnumType1 that does not meet the x
>> criteria.
>>
>> Since I know the subset from EmunType1 that meets the x criteria, how
>> can I define a subset EnumType2 that only includes those enums without
>> typing the whole darn subset twice?
>>
>> Example
>> enum Fruit
>> {
>> * *APPLE = 0,
>> * *BANANA,
>> * *ORANGE,
>> * *TANGERINE,
>> * *GRAPEFRUIT,
>> * *NUM_FRUITS
>>
>> };
>>
>> typedef Fruit Citrus; * // This is crap and I want to fix it

>
> I'm not sure you can do what you want. In view of which, what your
> developer has done doesn't seem entirely stupid - the compiler won't
> catch any errors but things are more obvious to a human reader.


Depends on your attitude to typedefs ... I am suspicious them (in this
usage) because they introduce uncertainty into an area where usually
the compiler guarantees correctness (i.e. if Fruit and Citrus had been
classes).

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      02-01-2012
On 31.01.2012 23:56, Christopher wrote:
> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
>
> typedef EnumType1 EnumType2
>
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
>
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from EnumType1 that does not meet the x
> criteria.


Exactly. You need different types.

> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?


There is no language support for this kind of problem.

AFAIK you have two options:
1. repeat the definition of the common constants.
2. declare your own enum classes.

In the latter case you need to know that Citrus is no subclass of Fruit,
because you can safely cast from the subset to the general one but not
the other way around.

class Citrus
{public:
static const Citrus ORANGE;
static const Citrus GRAPEFRUIT;
//...

protected:
Citrus() {}
};

class Fruit : Citrus
{ static const Fruit APPLE;
//...
protected:
Fruit() {}
};

bool operator==(const Citrus& l, const Citrus& r)
{ return &l == &r;
}
bool operator!=(const Citrus& l, const Citrus& r)
{ return &l != &r;
}

Note that this pseudo enums have no value, since the fixed number of
instances is already sufficient for uniqueness. This is similar to Java
2 like enums.

Things get complicated when you need to define different subsets.
Especially the comparison operators have to be overloaded appropriately.
And if you also require an associated int value, you need a virtual base
class holding that value or alternatively build your own lookup table to
ensure uniqueness over the related class types.


Marcel
 
Reply With Quote
 
Thomas Boell
Guest
Posts: n/a
 
      02-01-2012
On Tue, 31 Jan 2012 14:56:03 -0800 (PST)
Christopher <> wrote:

> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
>
> typedef EnumType1 EnumType2
>
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
>
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from EnumType1 that does not meet the x
> criteria.
>
> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?
>
> Example
> enum Fruit
> {
> APPLE = 0,
> BANANA,
> ORANGE,
> TANGERINE,
> GRAPEFRUIT,
> NUM_FRUITS
> };
>
> typedef Fruit Citrus; // This is crap and I want to fix it


You could do something like this:

#define FRUIT_VALUES \
APPLE, \
BANANA, \
ORANGE

enum Fruit
{
FRUIT_VALUES
};

enum Things
{
FRUIT_VALUES,
BEERCAN, CAR, ALOT
};

Whether using the preprocessor like this is "good style" depends on
your point of view.


 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      02-01-2012
On 02/01/12 02:51, Marcel Müller wrote:
> On 31.01.2012 23:56, Christopher wrote:
>> A developer before me created a large enumeration we can call
>> EnumType1.
>> He then, right under it, typedef-ed another
>>
>> typedef EnumType1 EnumType2
>>
>> He then created a comment "alias, should only include x types", where
>> x is a rule defining a subset of EnumType1
>>
>> This is crap imo. Any function or method declared to take EnumType2,
>> would happily take a value from EnumType1 that does not meet the x
>> criteria.

>
> Exactly. You need different types.
>
>> Since I know the subset from EmunType1 that meets the x criteria, how
>> can I define a subset EnumType2 that only includes those enums without
>> typing the whole darn subset twice?

>
> There is no language support for this kind of problem.
>
> AFAIK you have two options:
> 1. repeat the definition of the common constants.
> 2. declare your own enum classes.
>
> In the latter case you need to know that Citrus is no subclass of Fruit,
> because you can safely cast from the subset to the general one but not
> the other way around.
>
> class Citrus
> {public:
> static const Citrus ORANGE;
> static const Citrus GRAPEFRUIT;
> //...
>
> protected:
> Citrus() {}
> };
>
> class Fruit : Citrus
> { static const Fruit APPLE;
> //...
> protected:
> Fruit() {}
> };
>
> bool operator==(const Citrus& l, const Citrus& r)
> { return &l == &r;
> }
> bool operator!=(const Citrus& l, const Citrus& r)
> { return &l != &r;
> }
>
> Note that this pseudo enums have no value, since the fixed number of
> instances is already sufficient for uniqueness. This is similar to Java
> 2 like enums.

[snip]

Hi Marcel,

I tried a slightly modified form of your code shown in the attached.
However, trying to compile it, without the #define DEF_FRUITS, I got the
error messages:

../build/gcc4_6v/gcc.test/enum_static_const.o: In function `main':
/home/evansl/prog_dev/gcc.test/enum_static_const.cpp:37: undefined
reference to `Fruit::APPLE'
/home/evansl/prog_dev/gcc.test/enum_static_const.cpp:38: undefined
reference to `Citrus::ORANGE'
collect2: ld returned 1 exit status

In addition, I'm guessing you meant the copy CTOR's should be private
too to avoid any other instances of Fruit or Citrus from being
created. Is that right?

-regards,
Larry


 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      02-01-2012
On 01/31/12 16:56, Christopher wrote:
> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
>
> typedef EnumType1 EnumType2
>
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
>
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from EnumType1 that does not meet the x
> criteria.
>
> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?
>
> Example
> enum Fruit
> {
> APPLE = 0,
> BANANA,
> ORANGE,
> TANGERINE,
> GRAPEFRUIT,
> NUM_FRUITS
> };
>
> typedef Fruit Citrus; // This is crap and I want to fix it
>
>
>

Maybe this thread would help:

http://groups.google.com/group/comp....9f71bf8d783ec0

-regards,
Larry

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      02-01-2012
On 01.02.2012 18:46, Larry Evans wrote:
> I tried a slightly modified form of your code shown in the attached.
> However, trying to compile it, without the #define DEF_FRUITS, I got the
> error messages:
>
> ./build/gcc4_6v/gcc.test/enum_static_const.o: In function `main':
> /home/evansl/prog_dev/gcc.test/enum_static_const.cpp:37: undefined
> reference to `Fruit::APPLE'
> /home/evansl/prog_dev/gcc.test/enum_static_const.cpp:38: undefined
> reference to `Citrus::ORANGE'
> collect2: ld returned 1 exit status


You are right. It was no complete code. It only shows the way.

C++ requires statics to be defined outside the class in a compilation
unit. I.e.:

const Citrus Citrus::ORANGE;
const Citrus Citrus::GRAPEFRUIT;
const Fruit Fruit::APPLE;
....


> In addition, I'm guessing you meant the copy CTOR's should be private
> too to avoid any other instances of Fruit or Citrus from being
> created. Is that right?


Yes, both. The default constructor need to be private and the object
should be non-copyable.
Citrus(const Citrus&) = delete;
Citrus& operator=(const Citrus&) = delete;
should do the job.


Marcel
 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      02-01-2012
Thomas Boell <> wrote:
> You could do something like this:
>
> #define FRUIT_VALUES \
> APPLE, \
> BANANA, \
> ORANGE
>
> enum Fruit
> {
> FRUIT_VALUES
> };
>
> enum Things
> {
> FRUIT_VALUES,
> BEERCAN, CAR, ALOT
> };
>
> Whether using the preprocessor like this is "good style" depends on
> your point of view.


That's quite dangerous. You have to be sure, that FRUIT_VALUES is always
the first in the enum, otherwise the values are no more the same in both
enums.

I think it should not be possible to have the same name in two different
enums anyway. I am not sure though.

Tobi
 
Reply With Quote
 
Matt D.
Guest
Posts: n/a
 
      02-01-2012
On 2/1/2012 14:08, Thomas Boell wrote:
> You could do something like this:
>
> #define FRUIT_VALUES \
> APPLE, \
> BANANA, \
> ORANGE
>
> enum Fruit
> {
> FRUIT_VALUES
> };
>
> enum Things
> {
> FRUIT_VALUES,
> BEERCAN, CAR, ALOT
> };
>
> Whether using the preprocessor like this is "good style" depends on
> your point of view.


It's seems very much like inheritance, so it made me think that perhaps
using something along the following could be of use:
http://www.codeproject.com/Articles/...-a-C-enum-type

Best,

Matt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's a good way to find whether a hash is a subset of another hash (deep)? dblock Ruby 2 10-09-2011 08:37 PM
how to detect that one XML is a subset of another one? Phlip XML 6 04-23-2010 10:10 PM
The Easiest way to check if one array is a subset of another Thriving K. Ruby 2 08-04-2009 05:23 AM
How to test if one dict is subset of another? Jay Tee Python 15 02-20-2007 09:10 PM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM



Advertisments