Jump to content

[SOLVED] exporting functions from C++ to script with non basic type parameters


photo

Recommended Posts

I am trying to export a class from C++ to script but I am running into trouble when the parameters are something else then simple basic types as int or float etc.

 

for instance:

class cMyClass
{
    public:

        ...
        void SetSystemType (cMyClass::tSystemType type) ;
} ;


Interpreter::addExternFunction ("engine.myclass.SetSystemType", MakeExternObjectFunction (&m_myclass, &cMyClass::SetSystemType)) ;

will result in (g++, linux):

./unigine/include/UnigineInterpreter.h: In instantiation of ‘Unigine::VariableToType<Type>::VariableToType(void*, const Unigine::Variable&) [with Type = cMyClass::tSystemType]’:
./unigine/include/UnigineInterpreter.h:2123:36:   required from ‘Unigine::Variable Unigine::ExternObjectFunction<Class, TList, Func>::do_run(void*, Unigine::Variable*, Unigine::TypeToType<void>, Unigine::IntToType<1>) const [with Class = cMyClass; TList = Unigine::TypeList<void, Unigine::TypeList<cMyClass::tSystemType, Unigine::TypeListEnd> >; Func = void (cMyClass::*)(cMyClass::tSystemType)]’
./unigine/include/UnigineInterpreter.h:2105:63:   required from ‘Unigine::Variable Unigine::ExternObjectFunction<Class, TList, Func>::run(void*, Unigine::Variable*) const [with Class = cMyClass; TList = Unigine::TypeList<void, Unigine::TypeList<cMyClass::tSystemType, Unigine::TypeListEnd> >; Func = void (cMyClass::*)(cMyClass::tSystemType)]’
4simappwidget.cpp:1218:1:   required from here
./unigine/include/UnigineInterpreter.h:964:47: error: invalid cast from type ‘void*’ to type ‘cMyClass::tSystemType’

At first look it seems caused by Unigine::Variable only supporting the types in the enum in UnigineInterpreter.h ?

 

Note that I could of course change this function to take an int and convert internally, but I would very much prefer not to (and in cases where source changes are not possible it isn't an option anyway). Also, the same class also has functions taking std::string for which export results in similar error.

 

 

How to export functions with non basic arguments? (enum, std::string, stl/Qt types, etc) Can Unigine::Variable be (easily) extended (from C++ api)?

 

Or does everything with non basic type needs to be wrapped in wrapper class or functions?

 

 

 

 

 

 

Link to comment

As a related question, if I have:

class cMyClass
{
    public:

        ....
        void SetAddress (std::string address) ;
        void SetAddress (const char* address) ;
} ;

and I would want to export the second one taking a const char* how do I specify that I want that one in MakeExternObjectFunction?

Link to comment

 

First question, have a look to %UNIGINE_DIR%\source\samples\Api\Classes\Classes.cpp. It this what do you need?
For the second question this link.

 

 

In that sample there are no functions being exported with non basic type arguments (MyExternObjectGetSize & MyExternObjectSetSize do take a MyExternObject*, but that can easily be cast to void*)

 

I think you forgot the link? ;)

 

Please pass the arguments as references or pointers.

 

Thanks, with the above functions it indeed compiles when using const&.

 

But this is only an option if changing the source is possible. If not (no source), or if changing the source makes no sense (for instance when it is more widely used elsewhere) wrapping those functions is really the only option then?

 

Still curious as well how to select a specific method in case of polymorphism?

Link to comment

to answer my own question:

class Foo
{
    public:

        int f (const char* str)  { printf ("const char*\n") ; }
        int f (int str)         { printf ("int\n") ; }
};


int main ()
{
    int (Foo::* fptr1) (const char *) = &Foo::f ;
    int (Foo::* fptr2) (int) = &Foo::f ;

    Foo foo ;

    (foo.*fptr1) ("str") ; // prints "const char *"
    (foo.*fptr2) (3) ; // prints "int"
}

Selection of which 'f' we want is done not in the assignment but in the type declaration. But in MakeExternObjectFunction only assignment is done. So if a suitable MakeExternObjectFunction specialisation is available we need to select it with a cast?

class cMyClass
{
    public:

        ....
        void SetAddress (std::string address) ;
        void SetAddress (const char* address) ;
} ;


MakeExternObjectFunction (&m_myclass, static_cast<void (cMyClass::*) (const char*)>  (&cMyClass::SetAddress))) ;

This seems to work (or at least compile).

Link to comment

ah, that part of the documentation had escaped my attention, thanks.

 

confirms my thoughts about needing a cast. (Note though that the documentation there mentions only static members, for non-static members the cast is slightly different.)

Link to comment
×
×
  • Create New...