Exporting Member variable with self-defined type fails, while basic-type not

Issue #8 resolved
Oscar Zhao
created an issue

This is a more precise description of issue #7. I succeeded export member variables with basic types( int, double, string, etc.) but failed to export self-defined types ( self-defined classes). The error from visual studio 2008 is a compile error. Here I try my best to state my question clearly, hoping to get some help, thanks. The test code is as following:

#include "oolua.h"
#include "oolua_dsl.h"
#include "oolua_dsl_export.h"
#include <iostream>

/**
 *  export self-defined type
 */
struct Mouse
{
public:
    Mouse():m_strColor("black"), m_strBrand("Dell") {}
    std::string m_strColor;
    std::string m_strBrand;
};

struct Computer{
public:
    double          m_dLengthMeter;
    double          m_dWidthMeter;
    double          m_dHeightMeter;
    std::string     m_strBrand;
    std::string     m_strModel;
    std::string     m_strSerialNumber;
    std::string     m_strDateProduced;
    // self-defined type member variable
    Mouse           m_mouse;

    Computer():m_dLengthMeter(0.5), m_dWidthMeter(0.3), m_dHeightMeter(0.3)
        , m_strBrand("Acer"), m_strModel("4741g"), m_strSerialNumber("100001")
        , m_strDateProduced("2014-09-23")
    {}

    bool isWorking(){return true;}
    bool isDell(){return true;}
    bool isLenovo(){return true;}
    bool isApple(){return true;}
    bool isAcer(){return true;}
    bool func0(){return true;}
    bool func1(){return true;}
    bool func2(){return true;}
    bool func3(){return true;}

};

// Proxy

OOLUA_PROXY(Mouse)
OOLUA_PROXY_END

OOLUA_PROXY(Computer)
    OOLUA_MGET_MSET(m_dLengthMeter)     // works
    OOLUA_MGET_MSET(m_dWidthMeter)      // works
    OOLUA_MGET_MSET(m_dHeightMeter)     // works
    OOLUA_MGET_MSET(m_strBrand)         // works
    OOLUA_MGET_MSET(m_strModel)         // works

    // compile error, "error C2440: 'static_cast' : cannot convert from 'lua_Integer' to 'Mouse'"
    OOLUA_MGET_MSET(m_mouse)   

    OOLUA_MFUNC(isWorking)
    OOLUA_MFUNC(isDell)
    OOLUA_MFUNC(isAcer)
    OOLUA_MFUNC(isApple)
    OOLUA_MFUNC(isLenovo)

    OOLUA_MFUNC(func0)
    OOLUA_MFUNC(func1)
    OOLUA_MFUNC(func2)
    OOLUA_MFUNC(func3)
OOLUA_PROXY_END

// Export
OOLUA_EXPORT_NO_FUNCTIONS(Mouse)

OOLUA_EXPORT_FUNCTIONS(Computer
                       , isWorking, isDell, isAcer, isApple, isLenovo
                       , set_m_dLengthMeter, set_m_dWidthMeter, set_m_dHeightMeter
                       , set_m_strBrand, set_m_strModel
                       , set_m_mouse  // export setter
                       , func0, func1, func2, func3
                       )
OOLUA_EXPORT_FUNCTIONS_CONST(Computer
                       , get_m_dLengthMeter, get_m_dWidthMeter, get_m_dHeightMeter
                       , get_m_strBrand, get_m_strModel
                       , get_m_mouse // export getter
                       )


int main()
{
    OOLUA::Script* vm = new OOLUA::Script;
    vm->register_class<Computer>();

    /**
     *  code to use class Computer in lua
     */
    vm->run_chunk(
        "function func() "
        "com = Computer.new() "
        "com:set_m_strBrand(\"Dell\") "
        "w = com:get_m_strBrand() "
        "return w "
        "end ");
    vm->call("func");

    std::string brand;
    OOLUA::pull(*vm, brand);
    std::cout << "Brand: " << brand << std::endl;

    system("pause");

    delete(vm);
    return 0;
}

The error message is:

1>------ Build started: Project: TestProxy, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>e:\work\libs\trunk\include\oolua\stack_get.h(121) : error C2440: 'static_cast' : cannot convert from 'lua_Integer' to 'Mouse'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>        e:\work\libs\trunk\include\oolua\stack_get.h(117) : while compiling class template member function 'void OOLUA::INTERNAL::LUA_CALLED::get_basic_type<T,is_integral,is_convertable_to_int>::get(lua_State *const ,int,T &)'
1>        with
1>        [
1>            T=Mouse,
1>            is_integral=1,
1>            is_convertable_to_int=1
1>        ]
1>        e:\work\libs\trunk\include\oolua\stack_get.h(136) : see reference to class template instantiation 'OOLUA::INTERNAL::LUA_CALLED::get_basic_type<T,is_integral,is_convertable_to_int>' being compiled
1>        with
1>        [
1>            T=Mouse,
1>            is_integral=1,
1>            is_convertable_to_int=1
1>        ]
1>        e:\work\libs\trunk\include\oolua\stack_get.h(134) : while compiling class template member function 'void OOLUA::INTERNAL::LUA_CALLED::get_basic_type<T,is_integral,is_convertable_to_int>::get_imp(lua_State *const ,int,T &,LVD::Int2type<0>)'
1>        with
1>        [
1>            T=Mouse,
1>            is_integral=0,
1>            is_convertable_to_int=0
1>        ]
1>        e:\work\libs\trunk\include\oolua\stack_get.h(210) : see reference to class template instantiation 'OOLUA::INTERNAL::LUA_CALLED::get_basic_type<T,is_integral,is_convertable_to_int>' being compiled
1>        with
1>        [
1>            T=Mouse,
1>            is_integral=0,
1>            is_convertable_to_int=0
1>        ]
1>        e:\work\git\oolua\ooluatest\src\testproxy\main.cpp(64) : see reference to function template instantiation 'void OOLUA::INTERNAL::LUA_CALLED::get<Mouse>(lua_State *const ,int,T &)' being compiled
1>        with
1>        [
1>            T=Mouse
1>        ]
1>Build log was saved at "file://E:\work\Git\OOLua\OOLuaTest\_builder\Debug\TestProxy\BuildLog.htm"
1>TestProxy - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Comments (3)

  1. Liam Devine repo owner

    Oscar thank you for the bug report and test case. Please do not forget that in order to use the type (Mouse) it also needs registering with the Lua state.

  2. Log in to comment