Jump to content

Scoped enums


klepto2
 Share

Recommended Posts

Currently the enums in UAK are defined as unscoped enums like this:

enum TextFieldStyle
	{
		TEXTFIELD_READONLY = 1,
		//TEXTFIELD_PASSWORD = 2,
		TEXTFIELD_TEXTCHANGEACTIONEVENT = 4,
		TEXTFIELD_LOSEFOCUSACTIONEVENT = 8,
		TEXTFIELD_ENTERKEYACTIONEVENT = 16,
		TEXTFIELD_DEFAULT = TEXTFIELD_LOSEFOCUSACTIONEVENT + TEXTFIELD_ENTERKEYACTIONEVENT,
		//TEXTFIELD_NUMBER = 8,
		//TEXTFIELD_INTEGER = 16,
		//TEXTFIELD_FILEPATH = 32,
		TEXTFIELD_PASSWORD = 64
	};

I suggest to follow the common c++ suggestions to use scoped enums instead:

enum class TextFieldStyle
{
		ReadOnly = 1,
		Password = 2,
		TextChangeActionEvent = 4,
		LoseFocusActionEvent = 8,
		EnterKeyActionEvent = 16,
		Default = LoseFocusActionEvent + EnterKeyActionEvent,
};

The scoped enums have much more possibilities:

1. You can have the same enum-value name across multiple enums (eg Default or Left) 

2. enums can have a different value type instead of just int

3. the code completion is much more intuitive in most IDEs.

4. the code is more readable (in my opinion)

  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

This is how Leadwerks did things, except it used const static members. I much prefer the uppercase-separated-by-underscore approach we are using now. (The enum name like "TextFieldStyle" is only used to ensure that wrong values cannot be inserted, like supplying WINDOW_HIDDEN to CreatePanel().)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

static const and scoped enums are 2 different things. the current enums look like a mix of enums names for constants. Normally only constants are uppercase. The thing is you don't need the uppercase-separated-by-underscore approach. You can keep the uppercase names of course:

CreateTextfield(0,0,250,20,ui->root, TextfieldStyle::DEFAULT); // you can now clearly say what enum is used.

with your approach you can't have something like this:

enum VerticalAlignment
{
  TOP,BOTTOM,CENTER
};

enum HorizontalAlignment
{
  LEFT,RIGHT,CENTER
};

which leads to the need of the underscore approach.

with scoped enums this is legal:

enum class VerticalAlignment
{
  TOP,BOTTOM,CENTER
};

enum class HorizontalAlignment
{
  LEFT,RIGHT,CENTER
};

as mentioned in the above post in my opinion it feels cleaner and is more collision safe with other libraries.

  • Like 1
  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...