Giter Site home page Giter Site logo

rasiaht / cordova-plugin-cpp-bridge Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tkyaji/cordova-plugin-cpp-bridge

0.0 2.0 0.0 23 KB

This plugin makes it easy to call C++ from Javascript.

License: Apache License 2.0

C++ 0.65% JavaScript 97.33% CMake 0.33% C 0.69% Objective-C++ 1.00%

cordova-plugin-cpp-bridge's Introduction

cordova-plugin-cpp-bridge

This plugin makes it easy to call C++ from Javascript.

Platform

  • iOS
  • Android
  • OSX

Usage

1. Add Plugin

cordova plugin add cordova-plugin-cpp-bridge

2. Add C++ Files to cpp directory

The cpp directory has been added to your project when adding this plugin.

ex.

TestCpp.h

class TestCpp {
    public:
    int testMethod(int p1, const char *p2, double p3, bool p4);
    static const char* staticTestMethod();
};

TestCpp.cpp

#include "TestCpp.h"
#include "NativeLog.h"

int TestCpp::testMethod(int p1, const char *p2, double p3, bool p4) {
    _log("testMethod: %d, %s, %lf, %d", p1, p2, p3, p4);
    return p1 + 1;
}

const char* TestCpp::staticTestMethod() {
    return "staticTestMethod called!";
}

3. Edit class_define.json

Describe the interface of the class in class_define.json. This file is created in the cpp directory.

class_define.json

{
    "header_files": [<header file>, ...],
    "source_files": [<source file>, ...],

    "classes": {
        "<class name>": {
            "methods": {
                "<method name>": {
                    "params": [<int / double / string / boolean>, ...],     // optional
                    "return": "<void / int / double / string / boolean>",   // optional (default:void)
                    "is_static": <true / false>                             // optional (default:false)
                }
                , ...
            }
        }
        , ...
    }
}

ex.

{
    "header_files": ["TestCpp.h"],
    "source_files": ["TestCpp.cpp"],

    "classes": {
        "TestCpp": {
            "methods": {
                "testMethod": {
                    "params": ["int", "string", "double", "boolean"],
                    "return": "int"
                },
                "staticTestMethod": {
                    "is_static": true,
                    "return": "string"
                }
            }
        }
    }
}

4. Call cpp method from JavaScript.

ex.

cpp.TestCpp.new(function(testCpp) {
    testCpp.testMethod(999, "message", 1.111, true, function(ret) {
        console.log('TestCpp#testMethod : ' + ret);
    });
});

cpp.TestCpp.staticTestMethod(function(ret) {
    console.log('TestCpp#staticTestMethod : ' + ret);
});

5. Build

cordova build ios / android

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.