Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Giving types to enums  (Read 2551 times)

0 Members and 1 Guest are viewing this topic.

Manux

  • Newbie
  • *
  • Posts: 23
    • View Profile
Giving types to enums
« on: December 31, 2009, 02:54:55 am »
Hello,
I wondered if it is possible to give a numerical type other than int to enums.
I know it is possible using a compiler flag, unfortunately this will change all enums' types.

I want to optimise the size they will take, and I know some of them will be smaller than 255, and others probably will need a short.

It is indeed annoying to declare a variable which contains the enum's value with the good type to create sf::Packets

For example, this is what I am doing now:
Code: [Select]

    sf::Packet p;
    static short head = DEFAULT_HEAD;
    static char etype = GEVENT_SPAWN;
    static char otype = OTYPE_NPC;
    char* name=this->getFullName();
    p<<head<<etype<<otype<<name;
    return p;

what would be optimal:`
Code: [Select]

    sf::Packet p;
    char* name=this->getFullName();
    p<<DEFAULT_HEAD<< GEVENT_SPAWN << OTYPE_NPC <<name;
    return p;


Thanks for the ideas.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Giving types to enums
« Reply #1 on: December 31, 2009, 03:15:49 am »
Is the space really that important?

You could write a little class and overload the operator<< for sf::Packet.
Code: [Select]
struct EnumPack
{
     EnumPack(Enum1 e1, Enum2 e2)
     : e1(e1)
     , e2(e2)
     {
     }

     Enum1 e1;
     Enum2 e2;
};

sf::Packet& operator<<(sf::Packet& packet, const EnumPack& data)
{
    return packet << static_cast<sf::Int8>(data.e1) << static_cast<sf::Int16>(data.e2);
};

int main()
{
    sf::Packet packet;
    packet << EnumPack(DEFAULT_HEAD, GEVENT_SPAWN);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Manux

  • Newbie
  • *
  • Posts: 23
    • View Profile
Giving types to enums
« Reply #2 on: December 31, 2009, 03:53:14 am »
thanks! I had not thought of overloading, it's probably the most appropriate thing to do indeed.

As for space, it makes a notable difference to use chars or shorts when possible instead of ints, when lots of data is exchanged. Maybe scarcely related: we don't have much budget, none actually :P, so this counts.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Giving types to enums
« Reply #3 on: December 31, 2009, 01:12:34 pm »
Actually, you can directly overload operator<< for each enum type. The structure is not necessary.
Code: [Select]
enum Enum1
{
    DEFAULT_HEAD,
    MONSTER_HEAD,
};

sf::Packet& operator<<(sf::Packet& packet, Enum1 data)
{
    return packet << static_cast<sf::Int8>(data);
};

int main()
{
     sf::Packet packet;
     packet << DEFAULT_HEAD;
}

So you even achieve what you originally wanted. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: