Author Topic: REGULAR_ENUM--STRINGY_ENUM ...  (Read 850 times)

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,240
    • View Profile
    • http://www.titusgames.de
REGULAR_ENUM--STRINGY_ENUM ...
« on: 14 February 2010, 22:56:11 »
Sorry I'm new to C++, so please help me, all theses defines and switches in GAE are really not easy.

So, what are these defines good for? Why are they better than normal enums?

And what does all this mean ( from game_constants.h ):
Is this also some kind of precompiler directive in the comment?

Code: [Select]
/** The control type of a 'faction' (aka, player)
  * <ul><li><b>CLOSED</b> Slot closed, no faction</li>
  * <li><b>CPU</b> CPU player</li>
  * <li><b>CPU_ULTRA</b> Cheating CPU player</li>
  * <li><b>NETWORK</b> Network player</li>
  * <li><b>HUMAN</b> Local Player</li></ul>
  */
STRINGY_ENUM( ControlType,
CLOSED, CPU, CPU_ULTRA, NETWORK, HUMAN
);

I don't know what to do here and how to add my megaCPU  :-\ .
« Last Edit: 14 February 2010, 23:01:20 by titi »
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

hailstone

  • Local Moderator
  • Battle Machine
  • ********
  • Posts: 1,568
    • View Profile
Re: REGULAR_ENUM--STRINGY_ENUM ...
« Reply #1 on: 14 February 2010, 23:42:52 »
Edit: The stuff in the comments are for doxygen documentation (html).

STRINGY_ENUM is an easy way to print enum values (using MyEnum::get?). REGULAR_ENUM just adds scope so you access it like MyEnum::myEnumItem instead of needing to prepend a name before each item (ie ct for ControlType).

They are defined similar to an enum. ControlType is the name of the enum and the following are the item names. (eg case ControlType::CLOSED:  ). Can you use an = after the item to give it a custom value?

Here's the discussion on it:
https://forum.megaglest.org/index.php?topic=4171.msg22828#msg22828
https://forum.megaglest.org/index.php?topic=4644.0

It might be good to have a short tutorial on how to use it in the Wiki.
« Last Edit: 14 February 2010, 23:50:00 by hailstone »
Glest Advanced Engine - Admin/Programmer
https://sourceforge.net/projects/glestae/

silnarm

  • Local Moderator
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: REGULAR_ENUM--STRINGY_ENUM ...
« Reply #2 on: 15 February 2010, 00:40:09 »
Here's the discussion on it:
https://forum.megaglest.org/index.php?topic=4171.msg22828#msg22828
https://forum.megaglest.org/index.php?topic=4644.0
Warning: Development discussion, contains much macro magic and a bit of template trickery, not recommended for people new to C++, and not required knowledge.

Quote
It might be good to have a short tutorial on how to use it in the Wiki.
Yes, I've been meaning to do just that, with Yggdrasil recently joining us, and now softcoder and titi looking over our source, this is a very good idea. Will make this a priority.

In the meantime here is the low-down,

STRINGY_ENUM is for enumerations where you need to print the value of the enumeration somewhere, and/or you need to match a string (ie, from XML) to get an enum value.
These must be in game_constants.h.  They are scoped, so they must be accessed like in Java or C#, but of course with the C++ scope resolution operator, ControlType::CPU_MEGA for example.  The string support is supplied by a ancillary class, which is the enum name + 'Names', you use the [] operator to get a string, ie. ControlTypeNames[myControlType]. To match a enum from a string you use, ControlTypeNames.match(mystring).

REGULAR_ENUM is for enumerations where you do not need the string support in a release build, the Names classes are not generated for them, if NDEBUG is defined. Otherwise they are STRINGY, so you can use the Names classes in debug code. Again, these must be in game_constants.h.

For enums where you never ever want string support, you can use WRAPPED_ENUM, these never have the Names class built, and can be declared anywhere.

Quote from: hailstone
Can you use an = after the item to give it a custom value?
No. I did think of a way to do this, and to support automagic 'flag sets' (ie, enums with values 1, 2, 4, 8, etc) but at this time the enums are all sequentially numbered from 0, and also have INVALID = -1 added before the first value supplied, and COUNT appended to the end of the value list.

[Edit: typos]
« Last Edit: 15 February 2010, 10:04:29 by silnarm »
Glest Advanced Engine - Code Monkey

Timeline | Downloads