Enumeration of enums. Is there a way to attain something like this.

This is a discussion on Enumeration of enums. Is there a way to attain something like this. within the C++ Programming forums, part of the General Programming Boards category; I am working on a client server based communication protocol stack. I have a list of errors that could occur ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    13

    Enumeration of enums. Is there a way to attain something like this.

    I am working on a client server based communication protocol stack.
    I have a list of errors that could occur during the course of communication
    Like,


    Code:
    Errortype        [1]
        Type1        [0]
        Type2        [1]
    
    
    Type1
        Subtype1    [0]
        Subtype2    [1]
    
    
    Type2
        Subtype3    [0]
    
    
    Subtype1
        cause1        [0]
        cause2        [1]
        cause3        [2]
    
    
    Subtype2
        cause4        [0]
    
    
    Subtype3
        cause5        [0]
        cause6        [1]
        cause7        [2]
        cause8        [3]
    So, if the server finds an error in connection due to cause7 of subtype3
    and type2, it has to send the error report as:
    Code:
    errortype          : 0x01
    Type       : 0x01
    Subtype            : 0x00
    Cause    : 0x02

    So the data transferes is 01 01 00 02


    I found that i can't have enum of enums in c++. And other suggestion I got
    was to used a 4 byte integer for the whole error structure, so that the most significant 8 bit will show errortype, the next 8bit shows type, the next shows subtype and the least significant 8 bit shows cause. Is there any better way to do it, so that the types and subtypes and cause don't get mixed up, like type2 an't have subtype1 cause1.?
    Last edited by Sajas K K; 03-18-2013 at 05:53 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    When working with networking, it is always best to abstract the details, meaning that you should create some internal structure with your enums and stuff, then write conversion routines to pack it into some integer or array that you can send over the network. You need to code the reverse, too, obviously.
    Do not try to interpret raw data directly. It's not portable, and it makes it hard in case you need to change something later.
    Perhaps one of the best ways is to use a string stream to write data into a string using the stream operators, then send that string over the network. It is guaranteed to work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Sajas K K View Post
    So the data transferes is 01 01 00 02
    You could divide your datatype into ranges, for example if the above represents a byte (ignoring the 2 for a moment) then each sub type would be able to carry 3 bits of information. Meaning error type would be masked as 0xC0, type: 0x30, subtype: 0xC and cause: 0x3, this is quite limiting for a byte obviously but the same principle would work on larger ints as well for example.

    Edit:

    Given these ranges, you could then compose an error code by assigning: errortyp | type | subtype | cause; like that with a bitwise or.
    Last edited by Subsonics; 03-18-2013 at 11:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enums this time
    By sahilpatel_13 in forum C Programming
    Replies: 3
    Last Post: 03-11-2013, 12:14 PM
  2. Using enums in classes
    By eaane74 in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2007, 02:00 PM
  3. Enums and Templates
    By einarp in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2006, 05:07 AM
  4. Tricking enums
    By Ganoosh in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 01:46 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 11:25 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21