https://bugs.documentfoundation.org/show_bug.cgi?id=168771

            Bug ID: 168771
           Summary: Convert enum to enum class
           Product: LibreOffice
           Version: unspecified
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: normal
          Priority: medium
         Component: LibreOffice
          Assignee: [email protected]
          Reporter: [email protected]

Description:
Since C++11 when enum class (also named scoped enum) is introduced, it is
preferred to the plain enum which is inherited from C programming languages.
The task here is to convert the old enums to enum class.

Rationale:
enum class has many benefits when compared to plain enum, as it provides better
type safety among other things. Implicit conversion to integers, lack of
ability to define the underlying data type and compatibility issues are some of
the problems with plain enum that enum class solves.

Plain enums pollute namespace, and you have to pick names that are too long,
and have to carry the context inside their names. For example:
INETMSG_RFC822_BEGIN inside enum _ImplINetRFC822MessageHeaderState. With an
enum class, it is simply written as HeaderState::BEGIN. When placed inside a
file/class/namespace that makes it relevant, it is much easier to use: it is
more readable, and causes no issues for other identifiers with possible similar
names.,

commit 593f08303fef14243c8ee2ec4e1c1912628f59e8
convert enum _ImplINetRFC822MessageHeaderState to enum class

You can read more about that in:

Why is enum class considered safer to use than plain enum?
https://stackoverflow.com/questions/18335861/why-is-enum-class-considered-safer-to-use-than-plain-enum

Finding Instances:
You may find some of the instances with:

$ git grep -w enum *.cxx *.hxx|grep -v "enum class"

When you count it with 'wc -l', it shows something more than 2k instances.
Please note that sometimes this conversion can reduce readability because of
multiple "static_cast"s in return of no obvious gain. Then, let it be as is and
pick another instance.

Examples Commits:
You can see some of the previous conversions here, which is around 1k changes:

$ git log --oneline -i -E --grep="convert enum|scoped enum"

This is a good, but lengthy example of such a conversion:

commit 9072c5c8551c0bc512865ab15b1054c78706f1f3
convert SbxFlagsBits to scoped enum

Implementation:
First of all, please choose good names for the new enum class and values. For
example, you may convert APPLICATION_WINDOW_TITLE into
Application::WindowTitle. Therefore, it is suggested not to use the old names
as they were.

Converting enum to enum class is not always straightforward. You should try to
understand the code using the enum, and then try to replace it with enum class.
You may need to add extra state/values for situations where 0 or -1 or some
default value was used. There are cases where a numerical value is used for
different conflicting purposes, and then you have to do some sort of conflict
resolution to separate those cases.

You may end up modifying more and more files, and a few static_casts where they
are absolutely necessary because you are interpreting some integer value read
from input. These are the places where you should check the values yourself in
the code. You have to make sure that the numerical value is appropriate before
casting it to the enum class.

If you want to do bitwise operations, you should use o3tl::typed_flags, for
example:

template<> struct o3tl::typed_flags<FileViewFlags> :
o3tl::is_typed_flags<FileViewFlags, 0x26> {}

Please note that 0x26 is the mask, and is calculated by applying OR over all
possible values. All the values must be non-negative.

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to