These are chat archives for ChaiScript/ChaiScript

9th
Jan 2016
Ivan
@Rexagon
Jan 09 2016 09:36
Is it possible to export enum to script? And how to use it inside script
Sandu Liviu Catalin
@iSLC
Jan 09 2016 14:49

@Rexagon I don't think you can do that directly. However, I was able to emulate them using classes and global variables in order to scope them.

#include <chaiscript/dispatchkit/bootstrap.hpp>

struct BasicEnum
{
    const int mA, mB, mC;

    BasicEnum()
        : mA(1) , mB(2) , mC(3)
    { }
};

static const BasicEnum g_BasicEnum;

ModulePtr Register_Constants()
{
    ModulePtr mod(new chaiscript::Module());
    // You don't really care about the class name
    mod->add(chaiscript::user_type< BasicEnum >(), "INTERNAL_BasicEnum");

    mod->add(chaiscript::fun(&BasicEnum::mA),    "A");
    mod->add(chaiscript::fun(&BasicEnum::mB),    "B");
    mod->add(chaiscript::fun(&BasicEnum::mC),    "C");

    mod->add_global_const(chaiscript::Boxed_Value(&g_BasicEnum), "MyEnum");

    return mod;
}

Using it:

print(MyEnum.A);
print(MyEnum.B);
print(MyEnum.C);

The benefit is that you can use pretty much any type you like and not just integers.

Ivan
@Rexagon
Jan 09 2016 19:13
It was hard to bind ~150 constants (keyboard enum), but this solution works fine, thanks!