Coder Social home page Coder Social logo

Custom function about my_basic HOT 1 CLOSED

paladin-t avatar paladin-t commented on May 28, 2024
Custom function

from my_basic.

Comments (1)

paladin-t avatar paladin-t commented on May 28, 2024

Hi,

It's not able to call a non-static method without an instance in C++, I mean you can't simply do that. But we are still able to do it with some tricks. The problem is C++ needs to know which instance a method will execute with, so we can store an instance and tell C++.

With MY-BASIC, for example, assume we got a C++ class:

class AClass {

public:
    void foo(void) const;

};

Now I'd define some bridge function:

int _a_new_instance(mb_interpreter_t* s, void** l) {
    // Creates an instance of AClass.

    mb_value_t ret;

    ...

    mb_check(mb_attempt_open_bracket(s, l));

    mb_check(mb_attempt_close_bracket(s, l));

    ret.type = MB_DT_USERTYPE;
    ret.value.usertype = (void*)(new AClass);

    ...
}

int _a_foo_bridge(mb_interpreter_t* s, void** l) {
    // Calls the foo method of an AClass instance.

    mb_value_t ret;
    AClass* inst = NULL;

    ...

    mb_check(mb_attempt_open_bracket(s, l));

    mb_check(mb_pop_value(s, l, &a));

    mb_check(mb_attempt_close_bracket(s, l));

    inst = (AClass*)a.value.usertype;
    inst->foo();

    ...
}

Don't forget to register them. Then in MY-BASIC code:

a = a_new_instance()
a_foo_bridge(a) ' Calls the foo method.

This is not the only way to call a non-static C++ method with C, it's just a simple practice. You could find a proper way by searching something like "call c++ method from c". There are also many ways to store a non-static method and its corresponding instance in a variable, you may would like to take a look at "function bind" if you were using C++11, or use the famous very lightweight Fastdelegate lib for C++.

BTW. MB_DT_USERTYPE_REF is more intelligent than MB_DT_USERTYPE, because MY-BASIC can manage the deallocation of a referenced object with Reference Counting and GC.

Best
WRX

from my_basic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.