473,379 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to use stream with enum?

Hi,

I have some problem when I use enum with stream. The code segment is
listed below. I know if I change the first line to "int op;", there
will not be any error. However, what I really want is of enum type. Is
there any other way to fix this problem?

Best wishes,
Peng

rect_type op;// int op;
ss >> op;//std::istringstream ss; compile time error

//with the declaration of

enum rect_type{
CANONICAL,

TOP_LEFT,
BOTTOM_LEFT,
TOP_RIGHT,
BOTTOM_RIGHT,

TOP_MIDDLE,
BOTTOM_MIDDLE,
LEFT_MIDDLE,
RIGHT_MIDDLE,

CENTRAL
};

Jul 23 '05 #1
6 4667
On 4 Jun 2005 11:47:56 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote:
Hi,

I have some problem when I use enum with stream. The code segment is
listed below. I know if I change the first line to "int op;", there
will not be any error. However, what I really want is of enum type. Is
there any other way to fix this problem?

Best wishes,
Peng

rect_type op;// int op;
ss >> op;//std::istringstream ss; compile time error

//with the declaration of

enum rect_type{
CANONICAL,

TOP_LEFT,
BOTTOM_LEFT,
TOP_RIGHT,
BOTTOM_RIGHT,

TOP_MIDDLE,
BOTTOM_MIDDLE,
LEFT_MIDDLE,
RIGHT_MIDDLE,

CENTRAL
};


I bet you also tried overloading the global operator>> (taking the
first argument of type "ostream &" and a second argument of type
"rect_type const &")? I would. But you might not like the results. It
is indeed possible, but the big problem is that there is no built-in
conversion from int to enum in C++.

Somewhere down the line, you must convert your input from an integral
tye to type rect_type, and there is no "easy" way to do this except to
provide a conversion function taking some integer type as an argument,
returning a rect_type, and containing nothing but a huge switch
statement more or less duplicating your enum definition.

You could hide the conversion function in some kind of policy class,
but it might not be worth the effort.

--
Bob Hairgrove
No**********@Home.com
Jul 23 '05 #2
Ian
Bob Hairgrove wrote:


I bet you also tried overloading the global operator>> (taking the
first argument of type "ostream &" and a second argument of type
"rect_type const &")? I would. But you might not like the results. It
is indeed possible, but the big problem is that there is no built-in
conversion from int to enum in C++.

Somewhere down the line, you must convert your input from an integral
tye to type rect_type, and there is no "easy" way to do this except to
provide a conversion function taking some integer type as an argument,
returning a rect_type, and containing nothing but a huge switch
statement more or less duplicating your enum definition.

Not such a big deal for this example as the enum values are contiguous,
so you only have to check against min and max values and do a static_cast.

Ian
Jul 23 '05 #3


Bob Hairgrove wrote:
On 4 Jun 2005 11:47:56 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote:
Hi,

I have some problem when I use enum with stream. The code segment is
listed below. I know if I change the first line to "int op;", there
will not be any error. However, what I really want is of enum type. Is
there any other way to fix this problem?

Best wishes,
Peng

rect_type op;// int op;
ss >> op;//std::istringstream ss; compile time error

//with the declaration of

enum rect_type{
CANONICAL,

TOP_LEFT,
BOTTOM_LEFT,
TOP_RIGHT,
BOTTOM_RIGHT,

TOP_MIDDLE,
BOTTOM_MIDDLE,
LEFT_MIDDLE,
RIGHT_MIDDLE,

CENTRAL
};

[snip] Somewhere down the line, you must convert your input from an integral
tye to type rect_type, and there is no "easy" way to do this except to
provide a conversion function taking some integer type as an argument,
returning a rect_type, and containing nothing but a huge switch
statement more or less duplicating your enum definition.


This "function" is called casting! But, you do need to do a little
work: make your enum into an attribute class, and provide an 'isValid'
method:

struct RectType {
enum Value {
// enumerated values
};

static bool isValid(int value);
// Return 'true' if 'the specified value' is a valid 'RectType'
// and 'false' otherwise.
};

bool RectType::isValid(int value)
{
return (0 <= value <= CENTRAL); // depending on enumerated values
}

Notice that 'isValid' does not need to take 'RectType::Value' because
an object of type 'RectType' is always valid.

Then:

RectType::Value rectType; // initial value?
int value = -1;
stream >> value;
if (RectType::isValid(value)) {
rectType = (RectType::Value)value;
}

It helps to have some enumerated value like 'UNSPECIFIED' to be used as
an initial value in cases like the one above. Otherwise, you need to do
some fancier error handling.

Of course if you don't care about checking whether 'value' is a valid
'RectType', you just skip the if-clause.

/david

Jul 23 '05 #4
Ian
da********@warpmail.net wrote:

This "function" is called casting! But, you do need to do a little
work: make your enum into an attribute class, and provide an 'isValid'
method:

struct RectType {
enum Value {
// enumerated values
};

static bool isValid(int value);
// Return 'true' if 'the specified value' is a valid 'RectType'
// and 'false' otherwise.
};

bool RectType::isValid(int value)
{
return (0 <= value <= CENTRAL); // depending on enumerated values
}

Notice that 'isValid' does not need to take 'RectType::Value' because
an object of type 'RectType' is always valid.

Then:

RectType::Value rectType; // initial value?
int value = -1;
stream >> value;
if (RectType::isValid(value)) {
rectType = (RectType::Value)value;


It would be better style to use a static_cast here.

Ian
Jul 23 '05 #5
da********@warpmail.net wrote:
bool RectType::isValid(int value)
{
return (0 <= value <= CENTRAL); // depending on enumerated values
}


This will not work. Try, instead:

return (0 <= value) && (value <= CENTRAL) ;

-Alan
Jul 23 '05 #6
oops. Thanks for the correction. /david

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
7
by: Lee | last post by:
Hi, I'm a stream virgin and am attempting to output strings to a file. My approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
36
by: puzzlecracker | last post by:
Would someone explain why this declaration is illegal: class Sample<T> where T : Stream, class
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.