Author Topic: Numbering enumeration in C++  (Read 5484 times)

asandler

  • Senior Community Member
  • Posts: 276
  • Hero Points: 24
Numbering enumeration in C++
« on: April 01, 2010, 07:27:41 am »
I didn't find this macro using search, so I thought I'd ask before writing one myself.
I am looking for a macro that enumerates values in C++ enumerations. I.e. given:

enum Enumeration {
    FIRST,
    SECOND,
    THIRD
};

the macro would turn it into:

enum Enumeration {
    FIRST = 1,
    SECOND = 2,
    THIRD = 3
};

Ideally, it should show a dialog asking for initial value and step size. Anyone has anything like this macro?

Thanks.
Alex.

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Numbering enumeration in C++
« Reply #1 on: April 01, 2010, 05:36:26 pm »
I have a macro that might be helpful, but first a couple thoughts:

C++ automatically starts an enum at 0, and automatically increments each subsequent value.
So functionally the only thing you need to do is set the initial value.

SlickEdit understands enums and shows in a hover tooltip the numeric value of any enum value.
So for working with the code in SlickEdit the only thing you need to do is set the initial value.

>> And there is real danger in explicitly setting the numeric value of each entry in an enum, as my team has learned the hard way in the past:  when someone later adds a new value to the enum, it is easy to forget (or not realize the need) to manually renumber all the existing subsequent values, resulting in duplicate values.  And if two people add values around the same time one will get a merge conflict at checkin time, and apparently it can be difficult to correctly renumber the values while resolving a merge conflict.  These lead to product bugs, and they can be very subtle and/or disastrous.

Just some things to keep in mind before setting explicit values in an enum.

Anyway, here is a macro that gets the number under the cursor, moves down a line, adds 1 to the number, and inserts the new value (using a correct run of tabs or spaces).  It recognizes base 10 numbers, or base 16 numbers prefixed by "0x" or "h".  I've not used it in an enum, but I do use it in columnar data sets sometimes.  Perhaps you can use the macro as-is, or use it as a starting point for another macro.  Though I really can't recommend assigning explicit values in an enum, after having experienced the effects of doing so.

Load both macro modules, in order for the IncrementNext command to work.

(When a file is configured to use tabs for indent, for some reason SlickEdit requires macros to manually insert preceding tabs when inserting text, and SlickEdit does not seem to have a function to correctly calculate the necessary run of tabs/spaces unless the run starts at the left margin -- so the IncrementNext macro depends on the EnsureRealIndent macro -- both must be loaded).

Graeme

  • Senior Community Member
  • Posts: 2102
  • Hero Points: 253
Re: Numbering enumeration in C++
« Reply #2 on: April 01, 2010, 10:46:27 pm »
You can also use the gui-enumerate command or enumerate command to create a sequence of numbers in the current selection.  Maybe this command should be on the tools menu.  I use it a fair bit, as well as block editing.

Graeme

asandler

  • Senior Community Member
  • Posts: 276
  • Hero Points: 24
Re: Numbering enumeration in C++
« Reply #3 on: April 02, 2010, 09:00:06 am »
Thanks a lot guys. I'll give both ways a try and post some comments.
Will have to wait until Tuesday though, because of Passover ::)

asandler

  • Senior Community Member
  • Posts: 276
  • Hero Points: 24
Re: Numbering enumeration in C++
« Reply #4 on: April 08, 2010, 01:49:00 pm »
Both methods work. I like gui-enumerate more because it is available out of the box. Otherwise it works either way.
So, thanks a lot to both of you guys.