├── .clang-format
├── .github
└── workflows
│ └── CI.yml
├── .gitignore
├── .releaserc
├── .vscode
├── c_cpp_properties.json
├── settings.json
└── tasks.json
├── CMakeLists.txt
├── LICENSE
├── README.md
├── cmake
├── genepiConfig.cmake.in
└── utils.cmake
├── examples
├── CMakeLists.txt
├── classes
│ ├── classes.cpp
│ └── classes.js
├── functions
│ ├── functions.cpp
│ └── functions.js
├── index.js
├── inherit
│ ├── inherit.cpp
│ └── inherit.js
├── methods
│ ├── methods.cpp
│ └── methods.js
├── objects
│ ├── objects.cpp
│ └── objects.js
├── overloaded-functions
│ ├── overloaded-functions.cpp
│ └── overloaded-functions.js
└── overloaded-methods
│ ├── overloaded-methods.cpp
│ └── overloaded-methods.js
├── include
└── genepi
│ ├── arg_from_napi_value.h
│ ├── bind_class.h
│ ├── bind_class_base.h
│ ├── binding_std.h
│ ├── binding_type.h
│ ├── callable.h
│ ├── caller.h
│ ├── checker.h
│ ├── class_definer.h
│ ├── class_wrapper.h
│ ├── common.h
│ ├── creator.h
│ ├── function_definer.h
│ ├── function_definition.h
│ ├── genepi.h
│ ├── genepi_registry.h
│ ├── method_definition.h
│ ├── signature
│ ├── base_signature.h
│ ├── constructor_signature.h
│ ├── function_signature.h
│ ├── method_signature.h
│ ├── signature_param.h
│ └── templated_base_signature.h
│ ├── singleton.h
│ ├── type_list.h
│ ├── type_transformer.h
│ └── types.h
├── package-lock.json
├── package.json
└── src
└── genepi
├── common.cpp
├── genepi_registry.cpp
└── singleton.cpp
/.clang-format:
--------------------------------------------------------------------------------
1 | # clang-format configuration file based on the version 3.8
2 | # releases.llvm.org/3.8.0/tools/clang/docs/ClangFormatStyleOptions.html
3 | #BasedOnStyle (desactivated since we want to control everything [BC])
4 | AccessModifierOffset: -4
5 | AlignAfterOpenBracket: DontAlign
6 | AlignConsecutiveAssignments: false
7 | AlignConsecutiveDeclarations: false
8 | # If AlignEscapedNewlinesLeft is false, that put as far right as possible,
9 | # else left. In newer clang-format version,
10 | #use AlignEscapedNewlines: Right instead.
11 | AlignEscapedNewlinesLeft: false
12 | AlignOperands: true
13 | AlignTrailingComments: false
14 | AllowAllParametersOfDeclarationOnNextLine: true
15 | AllowShortBlocksOnASingleLine: true
16 | AllowShortCaseLabelsOnASingleLine: false
17 | AllowShortFunctionsOnASingleLine: Empty # pb with 3.5
18 | AllowShortIfStatementsOnASingleLine: false
19 | AllowShortLoopsOnASingleLine: false
20 | #AlwaysBreakAfterDefinitionReturnType (not set because deprecated [BC])
21 | AlwaysBreakAfterReturnType: None
22 | AlwaysBreakBeforeMultilineStrings: false
23 | AlwaysBreakTemplateDeclarations: true
24 | BinPackArguments: true
25 | BinPackParameters: false
26 | BraceWrapping:
27 | AfterClass: true
28 | AfterControlStatement: true
29 | AfterEnum: true
30 | AfterFunction: true
31 | AfterNamespace: true
32 | AfterObjCDeclaration: true
33 | AfterStruct: true
34 | AfterUnion: true
35 | BeforeCatch: true
36 | BeforeElse: true
37 | IndentBraces: false
38 | #SplitEmptyFunctionBody: false (this option provokes a clang-format error [BC])
39 | BreakBeforeBinaryOperators: NonAssignment # pb with 3.5
40 | BreakBeforeBraces: Custom # pb with 3.5
41 | #BreakBeforeInheritanceComma: false (this option needs a newer clang version)
42 | BreakBeforeTernaryOperators: true
43 | # For newer clang version, consider the new macro:
44 | # BreakConstructorInitializers
45 | BreakConstructorInitializersBeforeComma: false
46 | #BreakStringLiterals
47 | ColumnLimit: 80
48 | CommentPragmas: ''
49 | #CompactNamespaces: false (this option is in newer clang version)
50 | ConstructorInitializerAllOnOneLineOrOnePerLine: true
51 | ConstructorInitializerIndentWidth: 4
52 | ContinuationIndentWidth: 4
53 | Cpp11BracedListStyle: false
54 | DerivePointerAlignment: true
55 | DisableFormat: false
56 | #ExperimentalAutoDetectBinPacking (Not defined since the documentation to not use it)
57 | #FixNamespaceComments: false (this option needs a newer clang version)
58 | #ForEachMacros: TODO
59 | #IncludeCategories: TODO
60 | #IncludeIsMainRegex (this option needs a newer clang version)
61 | IndentCaseLabels: false
62 | IndentWidth: 4
63 | IndentWrappedFunctionNames: true
64 | KeepEmptyLinesAtTheStartOfBlocks: false
65 | Language: Cpp
66 | #MacroBlockBegin TODO
67 | #MacroBlockEnd TODO
68 | MaxEmptyLinesToKeep: 1
69 | NamespaceIndentation: All
70 | #PenaltyBreakBeforeFirstCallParameter TODO
71 | #PenaltyBreakComment TODO
72 | #PenaltyBreakFirstLessLess TODO
73 | #PenaltyBreakString TODO
74 | #PenaltyExcessCharacter TODO
75 | #PenaltyReturnTypeOnItsOwnLine TODO
76 | PointerAlignment: Left
77 | ReflowComments: true # TODO check if 3.5 or 3.8
78 | #SortIncludes (this option needs a newer clang version)
79 | #SortUsingDeclarations (this option needs a newer clang version)
80 | SpaceAfterCStyleCast: true
81 | #SpaceAfterTemplateKeyword: false (this option needs a newer clang version)
82 | SpaceBeforeAssignmentOperators: true
83 | SpaceBeforeParens: Never
84 | SpaceInEmptyParentheses: false
85 | SpacesBeforeTrailingComments: 1
86 | SpacesInAngles: true
87 | SpacesInCStyleCastParentheses: false
88 | SpacesInContainerLiterals: false
89 | SpacesInParentheses: true
90 | SpacesInSquareBrackets: false
91 | Standard: Cpp11
92 | TabWidth: 4
93 | UseTab: Never
94 |
--------------------------------------------------------------------------------
/.github/workflows/CI.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | pull_request:
6 |
7 | jobs:
8 | build:
9 | runs-on: ${{ matrix.os }}
10 |
11 | strategy:
12 | matrix:
13 | node-version: [12.x, 14.x]
14 | os: [ubuntu-latest, windows-latest, macos-latest]
15 |
16 | steps:
17 | - uses: actions/checkout@v1
18 | - name: Use Node.js ${{ matrix.node-version }}
19 | uses: actions/setup-node@v1
20 | with:
21 | node-version: ${{ matrix.node-version }}
22 | - name: npm install and build
23 | run: |
24 | npm install
25 | npm run examples
26 |
27 | semantic-release:
28 | runs-on: ubuntu-latest
29 | needs: build
30 | steps:
31 | - uses: actions/checkout@v1
32 | - run: npx semantic-release
33 | env:
34 | GITHUB_TOKEN: ${{ secrets.TOKEN }}
35 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
36 | - name: Notify slack
37 | if: failure() && github.ref == 'refs/heads/master'
38 | uses: 8398a7/action-slack@v2
39 | with:
40 | status: failure
41 | env:
42 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43 | SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
44 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | build
2 | dist
3 | node_modules
4 | .vscode/ipch
--------------------------------------------------------------------------------
/.releaserc:
--------------------------------------------------------------------------------
1 | {
2 | "plugins": [
3 | '@semantic-release/commit-analyzer',
4 | '@semantic-release/release-notes-generator',
5 | '@semantic-release/github',
6 | '@semantic-release/npm'
7 | ]
8 | }
9 |
--------------------------------------------------------------------------------
/.vscode/c_cpp_properties.json:
--------------------------------------------------------------------------------
1 | {
2 | "configurations": [
3 | {
4 | "name": "Linux",
5 | "compilerPath": "/usr/bin/clang",
6 | "intelliSenseMode": "clang-x64"
7 | }
8 | ],
9 | "version": 4
10 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "C_Cpp.default.cppStandard": "c++11",
3 | "C_Cpp.default.includePath": [
4 | "${workspaceFolder}/include",
5 | "${workspaceFolder}/node_modules/node-addon-api",
6 | "${env:HOME}/.cmake-js/node-x64/v10.9.0/include/node"
7 | ],
8 | "C_Cpp.dimInactiveRegions": false,
9 | "files.associations": {
10 | "type_traits": "cpp",
11 | "array": "cpp",
12 | "functional": "cpp",
13 | "istream": "cpp",
14 | "ostream": "cpp",
15 | "tuple": "cpp",
16 | "utility": "cpp",
17 | "*.tcc": "cpp",
18 | "deque": "cpp",
19 | "vector": "cpp",
20 | "cctype": "cpp",
21 | "clocale": "cpp",
22 | "cmath": "cpp",
23 | "cstdarg": "cpp",
24 | "cstdint": "cpp",
25 | "cstdio": "cpp",
26 | "cstdlib": "cpp",
27 | "cstring": "cpp",
28 | "cwchar": "cpp",
29 | "cwctype": "cpp",
30 | "forward_list": "cpp",
31 | "unordered_map": "cpp",
32 | "unordered_set": "cpp",
33 | "exception": "cpp",
34 | "fstream": "cpp",
35 | "initializer_list": "cpp",
36 | "iosfwd": "cpp",
37 | "iostream": "cpp",
38 | "limits": "cpp",
39 | "memory": "cpp",
40 | "new": "cpp",
41 | "optional": "cpp",
42 | "sstream": "cpp",
43 | "stdexcept": "cpp",
44 | "streambuf": "cpp",
45 | "string_view": "cpp",
46 | "system_error": "cpp",
47 | "typeinfo": "cpp",
48 | "algorithm": "cpp",
49 | "cstddef": "cpp",
50 | "atomic": "cpp",
51 | "iterator": "cpp",
52 | "map": "cpp",
53 | "memory_resource": "cpp",
54 | "numeric": "cpp",
55 | "random": "cpp",
56 | "string": "cpp",
57 | "cinttypes": "cpp",
58 | "bit": "cpp"
59 | },
60 | }
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | // See https://go.microsoft.com/fwlink/?LinkId=733558
3 | // for the documentation about the tasks.json format
4 | "version": "2.0.0",
5 | "tasks": [
6 | {
7 | "type": "npm",
8 | "script": "build",
9 | "problemMatcher": [
10 | "$gcc"
11 | ],
12 | "group": {
13 | "kind": "build",
14 | "isDefault": true
15 | }
16 | }
17 | ]
18 | }
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2019 - 2021 Geode-solutions
2 | #
3 | # Permission is hereby granted, free of charge, to any person obtaining a copy
4 | # of this software and associated documentation files (the "Software"), to deal
5 | # in the Software without restriction, including without limitation the rights
6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | # copies of the Software, and to permit persons to whom the Software is
8 | # furnished to do so, subject to the following conditions:
9 | #
10 | # The above copyright notice and this permission notice shall be included in
11 | # all copies or substantial portions of the Software.
12 | #
13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | # SOFTWARE.
20 |
21 | cmake_minimum_required(VERSION 3.5)
22 |
23 | project(genepi)
24 |
25 | set(UTILS_FILE "${PROJECT_SOURCE_DIR}/cmake/utils.cmake")
26 | file(READ "${UTILS_FILE}" GENEPI_UTILS)
27 | include("${UTILS_FILE}")
28 |
29 | set(genepi_include_dir "${PROJECT_SOURCE_DIR}/include/genepi")
30 | set(genepi_source_dir "${PROJECT_SOURCE_DIR}/src/genepi")
31 | add_library(genepi "${genepi_source_dir}/genepi_registry.cpp")
32 | add_library(genepi::genepi ALIAS genepi)
33 | set_target_properties(genepi PROPERTIES POSITION_INDEPENDENT_CODE ON)
34 | target_sources(genepi
35 | PRIVATE
36 | "${genepi_include_dir}/arg_from_napi_value.h"
37 | "${genepi_include_dir}/bind_class.h"
38 | "${genepi_include_dir}/bind_class_base.h"
39 | "${genepi_include_dir}/binding_std.h"
40 | "${genepi_include_dir}/binding_type.h"
41 | "${genepi_include_dir}/caller.h"
42 | "${genepi_include_dir}/checker.h"
43 | "${genepi_include_dir}/class_definer.h"
44 | "${genepi_include_dir}/class_wrapper.h"
45 | "${genepi_include_dir}/common.h"
46 | "${genepi_include_dir}/creator.h"
47 | "${genepi_include_dir}/function_definer.h"
48 | "${genepi_include_dir}/function_definition.h"
49 | "${genepi_include_dir}/genepi.h"
50 | "${genepi_include_dir}/genepi_registry.h"
51 | "${genepi_include_dir}/method_definition.h"
52 | "${genepi_include_dir}/signature/base_signature.h"
53 | "${genepi_include_dir}/signature/constructor_signature.h"
54 | "${genepi_include_dir}/signature/function_signature.h"
55 | "${genepi_include_dir}/signature/method_signature.h"
56 | "${genepi_include_dir}/signature/signature_param.h"
57 | "${genepi_include_dir}/signature/templated_base_signature.h"
58 | "${genepi_include_dir}/singleton.h"
59 | "${genepi_include_dir}/types.h"
60 | "${genepi_include_dir}/type_list.h"
61 | "${genepi_include_dir}/type_transformer.h"
62 | "${genepi_source_dir}/singleton.cpp"
63 | )
64 | target_include_directories(genepi
65 | PUBLIC
66 | ${PROJECT_SOURCE_DIR}/include
67 | ${PROJECT_BINARY_DIR}
68 | PRIVATE
69 | ${NODE_ADDON_API_DIR}
70 | ${CMAKE_JS_INC}
71 | )
72 |
73 | target_link_libraries(genepi PUBLIC ${CMAKE_JS_LIB})
74 | export(TARGETS genepi NAMESPACE genepi:: FILE genepi_target.cmake)
75 | include(GenerateExportHeader)
76 | generate_export_header(genepi
77 | EXPORT_MACRO_NAME genepi_api
78 | EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/genepi/genepi_export.h
79 | )
80 | include(CMakePackageConfigHelpers)
81 | configure_package_config_file(
82 | cmake/genepiConfig.cmake.in
83 | genepiConfig.cmake
84 | INSTALL_DESTINATION .
85 | )
86 |
87 | if(EXISTS ${PROJECT_SOURCE_DIR}/examples)
88 | add_subdirectory(examples)
89 | endif()
90 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 - 2021 Geode-solutions
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Genepiby Geode-solutions
2 | Automatic generation of N-API wrapper from a C++ library
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | ---
23 |
24 | ## Introduction
25 | `genepi` is a C++11 library providing a complete set of macros to generate a Node.js addon from your C++ code using N-API.
26 |
27 | `genepi` works with [cmake-js](https://github.com/cmake-js/cmake-js) (a Node.js native addon of [CMake](https://cmake.org/)) as build system. CMake is largely used in the C++ community, here are some arguments to why CMake is a great build system for Node.js addons: [link](https://github.com/cmake-js/cmake-js#why-cmake).
28 |
29 | `genepi` is **MIT licensed** and based on templates and macros inspired by [nbind](https://github.com/charto/nbind) but using N-API.
30 |
31 | ## Features
32 | `genepi` allows you to:
33 |
34 | - Use your C++ API from JavaScript without any extra effort.
35 | - From **Node.js** and **Electron**,
36 | - On Linux, macOS and Windows,
37 | - Without changes to your C++ code. Simply add a separate short description at the end.
38 | - Distribute **native** code binary.
39 |
40 | In more detail:
41 |
42 | - Export C++ classes, even ones not visible from other files.
43 | - Export multiple C++ inheritances, even between several libraries.
44 | - Export C++ methods simply by mentioning their names.
45 | - Auto-detect argument and return types from C++ declarations.
46 | - [Automatically convert types](#type-conversion) and data structures between languages.
47 | - Call C++ methods from JavaScript with type checking.
48 | - Pass instances of compatible classes by value between languages (through the C++ stack).
49 |
50 | ## Requirements
51 | You need [Node.js](https://nodejs.org/) (at least v10.x) and one of the following C++ compilers:
52 |
53 | - GCC 4.8 or above,
54 | - Clang 3.6 or above,
55 | - Visual Studio 2015 ([the Community version](https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx) is fine).
56 |
57 | ## Quick start
58 | 1. Use your already existing C++ code in JavaScript
59 |
60 | ```C++
61 | // My C++ code in hello.cpp
62 | #include
63 | #include
64 |
65 | struct Greeter {
66 | static void sayHello( const std::string& name )
67 | {
68 | std::cout << "Hello, " << name << std::endl;
69 | }
70 | };
71 | ```
72 |
73 | 2. Install genepi and add some scripts to the `package.json`
74 | ```Shell
75 | npm install @geode/genepi
76 | ```
77 |
78 | ```JSON
79 | {
80 | "scripts": {
81 | "build": "cmake-js compile",
82 | "build:debug": "cmake-js compile -D"
83 | }
84 | }
85 | ```
86 |
87 | 3. Add JavaScript binding
88 |
89 | ```C++
90 | // Add this to the file (or in another file)
91 | #include
92 |
93 | GENEPI_CLASS( Greeter )
94 | {
95 | GENEPI_METHOD( sayHello );
96 | }
97 | GENEPI_MODULE( hello )
98 | ```
99 |
100 | 4. Configure your project by creating a `CMakeLists.txt`
101 | ```CMake
102 | cmake_minimum_required(VERSION 3.5)
103 |
104 | project(my_project)
105 |
106 | find_package(genepi REQUIRED PATHS ${PROJECT_SOURCE_DIR}/node_modules/@geode/genepi/build)
107 |
108 | add_genepi_library(my_project "hello.cpp")
109 | ```
110 |
111 | 5. Compile your addon
112 | ```Shell
113 | npm run build
114 | ```
115 |
116 | 6. Use it!
117 | ```JavaScript
118 | var myProject = require('my_project.node');
119 | myProject.Greeter.sayHello('you');
120 | ```
121 |
122 | ## User guide
123 | - [Creating your project](#creating-your-project)
124 | - [Calling from Node.js](#calling-from-nodejs)
125 | - [Functions](#functions)
126 | - [Overloaded functions](#overloaded-functions)
127 | - [Classes and constructors](#classes-and-constructors)
128 | - [Methods](#methods)
129 | - [Overloaded methods](#overloaded-methods)
130 | - [Inheritance](#inheritance)
131 | - [Passing data structures](#passing-data-structures)
132 | - [Using objects](#using-objects)
133 | - [Type conversion](#type-conversion)
134 |
135 | ### Creating your project
136 | Create your repository using the provided Github template: [genepi-template](https://github.com/Geode-solutions/genepi-template).
137 | Here is how to use a Github template: [link](https://help.github.com/en/articles/creating-a-repository-from-a-template).
138 |
139 | ### Calling from Node.js
140 | Each `genepi` module (i.e. each Node.js addon generated) needs to be registered using the `GENEPI_MODULE` macro:
141 |
142 | ```C++
143 | // My C++ library
144 |
145 | GENEPI_MODULE( my_addon );
146 | ```
147 |
148 | This name `my_addon` is only used by [N-API](https://nodejs.org/api/n-api.html#n_api_module_registration).
149 | The name of the addon is set in the `CMakeLists.txt` using the `add_genepi_library` macro. See [Quick start](#quick-start).
150 |
151 | ```JavaScript
152 | // My JavaScript file
153 | var example = require('my-genepi-addon.node');
154 |
155 | // Use the binding
156 | ```
157 |
158 | This `require` will only work if the module can be found by Node.js. To ease the import, you can use the package [bindings](https://www.npmjs.com/package/bindings). It will try several possible paths to find the module.
159 |
160 | ```JavaScript
161 | // My JavaScript file
162 | var example = require('bindings')('my-genepi-addon');
163 |
164 | // Use the binding
165 | ```
166 |
167 |
168 | ### Functions
169 | Functions not belonging to any class can be exported inside a named or an anonymous namespace.
170 | The C++ function gets exported to JavaScript with the same name using `GENEPI_FUNCTION`,
171 | or it can be renamed by adding a second argument (without quotation marks) using `NAMED_GENEPI_FUNCTION`.
172 | Addind double underscore in the new name will result in adding the function in nested objects on the js side.
173 |
174 | If the C++ function is overloaded, `GENEPI_MULTIFUNCTION` macro must be used
175 | instead. See [overloaded functions](#overloaded-functions).
176 |
177 | Example from C++: **[`functions.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/functions/functions.cpp)**
178 |
179 | ```C++
180 | #include
181 | #include
182 |
183 | void sayHello( const std::string& name )
184 | {
185 | std::cout << "Hello, " << name << std::endl;
186 | }
187 |
188 | void sayBye( const std::string& name )
189 | {
190 | std::cout << "Bye, " << name << std::endl;
191 | }
192 |
193 | void sayByeAgain( const std::string& name )
194 | {
195 | std::cout << "Bye again, " << name << std::endl;
196 | }
197 |
198 | namespace foo
199 | {
200 | void sayNamespacedHello( const std::string& name )
201 | {
202 | std::cout << "Hello, " << name << std::endl;
203 | }
204 | }
205 |
206 | #include
207 |
208 | namespace
209 | {
210 | GENEPI_FUNCTION( sayHello );
211 | NAMED_GENEPI_FUNCTION( sayBye, sayGoodbye );
212 | NAMED_GENEPI_FUNCTION( sayByeAgain, say__Goodbye );
213 | }
214 |
215 | namespace foo
216 | {
217 | GENEPI_FUNCTION( sayNamespacedHello );
218 | }
219 |
220 | GENEPI_MODULE( functions );
221 | ```
222 |
223 | Example from JavaScript: **[`functions.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/functions/functions.js)**
224 |
225 | ```JavaScript
226 | var functions = require('genepi-functions.node');
227 |
228 | functions.sayHello('you'); // Output: Hello, you
229 | functions.sayGoodbye('you'); // Output: Bye, you
230 | functions.say.Goodbye('you'); // Output: Bye again, you
231 | functions.sayNamespacedHello('you'); // Output: Hello, you
232 | ```
233 |
234 | ### Overloaded functions
235 | The `GENEPI_FUNCTION()` macro cannot distinguish between several
236 | overloaded versions of the same function, causing an error.
237 | In this case the `GENEPI_MULTIFUNCTION()` macro must be used.
238 |
239 | The second parameter of the macro is the return type.
240 | For calling from JavaScript, each overload needs to have a distinct name, given in the third parameter (without quotation marks).
241 | The remaining parameters are the parameter types of the C++ function.
242 |
243 | Example from C++: **[`overloaded-functions.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/overloaded-functions/overloaded-functions.cpp)**
244 |
245 | ```C++
246 | #include
247 | #include
248 |
249 | void test( const std::string& number )
250 | {
251 | std::cout << "Number " << number << std::endl;
252 | }
253 |
254 | void test( int number )
255 | {
256 | std::cout << "Number " << number << std::endl;
257 | }
258 |
259 | void test( int number, int another_number )
260 | {
261 | std::cout << "Number " << number + another_number << std::endl;
262 | }
263 |
264 | #include
265 |
266 | namespace
267 | {
268 | GENEPI_MULTIFUNCTION( test, void, test_string, const std::string& );
269 | GENEPI_MULTIFUNCTION( test, void, test_int, int );
270 | GENEPI_MULTIFUNCTION( test, void, test_int2, int, int );
271 | }
272 |
273 | GENEPI_MODULE( overloaded_functions );
274 | ```
275 |
276 | Example from JavaScript: **[`overloaded-functions.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/overloaded-functions/overloaded-functions.js)**
277 |
278 | ```JavaScript
279 | var overloadedFunctions = require('genepi-overloaded-functions.node');
280 |
281 | overloadedFunctions.test_string('42'); // Output: Number 42
282 | overloadedFunctions.test_int(42); // Output: Number 42
283 | overloadedFunctions.test_int2(20, 22); // Output: Number 42
284 | ```
285 |
286 | ### Classes and constructors
287 | The `GENEPI_CLASS(className)` macro takes the name of your C++ class as an argument
288 | (without any quotation marks), and exports it to JavaScript using the same name.
289 | It's followed by a curly brace enclosed block of method exports,
290 | as if it was a function definition.
291 |
292 | The class can be renamed on the JavaScript side by using the `NAMED_GENEPI_CLASS` macro and passing a string as a
293 | second argument. This is especially useful for binding a template class
294 | specialization with a more reasonable name: `NAMED_GENEPI_CLASS(Data, IntData)`.
295 |
296 | Constructors are exported with a macro call `GENEPI_CONSTRUCTOR(types...);` where `types` is a comma-separated list of arguments to the constructor, such as `int, int`. Calling `GENEPI_CONSTRUCTOR` multiple times allows overloading it.
297 |
298 | Constructor arguments are the only types that `genepi` cannot detect automatically.
299 |
300 | Example from C++: **[`classes.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/classes/classes.cpp)**
301 |
302 | ```C++
303 | #include
304 | #include
305 |
306 | class ClassExample
307 | {
308 | public:
309 | ClassExample()
310 | {
311 | std::cout << "No arguments" << std::endl;
312 | }
313 |
314 | ClassExample( int a, int b )
315 | {
316 | std::cout << "Ints: " << a << " " << b << std::endl;
317 | }
318 |
319 | ClassExample( const std::string& msg )
320 | {
321 | std::cout << "String: " << msg << std::endl;
322 | }
323 | };
324 |
325 | #include
326 |
327 | GENEPI_CLASS( ClassExample )
328 | {
329 | GENEPI_CONSTRUCTOR();
330 | GENEPI_CONSTRUCTOR( int, int );
331 | GENEPI_CONSTRUCTOR( const std::string& );
332 | }
333 |
334 | GENEPI_MODULE( classes );
335 | ```
336 |
337 | Example from JavaScript: **[`classes.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/classes/classes.js)**
338 |
339 | ```JavaScript
340 | var classes = require('genepi-classes.node');
341 |
342 | var a = new classes.ClassExample(); // Output: No arguments
343 | var b = new classes.ClassExample(42, 54); // Output: Ints: 42 54
344 | var c = new classes.ClassExample("Don't panic"); // Output: String: Don't panic
345 | ```
346 |
347 | ### Methods
348 | Methods are exported inside a `GENEPI_CLASS` or a `NAMED_GENEPI_CLASS` block with a macro call `GENEPI_METHOD`
349 | which takes the name of the method as an argument (without any quotation marks).
350 | The C++ method gets exported to JavaScript with the same name.
351 |
352 | If the C++ method is overloaded, `GENEPI_MULTIMETHOD` macro must be used instead.
353 | See [overloaded methods](#overloaded-methods).
354 |
355 | If the method is `static`, it becomes a property of the JavaScript constructor function
356 | and can be accessed like `className.methodName()`. Otherwise it becomes a property of
357 | the prototype and can be accessed like `obj = new className(); obj.methodName();`
358 |
359 | Example from C++: **[`methods.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/methods/methods.cpp)**
360 |
361 | ```C++
362 | #include
363 |
364 | class MethodExample
365 | {
366 | public:
367 | void add( int a, int b )
368 | {
369 | sum_ += a + b;
370 | std::cout << "Sum = " << sum_ << std::endl;
371 | }
372 |
373 | static void static_add( int a, int b )
374 | {
375 | MethodExample example;
376 | example.add( a, b );
377 | }
378 |
379 | private:
380 | int sum_{ 0 };
381 | };
382 |
383 | #include
384 |
385 | GENEPI_CLASS( MethodExample )
386 | {
387 | GENEPI_CONSTRUCTOR();
388 | GENEPI_METHOD( add );
389 | GENEPI_METHOD( static_add );
390 | }
391 |
392 | GENEPI_MODULE( methods );
393 | ```
394 |
395 | Example from JavaScript: **[`methods.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/methods/methods.js)**
396 |
397 | ```JavaScript
398 |
399 | var methods = require('genepi-methods.node');
400 |
401 | var example = new methods.MethodExample();
402 | example.add(12, 24); // Output: Sum = 36
403 |
404 | methods.MethodExample.static_add(12,24); // Output: Sum = 36
405 |
406 | ```
407 | ### Overloaded methods
408 | The `GENEPI_METHOD()` macro, like `GENEPI_FUNCTION` macro, cannot distinguish between several
409 | overloaded versions of the same method.
410 | In this case the `GENEPI_MULTIMETHOD()` macro must be used.
411 |
412 | The second parameter of the macro is the return type.
413 | For calling from JavaScript, each overload needs to have a distinct name, given in the third parameter **(WITH quotation marks)**.
414 | The remaining parameters are the parameter types of the C++ method.
415 |
416 | Example from C++: **[`overloaded-methods.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/overloaded-methods/overloaded-methods.cpp)**
417 |
418 | ```C++
419 | #include
420 | #include
421 |
422 | class OverloadMethod
423 | {
424 | public:
425 | void test( const std::string& number )
426 | {
427 | std::cout << "Number " << number << std::endl;
428 | }
429 |
430 | void test( int number )
431 | {
432 | std::cout << "Number " << number << std::endl;
433 | }
434 |
435 | void test( int number, int another_number )
436 | {
437 | std::cout << "Number " << number + another_number << std::endl;
438 | }
439 | };
440 |
441 | #include
442 |
443 | GENEPI_CLASS( OverloadMethod )
444 | {
445 | GENEPI_CONSTRUCTOR();
446 | GENEPI_MULTIMETHOD( test, void, "test_string", const std::string& );
447 | GENEPI_MULTIMETHOD( test, void, "test_int", int );
448 | GENEPI_MULTIMETHOD( test, void, "test_int2", int, int );
449 | }
450 |
451 | GENEPI_MODULE( overloaded_methods );
452 | ```
453 |
454 | Example from JavaScript: **[`overloaded-functions.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/overloaded-functions/overloaded-functions.js)**
455 |
456 | ```JavaScript
457 | var overloadedMethods = require('genepi-overloaded-methods.node');
458 |
459 | var a = new overloadedMethods.OverloadMethod();
460 | a.test_string('42'); // Ouput: Number 42
461 | a.test_int(42); // Ouput: Number 42
462 | a.test_int2(20, 22); // Ouput: Number 42
463 | ```
464 |
465 | ### Inheritance
466 | When a C++ class inherits another, the `GENEPI_INHERIT` macro can be used to allow calling parent
467 | class methods on the child class, or passing child class instances to C++ methods expecting
468 | parent class instances.
469 |
470 | Internally JavaScript only has prototype-based single inheritance while C++ supports
471 | multiple inheritance. To simulate it, `genepi` will copy the contents of the parents to the prototype. This has otherwise
472 | the same effect, except the JavaScript `instanceof` operator will return `false` for the parent classes.
473 |
474 | Example from C++: **[`inherit.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/inherit/inherit.cpp)**
475 |
476 | ```C++
477 | #include
478 |
479 | class FirstParent
480 | {
481 | public:
482 | FirstParent()
483 | {
484 | std::cout << "FirstParent" << std::endl;
485 | }
486 |
487 | void from_first_parent()
488 | {
489 | std::cout << "from first parent" << std::endl;
490 | }
491 | };
492 |
493 | class SecondParent
494 | {
495 | public:
496 | SecondParent()
497 | {
498 | std::cout << "SecondParent" << std::endl;
499 | }
500 |
501 | void from_second_parent()
502 | {
503 | std::cout << "from second parent" << std::endl;
504 | }
505 | };
506 |
507 | class Child: public FirstParent, public SecondParent
508 | {
509 | public:
510 | Child()
511 | {
512 | std::cout << "Child" << std::endl;
513 | }
514 | };
515 |
516 | #include
517 |
518 | GENEPI_CLASS( FirstParent )
519 | {
520 | GENEPI_CONSTRUCTOR();
521 | GENEPI_METHOD( from_first_parent );
522 | }
523 |
524 | GENEPI_CLASS( SecondParent )
525 | {
526 | GENEPI_CONSTRUCTOR();
527 | GENEPI_METHOD( from_second_parent );
528 | }
529 |
530 | GENEPI_CLASS( Child )
531 | {
532 | GENEPI_CONSTRUCTOR();
533 | GENEPI_INHERIT( FirstParent );
534 | GENEPI_INHERIT( SecondParent );
535 | }
536 |
537 | GENEPI_MODULE( inherit );
538 | ```
539 |
540 | Example from JavaScript: **[`overloaded-methods.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/overloaded-methods/overloaded-methods.js)**
541 |
542 | ```JavaScript
543 | var inherit = require('genepi-inherit.node');
544 |
545 | var a = new inherit.Child(); // Ouput: FirstParent / SecondParent / Child
546 | a.from_first_parent(); // Output: from first parent
547 | a.from_second_parent(); // Output: from second parent
548 | ```
549 |
550 | ### Passing data structures
551 | `genepi` supports automatically converting between JavaScript arrays and C++
552 | `std::vector` or `std::array` types. Just use them as arguments or return values
553 | in C++ methods.
554 |
555 | Note that data structures don't use the same memory layout in both languages,
556 | so the data always gets copied which takes more time for more data.
557 | For example the strings in an array of strings also get copied,
558 | one character at a time.
559 |
560 | ### Using objects
561 | C++ objects can be passed to and from JavaScript using different
562 | parameter and return types in C++ code:
563 |
564 | - *by reference* using pointers or references
565 | - *by value*
566 |
567 | Constness of objects is **not** ensured by `genepi`. So you could call a non const method on a const object.
568 |
569 | Note: using pointers and references is particularly **dangerous** because the pointer may become invalid without JavaScript noticing it.
570 |
571 | Example from C++: **[`objects.cpp`](https://github.com/Geode-solutions/genepi/blob/master/examples/objects/objects.cpp)**
572 |
573 | ```C++
574 | #include
575 |
576 | class Coord
577 | {
578 | public:
579 | Coord( int x, int y ) : x_( x ), y_( y ) {}
580 |
581 | int getX()
582 | {
583 | return x_;
584 | }
585 | int getY()
586 | {
587 | return y_;
588 | }
589 |
590 | private:
591 | int x_, y_;
592 | };
593 |
594 | class ObjectExample
595 | {
596 | public:
597 | static void showByValue( Coord coord )
598 | {
599 | std::cout << "C++ value " << coord.getX() << ", " << coord.getY()
600 | << std::endl;
601 | }
602 |
603 | static void showByRef( Coord* coord )
604 | {
605 | std::cout << "C++ ref " << coord->getX() << ", " << coord->getY()
606 | << std::endl;
607 | }
608 |
609 | static Coord getValue()
610 | {
611 | return Coord{ 12, 34 };
612 | }
613 |
614 | static Coord* getRef()
615 | {
616 | static Coord coord{ 56, 78 };
617 | return &coord;
618 | }
619 | };
620 |
621 | #include
622 |
623 | GENEPI_CLASS( Coord )
624 | {
625 | GENEPI_CONSTRUCTOR( int, int );
626 | GENEPI_METHOD( getX );
627 | GENEPI_METHOD( getY );
628 | }
629 |
630 | GENEPI_CLASS( ObjectExample )
631 | {
632 | GENEPI_METHOD( showByValue );
633 | GENEPI_METHOD( showByRef );
634 | GENEPI_METHOD( getValue );
635 | GENEPI_METHOD( getRef );
636 | }
637 |
638 | GENEPI_MODULE( objects );
639 | ```
640 |
641 | Example from JavaScript: **[`objects.js`](https://github.com/Geode-solutions/genepi/blob/master/examples/objects/objects.js)**
642 |
643 | ```JavaScript
644 | var objects = require('genepi-objects.node');
645 |
646 | var value1 = new objects.Coord(123, 456);
647 | var value2 = objects.ObjectExample.getValue();
648 | objects.ObjectExample.showByValue(value1); // Output: C++ value 123, 456
649 | objects.ObjectExample.showByValue(value2); // Output: C++ value 12, 34
650 |
651 | var ref = objects.ObjectExample.getRef();
652 | objects.ObjectExample.showByRef(ref); // Output: C++ ref 56, 78
653 | ```
654 |
655 | ### Type conversion
656 | Parameters and return values of function calls between languages
657 | are automatically converted between equivalent types:
658 |
659 | | JavaScript | C++ |
660 | | ---------- | ------------------------------------------- |
661 | | number | (`un`)`signed char`, `short`, `int`, `long` |
662 | | number | `float`, `double` |
663 | | boolean | `bool` |
664 | | string | `const` (`unsigned`) `char *` |
665 | | string | `std::string` |
666 | | Array | `std::vector` |
667 | | Array | `std::array` |
668 | | genepi-wrapped pointer | Pointer or reference to an instance of any bound class
See [Using objects](#using-objects) |
669 |
670 | ## Alternatives
671 | - [nbind](https://github.com/charto/nbind)
672 | - [Embind](https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html)
673 | - [v8pp](https://github.com/pmed/v8pp)
674 |
675 |
676 | ## Questions
677 | For questions and support please use the official [slack](https://slackin-opengeode.herokuapp.com) and go to the channel #genepi. The issue list of this repo is exclusively for bug reports and feature requests.
678 |
679 |
680 | ## Changelog
681 | Detailed changes for each release are documented in the [release notes](https://github.com/Geode-solutions/OpenGeode/releases).
682 |
683 |
684 | ## License
685 |
686 | [MIT](https://opensource.org/licenses/MIT)
687 |
688 | Copyright (c) 2019-present, Geode-solutions
689 |
--------------------------------------------------------------------------------
/cmake/genepiConfig.cmake.in:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2019 - 2021 Geode-solutions
2 | #
3 | # Permission is hereby granted, free of charge, to any person obtaining a copy
4 | # of this software and associated documentation files (the "Software"), to deal
5 | # in the Software without restriction, including without limitation the rights
6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | # copies of the Software, and to permit persons to whom the Software is
8 | # furnished to do so, subject to the following conditions:
9 | #
10 | # The above copyright notice and this permission notice shall be included in
11 | # all copies or substantial portions of the Software.
12 | #
13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | # SOFTWARE.
20 |
21 | @PACKAGE_INIT@
22 |
23 | # Load information for genepi target.
24 | include(${CMAKE_CURRENT_LIST_DIR}/genepi_target.cmake)
25 |
26 | @GENEPI_UTILS@
--------------------------------------------------------------------------------
/cmake/utils.cmake:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2019 - 2021 Geode-solutions
2 | #
3 | # Permission is hereby granted, free of charge, to any person obtaining a copy
4 | # of this software and associated documentation files (the "Software"), to deal
5 | # in the Software without restriction, including without limitation the rights
6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | # copies of the Software, and to permit persons to whom the Software is
8 | # furnished to do so, subject to the following conditions:
9 | #
10 | # The above copyright notice and this permission notice shall be included in
11 | # all copies or substantial portions of the Software.
12 | #
13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | # SOFTWARE.
20 |
21 | set(CMAKE_CXX_STANDARD 11)
22 | set(CMAKE_CXX_STANDARD_REQUIRED ON)
23 | set(CMAKE_CXX_EXTENSIONS OFF)
24 |
25 | execute_process(
26 | COMMAND node -p "require('node-addon-api').include"
27 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
28 | OUTPUT_VARIABLE NODE_ADDON_API_DIR
29 | )
30 | string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
31 | string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
32 |
33 | macro(add_genepi_library target_name)
34 | add_library(${target_name} SHARED ${ARGN} ${CMAKE_JS_SRC})
35 | target_include_directories(${target_name}
36 | PUBLIC
37 | ${PROJECT_SOURCE_DIR}/include
38 | ${PROJECT_BINARY_DIR}
39 | PRIVATE
40 | ${NODE_ADDON_API_DIR}
41 | ${CMAKE_JS_INC}
42 | )
43 | set_target_properties(${target_name}
44 | PROPERTIES
45 | PREFIX ""
46 | SUFFIX ".node"
47 | )
48 | target_link_libraries(${target_name}
49 | PUBLIC
50 | genepi::genepi
51 | ${CMAKE_JS_LIB}
52 | )
53 | endmacro()
54 |
--------------------------------------------------------------------------------
/examples/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2019 - 2021 Geode-solutions
2 | #
3 | # Permission is hereby granted, free of charge, to any person obtaining a copy
4 | # of this software and associated documentation files (the "Software"), to deal
5 | # in the Software without restriction, including without limitation the rights
6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | # copies of the Software, and to permit persons to whom the Software is
8 | # furnished to do so, subject to the following conditions:
9 | #
10 | # The above copyright notice and this permission notice shall be included in
11 | # all copies or substantial portions of the Software.
12 | #
13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | # SOFTWARE.
20 |
21 | function(add_genepi_example example)
22 | add_genepi_library(genepi-${example} "${CMAKE_CURRENT_LIST_DIR}/${example}/${example}.cpp")
23 | endfunction()
24 |
25 | add_genepi_example(functions)
26 | add_genepi_example(overloaded-functions)
27 | add_genepi_example(classes)
28 | add_genepi_example(methods)
29 | add_genepi_example(overloaded-methods)
30 | add_genepi_example(inherit)
31 | add_genepi_example(objects)
32 |
--------------------------------------------------------------------------------
/examples/classes/classes.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 | #include
26 |
27 | class ClassExample
28 | {
29 | public:
30 | ClassExample()
31 | {
32 | std::cout << "No arguments" << std::endl;
33 | }
34 |
35 | ClassExample( int a, int b )
36 | {
37 | std::cout << "Ints: " << a << " " << b << std::endl;
38 | }
39 |
40 | ClassExample( int a )
41 | {
42 | std::cout << "Int: " << a << std::endl;
43 | }
44 |
45 | ClassExample( const std::string& msg )
46 | {
47 | std::cout << "String: " << msg << std::endl;
48 | }
49 | };
50 |
51 | #include
52 |
53 | GENEPI_CLASS( ClassExample )
54 | {
55 | GENEPI_CONSTRUCTOR();
56 | GENEPI_CONSTRUCTOR( int, int );
57 | GENEPI_CONSTRUCTOR( const std::string& );
58 | GENEPI_CONSTRUCTOR( int );
59 | }
60 |
61 | GENEPI_MODULE( classes );
--------------------------------------------------------------------------------
/examples/classes/classes.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var classes = require('bindings')('genepi-classes');
25 |
26 | var a = new classes.ClassExample();
27 | var b = new classes.ClassExample(42, 54);
28 | var c = new classes.ClassExample(42);
29 | var d = new classes.ClassExample("Don't panic");
30 |
--------------------------------------------------------------------------------
/examples/functions/functions.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 | #include
26 | #include
27 |
28 | void sayHello( const std::string& name )
29 | {
30 | std::cout << "Hello, " << name << std::endl;
31 | }
32 |
33 | void sayBye( const std::string& name )
34 | {
35 | std::cout << "Bye, " << name << std::endl;
36 | }
37 |
38 | void sayBye2( const std::string& name )
39 | {
40 | std::cout << "Bye2, " << name << std::endl;
41 | }
42 |
43 | void displayArray( std::vector< double > values )
44 | {
45 | for( const auto i : values )
46 | {
47 | std::cout << i << ' ';
48 | }
49 | std::cout << std::endl;
50 | }
51 |
52 | void displayArray2( const std::vector< double >& values )
53 | {
54 | for( const auto i : values )
55 | {
56 | std::cout << i << ' ';
57 | }
58 | std::cout << std::endl;
59 | }
60 |
61 | namespace foo
62 | {
63 | void sayNamespacedHello( const std::string& name )
64 | {
65 | std::cout << "Hello, " << name << std::endl;
66 | }
67 | } // namespace foo
68 |
69 | #include
70 |
71 | namespace
72 | {
73 | GENEPI_FUNCTION( sayHello );
74 | NAMED_GENEPI_FUNCTION( sayBye, sayGoodbye );
75 | NAMED_GENEPI_FUNCTION( sayBye2, say__Goodbye );
76 | GENEPI_FUNCTION( displayArray );
77 | GENEPI_FUNCTION( displayArray2 );
78 | } // namespace
79 |
80 | namespace foo
81 | {
82 | GENEPI_FUNCTION( sayNamespacedHello );
83 | }
84 |
85 | GENEPI_MODULE( functions );
--------------------------------------------------------------------------------
/examples/functions/functions.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var functions = require('bindings')('genepi-functions');
25 |
26 | functions.sayHello('you');
27 | functions.sayGoodbye('you');
28 | functions.say.Goodbye('you');
29 | functions.sayNamespacedHello('you');
30 |
31 | const array = [1, 2, 3]
32 | functions.displayArray(array)
33 | functions.displayArray2(array)
--------------------------------------------------------------------------------
/examples/index.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | require('./functions/functions')
25 | require('./overloaded-functions/overloaded-functions')
26 | require('./classes/classes')
27 | require('./methods/methods')
28 | require('./overloaded-methods/overloaded-methods')
29 | require('./inherit/inherit')
30 | require('./objects/objects')
--------------------------------------------------------------------------------
/examples/inherit/inherit.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 |
26 | class FirstParent
27 | {
28 | public:
29 | FirstParent()
30 | {
31 | std::cout << "FirstParent" << std::endl;
32 | }
33 |
34 | void from_first_parent()
35 | {
36 | std::cout << "from first parent" << std::endl;
37 | }
38 | };
39 |
40 | class SecondParent
41 | {
42 | public:
43 | SecondParent()
44 | {
45 | std::cout << "SecondParent" << std::endl;
46 | }
47 |
48 | void from_second_parent()
49 | {
50 | std::cout << "from second parent" << std::endl;
51 | }
52 | };
53 |
54 | class Child : public FirstParent, public SecondParent
55 | {
56 | public:
57 | Child()
58 | {
59 | std::cout << "Child" << std::endl;
60 | }
61 | };
62 |
63 | #include
64 |
65 | GENEPI_CLASS( FirstParent )
66 | {
67 | GENEPI_CONSTRUCTOR();
68 | GENEPI_METHOD( from_first_parent );
69 | }
70 |
71 | GENEPI_CLASS( SecondParent )
72 | {
73 | GENEPI_CONSTRUCTOR();
74 | GENEPI_METHOD( from_second_parent );
75 | }
76 |
77 | GENEPI_CLASS( Child )
78 | {
79 | GENEPI_CONSTRUCTOR();
80 | GENEPI_INHERIT( FirstParent );
81 | GENEPI_INHERIT( SecondParent );
82 | }
83 |
84 | GENEPI_MODULE( inherit );
--------------------------------------------------------------------------------
/examples/inherit/inherit.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var inherit = require('bindings')('genepi-inherit');
25 |
26 | var a = new inherit.Child();
27 | a.from_first_parent();
28 | a.from_second_parent();
29 |
--------------------------------------------------------------------------------
/examples/methods/methods.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 |
26 | class MethodExample
27 | {
28 | public:
29 | void add( int a, int b )
30 | {
31 | sum_ += a + b;
32 | std::cout << "Sum = " << sum_ << std::endl;
33 | }
34 |
35 | static void static_add( int a, int b )
36 | {
37 | MethodExample example;
38 | example.add( a, b );
39 | }
40 |
41 | private:
42 | int sum_{ 0 };
43 | };
44 |
45 | #include
46 |
47 | GENEPI_CLASS( MethodExample )
48 | {
49 | GENEPI_CONSTRUCTOR();
50 | GENEPI_METHOD( add );
51 | GENEPI_METHOD( static_add );
52 | }
53 |
54 | GENEPI_MODULE( methods );
--------------------------------------------------------------------------------
/examples/methods/methods.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var methods = require('bindings')('genepi-methods');
25 |
26 | var example = new methods.MethodExample();
27 | example.add(12, 24);
28 |
29 | methods.MethodExample.static_add(12, 24);
30 |
--------------------------------------------------------------------------------
/examples/objects/objects.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 |
26 | class Coord
27 | {
28 | public:
29 | Coord( int x, int y ) : x_( x ), y_( y ) {}
30 |
31 | int getX()
32 | {
33 | return x_;
34 | }
35 | int getY()
36 | {
37 | return y_;
38 | }
39 |
40 | private:
41 | int x_, y_;
42 | };
43 |
44 | class ObjectExample
45 | {
46 | public:
47 | static void showByValue( Coord coord )
48 | {
49 | std::cout << "C++ value " << coord.getX() << ", " << coord.getY()
50 | << std::endl;
51 | }
52 |
53 | static void showByRef( Coord* coord )
54 | {
55 | std::cout << "C++ ref " << coord->getX() << ", " << coord->getY()
56 | << std::endl;
57 | }
58 |
59 | static Coord getValue()
60 | {
61 | return Coord{ 12, 34 };
62 | }
63 |
64 | static Coord* getRef()
65 | {
66 | static Coord coord{ 56, 78 };
67 | return &coord;
68 | }
69 | };
70 |
71 | #include
72 |
73 | GENEPI_CLASS( Coord )
74 | {
75 | GENEPI_CONSTRUCTOR( int, int );
76 | GENEPI_METHOD( getX );
77 | GENEPI_METHOD( getY );
78 | }
79 |
80 | GENEPI_CLASS( ObjectExample )
81 | {
82 | GENEPI_METHOD( showByValue );
83 | GENEPI_METHOD( showByRef );
84 | GENEPI_METHOD( getValue );
85 | GENEPI_METHOD( getRef );
86 | }
87 |
88 | GENEPI_MODULE( objects );
--------------------------------------------------------------------------------
/examples/objects/objects.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var objects = require('bindings')('genepi-objects');
25 |
26 | var value1 = new objects.Coord(123, 456);
27 | var value2 = objects.ObjectExample.getValue();
28 | objects.ObjectExample.showByValue(value1);
29 | objects.ObjectExample.showByValue(value2);
30 |
31 | var ref = objects.ObjectExample.getRef();
32 | objects.ObjectExample.showByRef(ref);
33 |
--------------------------------------------------------------------------------
/examples/overloaded-functions/overloaded-functions.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 | #include
26 |
27 | void test( const std::string& number )
28 | {
29 | std::cout << "Number " << number << std::endl;
30 | }
31 |
32 | void test( int number )
33 | {
34 | std::cout << "Number " << number << std::endl;
35 | }
36 |
37 | void test( int number, int another_number )
38 | {
39 | std::cout << "Number " << number + another_number << std::endl;
40 | }
41 |
42 | #include
43 |
44 | namespace
45 | {
46 | GENEPI_MULTIFUNCTION( test, void, test_string, const std::string& );
47 | GENEPI_MULTIFUNCTION( test, void, test_int, int );
48 | GENEPI_MULTIFUNCTION( test, void, test_int2, int, int );
49 | } // namespace
50 |
51 | GENEPI_MODULE( overloaded_functions );
52 |
--------------------------------------------------------------------------------
/examples/overloaded-functions/overloaded-functions.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var overloadedFunctions = require('bindings')('genepi-overloaded-functions');
25 |
26 | overloadedFunctions.test_string('42');
27 | overloadedFunctions.test_int(42);
28 | overloadedFunctions.test_int2(20, 22);
--------------------------------------------------------------------------------
/examples/overloaded-methods/overloaded-methods.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #include
25 | #include
26 |
27 | class OverloadMethod
28 | {
29 | public:
30 | void test( const std::string& number )
31 | {
32 | std::cout << "Number " << number << std::endl;
33 | }
34 |
35 | void test( int number )
36 | {
37 | std::cout << "Number " << number << std::endl;
38 | }
39 |
40 | void test( int number, int another_number )
41 | {
42 | std::cout << "Number " << number + another_number << std::endl;
43 | }
44 | };
45 |
46 | #include
47 |
48 | GENEPI_CLASS( OverloadMethod )
49 | {
50 | GENEPI_CONSTRUCTOR();
51 | GENEPI_MULTIMETHOD( test, void, "test_string", const std::string& );
52 | GENEPI_MULTIMETHOD( test, void, "test_int", int );
53 | GENEPI_MULTIMETHOD( test, void, "test_int2", int, int );
54 | }
55 |
56 | GENEPI_MODULE( overloaded_methods );
57 |
--------------------------------------------------------------------------------
/examples/overloaded-methods/overloaded-methods.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | var overloadedMethods = require('bindings')('genepi-overloaded-methods');
25 |
26 | var a = new overloadedMethods.OverloadMethod();
27 | a.test_string('42');
28 | a.test_int(42);
29 | a.test_int2(20, 22);
--------------------------------------------------------------------------------
/include/genepi/arg_from_napi_value.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #pragma once
25 |
26 | #include
27 |
28 | namespace genepi
29 | {
30 | // ArgFromNapiValue converts JavaScript types into C++ types, usually with
31 | // BindingType<>::fromNapiValueType but some types require additional
32 | // temporary storage, such as a string converted to C style. FromNapiValue
33 | // is a struct, so wrappers for all objects can be constructed as function
34 | // arguments, and their actual values passed to the called function are
35 | // returned by the get() function. The wrappers go out of scope and are
36 | // destroyed at the end of the function call.
37 |
38 | // Handle most C++ types.
39 | template < size_t Index, typename ArgType >
40 | struct ArgFromNapiValue
41 | {
42 | using Transformed = TypeTransformer< ArgType >;
43 |
44 | ArgFromNapiValue( const Napi::CallbackInfo &args ) {}
45 |
46 | typename Transformed::Type get( const Napi::CallbackInfo &args )
47 | {
48 | return Transformed::Binding::fromNapiValue( args[Index] );
49 | }
50 | };
51 |
52 | // Handle char pointers, which will receive a C string representation of any
53 | // JavaScript value.
54 | template < size_t Index >
55 | struct ArgFromNapiValue< Index, const char * >
56 | {
57 | ArgFromNapiValue( const Napi::CallbackInfo &args )
58 | : val( args[Index].ToString() )
59 | {
60 | }
61 |
62 | const char *get( const Napi::CallbackInfo &args )
63 | {
64 | return val.Utf8Value().c_str();
65 | }
66 |
67 | // RAII style storage for the string data.
68 | Napi::String val;
69 | };
70 |
71 | // Automatically cast char to unsigned if the C++ function expects it.
72 | template < size_t Index >
73 | struct ArgFromNapiValue< Index, const unsigned char * >
74 | {
75 | ArgFromNapiValue( const Napi::CallbackInfo &args )
76 | : val( args[Index].ToString() )
77 | {
78 | }
79 |
80 | const unsigned char *get( const Napi::CallbackInfo &args )
81 | {
82 | return reinterpret_cast< const unsigned char * >(
83 | val.Utf8Value().c_str() );
84 | }
85 |
86 | // RAII style storage for the string data.
87 | Napi::String val;
88 | };
89 | } // namespace genepi
90 |
--------------------------------------------------------------------------------
/include/genepi/bind_class.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #pragma once
25 |
26 | #include
27 |
28 | #include
29 | #include
30 |
31 | namespace genepi
32 | {
33 | template < typename Bound >
34 | class BindClass : public BindClassBase
35 | {
36 | public:
37 | void init( std::string name )
38 | {
39 | this->name_ = std::move( name );
40 | }
41 |
42 | static BindClass& instance()
43 | {
44 | return Singleton::instance< BindClass< Bound > >();
45 | }
46 |
47 | std::string type() final
48 | {
49 | return typeid( Bound ).name();
50 | }
51 |
52 | template < typename SuperType >
53 | void add_super_class();
54 |
55 | void initialize( Napi::Env& env, Napi::Object& target ) final
56 | {
57 | std::deque< MethodDefinition > methods;
58 | std::unordered_set< const BindClassBase* > classes;
59 | initialize_api( methods, classes );
60 |
61 | ClassWrapperBase< Bound >::instance().Initialize(
62 | env, target, name_, static_methods_, methods, instance() );
63 | }
64 |
65 | void construct( const Napi::CallbackInfo& info ) const
66 | {
67 | try
68 | {
69 | for( const auto& constructor : constructors_.at(
70 | static_cast< unsigned int >( info.Length() ) ) )
71 | {
72 | try
73 | {
74 | constructor( info );
75 | return;
76 | }
77 | catch( const Napi::Error& /*unsued*/ )
78 | {
79 | continue;
80 | }
81 | }
82 | throw Napi::Error::New( info.Env(), "Wrong argument types" );
83 | }
84 | catch( const std::out_of_range& )
85 | {
86 | throw Napi::Error::New(
87 | info.Env(), "Wrong number of arguments" );
88 | }
89 | }
90 | };
91 |
92 | template < class Bound >
93 | ClassWrapper< Bound >::ClassWrapper( const Napi::CallbackInfo& info )
94 | : Napi::ObjectWrap< ClassWrapper< Bound > >( info )
95 | {
96 | if( info.Length() == 2 && info[0].IsBoolean() && info[1].IsExternal() )
97 | {
98 | if( info[0].As< Napi::Boolean >() )
99 | {
100 | this->underlying_class_ =
101 | *info[1]
102 | .As< Napi::External< std::shared_ptr< Bound > > >()
103 | .Data();
104 | }
105 | else
106 | {
107 | this->underlying_class_.reset(
108 | info[1].As< Napi::External< Bound > >().Data(),
109 | NoDeleter{} );
110 | }
111 | }
112 | else
113 | {
114 | BindClass< Bound >::instance().construct( info );
115 | }
116 | }
117 |
118 | template < class Bound, class SuperType >
119 | void* upcast( void* arg )
120 | {
121 | return static_cast< SuperType* >( static_cast< Bound* >( arg ) );
122 | }
123 |
124 | template < class Bound >
125 | template < typename SuperType >
126 | void BindClass< Bound >::add_super_class()
127 | {
128 | super_classes_.emplace_back(
129 | BindClass< SuperType >::instance(), upcast< Bound, SuperType > );
130 | }
131 |
132 | } // namespace genepi
133 |
--------------------------------------------------------------------------------
/include/genepi/bind_class_base.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019 - 2021 Geode-solutions
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to deal
6 | * in the Software without restriction, including without limitation the rights
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | * copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | * SOFTWARE.
21 | *
22 | */
23 |
24 | #pragma once
25 |
26 | #include
27 | #include