peterchen wrote:
I'd put the list of enum definitions into a separate file [and #include it multiple times]
I sometimes use that technique. It wreaks havoc with some compilers' efforts to optimize #include file performance (e.g. it prevents the use of #pragma once) but it has the advantage of eliminating a lot of annoying semicolons. Both the multi-#include and the multi-line #define approaches have a certain clunkiness to them, but I've not decided which bit of clunkiness I dislike more. BTW, on one project, it was necessary for a somewhat substantial piece of performance-critical code to operate essentially identically on two data structures. The normal approach would have been to have a routine take a pointer to a structure, and then perform all operations using that structure. An alternative approach would have been for the routine to have a static copy of the structure; to update both structures the code could then copy structure #1 into that static copy, operate upon it, and copy it back, then do likewise for structure #2. This would probably have offered better performance than doing all operation using pointers (on the Z80, using 16-bit integers and pointers would have been rather slow):
/* foo.i1 = foo.i2 + foo.i3; */
/* About 16+16+8+16 = 56 cycles I think */
ld hl,(foo.i2)
ld de,(foo.i3)
add hl,de
ld (foo.i1),hl
/* bar->i1 = bar->i2 + bar->i3 */
/* About 20+14+14+14+14+14+14 = 104 cycles I think */
/* Note I doubt the compiler would produce code this good */
ld iy,(foo)
ld a,(iy+.i2)
add a,(iy+.i3)
ld (iy+.i1),a
ld a,(iy+.i2+1)
adc a,(iy+.i3+1)
ld (iy+.i1),a
but the performance overhead for having to do copy the data structure four times for every system event cycle would still have been substantial. Instead, I #included the code for the routine twice, with a few #define macros set differently for each invocation. (BTW, I just realized that since I defined the PLD for memory addressing, I could perhaps have tweaked it so that a 4K slice of address space could point to two different blocks of memory, and then put the data structure there. Water under the bridge now.)