调整C++ 代码
This commit is contained in:
7
hmcalculate/.gitignore
vendored
Normal file
7
hmcalculate/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/node_modules
|
||||
/oh_modules
|
||||
/.preview
|
||||
/build
|
||||
/.cxx
|
||||
/.test
|
||||
oh-package-lock.json5
|
1
hmcalculate/Index.ets
Normal file
1
hmcalculate/Index.ets
Normal file
@@ -0,0 +1 @@
|
||||
export { CalculateAdapter } from './src/main/cpp/types/libhmcalculate/index'
|
39
hmcalculate/build-profile.json5
Normal file
39
hmcalculate/build-profile.json5
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"apiType": "stageMode",
|
||||
"buildOption": {
|
||||
"externalNativeOptions": {
|
||||
"path": "./src/main/cpp/CMakeLists.txt",
|
||||
"arguments": "",
|
||||
"cppFlags": "",
|
||||
"abiFilters": [
|
||||
"arm64-v8a"
|
||||
]
|
||||
}
|
||||
},
|
||||
"buildOptionSet": [
|
||||
{
|
||||
"name": "release",
|
||||
"arkOptions": {
|
||||
"obfuscation": {
|
||||
"ruleOptions": {
|
||||
"enable": true,
|
||||
"files": [
|
||||
"./obfuscation-rules.txt"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"nativeLib": {
|
||||
"debugSymbol": {
|
||||
"strip": true,
|
||||
"exclude": []
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
"targets": [
|
||||
{
|
||||
"name": "default"
|
||||
}
|
||||
]
|
||||
}
|
6
hmcalculate/hvigorfile.ts
Normal file
6
hmcalculate/hvigorfile.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { hspTasks } from '@ohos/hvigor-ohos-plugin';
|
||||
|
||||
export default {
|
||||
system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
|
||||
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
|
||||
}
|
18
hmcalculate/obfuscation-rules.txt
Normal file
18
hmcalculate/obfuscation-rules.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
# Define project specific obfuscation rules here.
|
||||
# You can include the obfuscation configuration files in the current module's build-profile.json5.
|
||||
#
|
||||
# For more details, see
|
||||
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md
|
||||
|
||||
# Obfuscation options:
|
||||
# -disable-obfuscation: disable all obfuscations
|
||||
# -enable-property-obfuscation: obfuscate the property names
|
||||
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
|
||||
# -compact: remove unnecessary blank spaces and all line feeds
|
||||
# -remove-log: remove all console.* statements
|
||||
# -print-namecache: print the name cache that contains the mapping from the old names to new names
|
||||
# -apply-namecache: reuse the given cache file
|
||||
|
||||
# Keep options:
|
||||
# -keep-property-name: specifies property names that you want to keep
|
||||
# -keep-global-name: specifies names that you want to keep in the global scope
|
12
hmcalculate/oh-package.json5
Normal file
12
hmcalculate/oh-package.json5
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@devwiki/hmcalculate",
|
||||
"version": "1.0.0",
|
||||
"description": "Please describe the basic information.",
|
||||
"main": "Index.ets",
|
||||
"author": "",
|
||||
"license": "Apache-2.0",
|
||||
"packageType": "InterfaceHar",
|
||||
"dependencies": {
|
||||
"libhmcalculate.so": "file:./src/main/cpp/types/libhmcalculate"
|
||||
}
|
||||
}
|
19
hmcalculate/src/main/cpp/CMakeLists.txt
Normal file
19
hmcalculate/src/main/cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
# the minimum version of CMake.
|
||||
cmake_minimum_required(VERSION 3.4.1)
|
||||
project(hmcalculate)
|
||||
|
||||
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
if(DEFINED PACKAGE_FIND_FILE)
|
||||
include(${PACKAGE_FIND_FILE})
|
||||
endif()
|
||||
|
||||
include_directories(${NATIVERENDER_ROOT_PATH}
|
||||
${NATIVERENDER_ROOT_PATH}/include)
|
||||
|
||||
add_library(hmcalculate SHARED
|
||||
napi_init.cpp
|
||||
CalculateInfo.h
|
||||
CalculateAdaptor.cpp)
|
||||
|
||||
target_link_libraries(hmcalculate PUBLIC ${NATIVERENDER_ROOT_PATH}/../../../../cppLib/dist/lib/arm64-v8a/libcalculate.so)
|
25
hmcalculate/src/main/cpp/Calculate.h
Normal file
25
hmcalculate/src/main/cpp/Calculate.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef CPPLIB_LIBRARY_H
|
||||
#define CPPLIB_LIBRARY_H
|
||||
|
||||
#include <string>
|
||||
#include "CalculateInfo.h"
|
||||
|
||||
class Calculate {
|
||||
public:
|
||||
static Calculate& getInstance();
|
||||
int add(int a, int b);
|
||||
CalculateInfo getInfo();
|
||||
|
||||
// 删除拷贝构造函数和赋值运算符
|
||||
Calculate(const Calculate&) = delete;
|
||||
Calculate& operator=(const Calculate&) = delete;
|
||||
|
||||
private:
|
||||
// 构造函数和析构函数私有化
|
||||
Calculate() {}
|
||||
~Calculate() {}
|
||||
};
|
||||
#endif //CPPLIB_LIBRARY_H
|
||||
|
||||
|
||||
|
64
hmcalculate/src/main/cpp/CalculateAdaptor.cpp
Normal file
64
hmcalculate/src/main/cpp/CalculateAdaptor.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created on 2024/4/22.
|
||||
//
|
||||
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
|
||||
// please include "napi/native_api.h".
|
||||
|
||||
#include "CalculateAdaptor.h"
|
||||
#include "Calculate.h"
|
||||
#include "CalculateInfo.h"
|
||||
|
||||
CalculateAdaptor::CalculateAdaptor() {
|
||||
_calculate = &Calculate::getInstance();
|
||||
}
|
||||
|
||||
CalculateAdaptor::CalculateAdaptor(napi_env env, napi_value thisVar) {
|
||||
_calculate = &Calculate::getInstance();
|
||||
}
|
||||
|
||||
CalculateAdaptor::~CalculateAdaptor() {
|
||||
|
||||
}
|
||||
|
||||
CalculateAdaptor *util_get_napi_info(napi_env env, napi_callback_info cbinfo, size_t argc, napi_value *argv)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
|
||||
CalculateAdaptor *calculator = nullptr;
|
||||
napi_unwrap(env, thisVar, (void **)&calculator);
|
||||
return calculator;
|
||||
}
|
||||
|
||||
|
||||
napi_value calculate_add(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 2;
|
||||
napi_value argv[2] = {0};
|
||||
CalculateAdaptor *calculator = util_get_napi_info(env, info, argc, argv);
|
||||
|
||||
int32_t result = 0;
|
||||
napi_status a = napi_ok;
|
||||
a = napi_get_value_int32(env, argv[0], &result);
|
||||
|
||||
napi_status b = napi_ok;
|
||||
b = napi_get_value_int32(env, argv[1], &result);
|
||||
|
||||
napi_value value;
|
||||
napi_create_int32(env, calculator->_calculate->add(a, b), &value);
|
||||
return value;
|
||||
}
|
||||
|
||||
napi_value calculate_getInfo(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 0;
|
||||
napi_value argv[1] = {0};
|
||||
CalculateAdaptor *calculator = util_get_napi_info(env, info, argc, argv);
|
||||
|
||||
CalculateInfo info2;
|
||||
info2.name = calculator->_calculate->getInfo().name;
|
||||
info2.versionCode = calculator->_calculate->getInfo().versionCode;
|
||||
info2.versionName = calculator->_calculate->getInfo().versionName;
|
||||
|
||||
napi_value value;
|
||||
|
||||
return value;
|
||||
}
|
28
hmcalculate/src/main/cpp/CalculateAdaptor.h
Normal file
28
hmcalculate/src/main/cpp/CalculateAdaptor.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created on 2024/4/22.
|
||||
//
|
||||
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
|
||||
// please include "napi/native_api.h".
|
||||
|
||||
#ifndef HM4DEMO_CALCULATOR_H
|
||||
#define HM4DEMO_CALCULATOR_H
|
||||
|
||||
#include "CalculateInfo.h"
|
||||
#include "Calculate.h"
|
||||
#include "js_native_api.h"
|
||||
|
||||
class CalculateAdaptor {
|
||||
public:
|
||||
CalculateAdaptor();
|
||||
CalculateAdaptor(napi_env env, napi_value thisVar);
|
||||
~CalculateAdaptor();
|
||||
Calculate *_calculate;
|
||||
};
|
||||
|
||||
CalculateAdaptor *util_get_napi_info(napi_env env, napi_callback_info cbinfo, size_t argc, napi_value *argv);
|
||||
|
||||
napi_value calculate_add(napi_env env, napi_callback_info info);
|
||||
napi_value calculate_getInfo(napi_env env, napi_callback_info info);
|
||||
|
||||
|
||||
#endif //HM4DEMO_CALCULATOR_H
|
17
hmcalculate/src/main/cpp/CalculateInfo.h
Normal file
17
hmcalculate/src/main/cpp/CalculateInfo.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created on 2024/5/28.
|
||||
//
|
||||
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
|
||||
// please include "napi/native_api.h".
|
||||
|
||||
#ifndef HM4DEMO_CALCULATEINFO_H
|
||||
#define HM4DEMO_CALCULATEINFO_H
|
||||
|
||||
#include <string>
|
||||
struct CalculateInfo {
|
||||
std::string name;
|
||||
std::string versionName;
|
||||
int versionCode;
|
||||
};
|
||||
|
||||
#endif //HM4DEMO_CALCULATEINFO_H
|
49
hmcalculate/src/main/cpp/napi_init.cpp
Normal file
49
hmcalculate/src/main/cpp/napi_init.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "napi/native_api.h"
|
||||
#include "CalculateAdaptor.h"
|
||||
#include "js_native_api.h"
|
||||
|
||||
napi_value JS_Constructor(napi_env env, napi_callback_info info) {
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
|
||||
CalculateAdaptor *adaptor = new CalculateAdaptor(env, thisVar);
|
||||
napi_wrap(
|
||||
env, thisVar, adaptor,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
CalculateAdaptor *ada = (CalculateAdaptor *)data;
|
||||
delete ada;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value Init(napi_env env, napi_value exports)
|
||||
{
|
||||
const char className[] = "CalculateAdaptor";
|
||||
napi_property_descriptor desc[] = {{"add", nullptr, calculate_add, nullptr, nullptr, nullptr, napi_default, nullptr},
|
||||
{"getInfo", nullptr, calculate_getInfo, nullptr, nullptr, nullptr, napi_default, nullptr}};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_value result = nullptr;
|
||||
napi_define_class(env, className, sizeof(className), JS_Constructor, nullptr,
|
||||
sizeof(desc) / sizeof(desc[0]), desc, &result);
|
||||
|
||||
napi_set_named_property(env, exports, "CalculateAdaptor", result);
|
||||
return exports;
|
||||
}
|
||||
|
||||
static napi_module demoModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = Init,
|
||||
.nm_modname = "native_lib",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterNatvie_libModule(void)
|
||||
{
|
||||
napi_module_register(&demoModule);
|
||||
}
|
4
hmcalculate/src/main/cpp/types/libhmcalculate/index.d.ts
vendored
Normal file
4
hmcalculate/src/main/cpp/types/libhmcalculate/index.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
export class CalculateAdapter {
|
||||
add(a: number, b: number): number;
|
||||
getInfo(): Object;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "libhmcalculate.so",
|
||||
"types": "./index.d.ts",
|
||||
"version": "",
|
||||
"description": "Please describe the basic information."
|
||||
}
|
6
hmcalculate/src/main/ets/CalculateInfo.ets
Normal file
6
hmcalculate/src/main/ets/CalculateInfo.ets
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
export interface CalculateInfo {
|
||||
name: string;
|
||||
versionName: string;
|
||||
versionCode: number;
|
||||
}
|
20
hmcalculate/src/main/ets/Calculator.ets
Normal file
20
hmcalculate/src/main/ets/Calculator.ets
Normal file
@@ -0,0 +1,20 @@
|
||||
import { CalculateAdapter } from 'native_lib.so'
|
||||
import { CalculateInfo } from './CalculateInfo'
|
||||
|
||||
export class Calculator {
|
||||
|
||||
private adaptor!:CalculateAdapter;
|
||||
|
||||
constructor() {
|
||||
this.adaptor = new CalculateAdapter();
|
||||
}
|
||||
|
||||
add(a:number, b: number): number {
|
||||
return this.adaptor.add(a, b);
|
||||
}
|
||||
|
||||
getInfo(): CalculateInfo {
|
||||
return this.adaptor.getInfo() as CalculateInfo;
|
||||
}
|
||||
|
||||
}
|
21
hmcalculate/src/main/ets/pages/Index.ets
Normal file
21
hmcalculate/src/main/ets/pages/Index.ets
Normal file
@@ -0,0 +1,21 @@
|
||||
import { hilog } from '@kit.PerformanceAnalysisKit';
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct Index {
|
||||
@State message: string = 'Hello World';
|
||||
|
||||
build() {
|
||||
Row() {
|
||||
Column() {
|
||||
Text(this.message)
|
||||
.fontSize(50)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
.onClick(() => {
|
||||
})
|
||||
}
|
||||
.width('100%')
|
||||
}
|
||||
.height('100%')
|
||||
}
|
||||
}
|
14
hmcalculate/src/main/module.json5
Normal file
14
hmcalculate/src/main/module.json5
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "hmcalculate",
|
||||
"type": "shared",
|
||||
"description": "$string:shared_desc",
|
||||
"deviceTypes": [
|
||||
"phone",
|
||||
"tablet",
|
||||
"2in1"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"pages": "$profile:main_pages"
|
||||
}
|
||||
}
|
8
hmcalculate/src/main/resources/base/element/color.json
Normal file
8
hmcalculate/src/main/resources/base/element/color.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "white",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
8
hmcalculate/src/main/resources/base/element/string.json
Normal file
8
hmcalculate/src/main/resources/base/element/string.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "shared_desc",
|
||||
"value": "description"
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"pages/Index"
|
||||
]
|
||||
}
|
5
hmcalculate/src/test/List.test.ets
Normal file
5
hmcalculate/src/test/List.test.ets
Normal file
@@ -0,0 +1,5 @@
|
||||
import localUnitTest from './LocalUnit.test';
|
||||
|
||||
export default function testsuite() {
|
||||
localUnitTest();
|
||||
}
|
33
hmcalculate/src/test/LocalUnit.test.ets
Normal file
33
hmcalculate/src/test/LocalUnit.test.ets
Normal file
@@ -0,0 +1,33 @@
|
||||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
|
||||
export default function localUnitTest() {
|
||||
describe('localUnitTest',() => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
beforeEach(() => {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
afterEach(() => {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
afterAll(() => {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
it('assertContain', 0, () => {
|
||||
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
||||
let a = 'abc';
|
||||
let b = 'b';
|
||||
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
||||
expect(a).assertContain(b);
|
||||
expect(a).assertEqual(a);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user