Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

Sqrat

<< < (3/3)

thomas:

--- Quote from: MortenMacFly on June 27, 2012, 04:45:12 pm ---Can you provide a test case of all three solutions? For the enums: What do we do currently with SQPlus (I cannot have a look atm...)?
--- End quote ---
What we currently do with SQPlus is a macro (one invocation per enum type) that expands to code like this:

--- Code: ---namespace SqPlus \
{ \
    inline void Push(HSQUIRRELVM v,T value) { sq_pushinteger(v,value); } \
    inline bool Match(TypeWrapper<T>, HSQUIRRELVM v, int idx) { return sq_gettype(v,idx) == OT_INTEGER; } \
    inline T Get(TypeWrapper<T>,HSQUIRRELVM v,int idx) { SQInteger i; SQPLUS_CHECK_GET(sq_getinteger(v,idx,&i)); return (T)i; } \
}
--- End code ---
Which, if I understand correctly, provides a kind of "getter function" for the compile-time constant, which I guess is evaluated at runtime by the SQPlus layer... though I don't quite get how it matches enum names...

The "maintain separately " solution would look something like this:

--- Code: ---// File printing_types.h
enum PrintColourMode
{
    pcmBlackAndWhite,
    pcmColourOnWhite,
    pcmInvertColours,
    pcmAsIs
};


// File bindings/sc_base_types.h
Sqrat::ConstTable().Enum(_SC("PrintColourMode"), Sqrat::Enumeration() .Const(_SC("pcmBlackAndWhite"), pcmBlackAndWhite)
Sqrat::ConstTable().Enum(_SC("PrintColourMode"), Sqrat::Enumeration() .Const(_SC("pcmColourOnWhite"), pcmColourOnWhite)
Sqrat::ConstTable().Enum(_SC("PrintColourMode"), Sqrat::Enumeration() .Const(_SC("pcmInvertColours"), pcmInvertColours)
Sqrat::ConstTable().Enum(_SC("PrintColourMode"), Sqrat::Enumeration() .Const(_SC("pcmAsIs"), pcmAsIs)

--- End code ---
If someone adds an enumeration on one end and forgets the other, it all crashes and burns. Also, you have to do this once for every single enum value in every single enum type.


... and the X-Macro solution would look something like this:

--- Code: ---// File print_colour_mode.h
X(pcmBlackAndWhite, = 5)
X(pcmColourOnWhite, )
X(pcmInvertColours, )
X(pcmAsIs,          )


// File printing_types.h
#define END        END2(__LINE__, __COUNTER__) /* need this to get rid of the last comma */
#define END2(a,b)  END3(a,b)
#define END3(a,b)  _##a##b##_

#define X(a, b) a b,
enum PrintColourMode
{
#include "print_colour_mode.h"
END
};
#undef X


// File sc_base_types.h
#define X(a, b) .Const(_SC(#a), a)
void bind_enums()
{
Sqrat::ConstTable().Enum
(
_SC("PrintColourMode"), Sqrat::Enumeration()
#include "print_colour_mode.h"
);
}
#undef X
--- End code ---
This is not really C++ any more, but surprisingly it works...

The code would be somewhat less ugly if enums always started at zero and were contiguous ... but alas, life is not fair...

MortenMacFly:

--- Quote from: thomas on June 27, 2012, 07:41:41 pm ---[...] all the options [...]

--- End quote ---
Well to me all look acceptable to me. ::)

Navigation

[0] Message Index

[*] Previous page

Go to full version