当前位置: 首页 > news >正文

网站备案是否收费标准谷歌在线浏览入口

网站备案是否收费标准,谷歌在线浏览入口,网站页面优化包括,市环保局网站建设方案1. HarmonyOS ArkTS与C数据类型转换 本文介绍了C与TS各自数据类型与互相之间的数据类型转换,在需要使用C模块时可以快速上手对各种数据类型进行转换。 1.1. 概述 HarmonyOS的主力开发语言是ArkTS,也提供了C语言的支持,对于一些能力&#xff…

1. HarmonyOS ArkTS与C++数据类型转换

  本文介绍了C++与TS各自数据类型与互相之间的数据类型转换,在需要使用C++模块时可以快速上手对各种数据类型进行转换。

1.1. 概述

  HarmonyOS的主力开发语言是ArkTS,也提供了C++语言的支持,对于一些能力,比如音视频编解码等,HarmonyOS 提供的也只有C++ API,对于一些其他平台现有能力的迁移,C++也是最快捷高效的,所以对于一个HarmonyOS 开发者,掌握ArkTS与C++交互成了一项必备技能。
 &emsp每种编程语言都有自己定义的数据类型,不同编程语言之间互相调用就涉及到了数据类型的转换,ArkTS与C++的转换主要有Node-API接口提供,本文介绍ArkTS与C++互相转换的接口和最佳实践。
  做过Android JNI开发的小伙伴对Java和C++的互相调用有所了解,JNI提供了Java和C++的类型转换,与JNI不同的是NAPI中,TS调用C++的参数都封装到了一起:

static napi_value add(napi_env env, napi_callback_info info)  
{
}

  不管要传递多少个参数,都封装在napi_callback_info中,可以从napi_callback_info中获取全部参数信息:

size_t argc = 7;//参数个数
napi_value args[7] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

1.2. 创建Native C++ Module

1.2.1. 右键项目->new->module

在这里插入图片描述

1.2.2. 修改build-profile.json5配置

"externalNativeOptions": {"path": "./src/main/cpp/CMakeLists.txt","arguments": "-v -DOHOS_STL=c++_shared","abiFilters": [
//        "armeabi-v7a",
//        "x86_64","arm64-v8a"],"cppFlags": ""}

1.2.3. CMakeLists.txt 配置

# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(MyApplication43)set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})include_directories(${NATIVERENDER_ROOT_PATH}${NATIVERENDER_ROOT_PATH}/include)add_library(application SHARED SRRtcVideoEngineNapi.cpp SRRtcRoomCallBackNapi.cpp)
target_link_libraries(application PUBLIC libace_napi.z.so)

1.3. 代码实现

1.3.1. 主调接口实现

  NAPI中缓存回调接口的变量,便于后面回调给TS

napi_value SRRtcVideoEngineNapi::setRRoomCallBack(napi_env env, napi_callback_info info) {LogE("setRRoomCallBack---一个参数:ISRoomCallBack");size_t argc = 1;napi_value args[1] = {nullptr};napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);if (status != napi_ok) {napi_throw_error(env, "", "");return nullptr;}// 缓存回调函数全局变量,回调ets用if (SRGlobalvar::napi_CallbackReference == nullptr) {LogE("setRRoomCallBack===new NapiCallBack()");SRGlobalvar::napi_CallbackReference = new NapiCallBack(); // 创建缓存函数}napi_create_reference(env, args[0], 1, &SRGlobalvar::napi_CallbackReference->roomCallBack_napi);SRGlobalvar::napi_CallbackReference->env = env;// 调用 底层sdk : RRoomControlMgr.setCallBackRResult rResult = sr_engineSdk->setRRoomCallBackRtcEngine();return SRGlobalvar::returnResult(env, rResult);
}

1.3.2. 回调接口实现

  通过缓存的env,callback对象,调用napi_call_function方法将数据传回给ts

void SRRtcRoomCallBackNapi::onRoomJoinConfirm(RResult rResult, const RRoomInfo &roomInfo) {// 处理 onRoomJoinConfirm 通知LogE("回调消息---SRRtcRoomCallBack:onRoomJoinConfirm ");// 转换N-API对象napi_value roomInfo_napi = SRGlobalvar::convertToSRRoomInfo(SRGlobalvar::napi_CallbackReference->env, roomInfo);napi_value rResult_napi = SRGlobalvar::convertToSRResult(SRGlobalvar::napi_CallbackReference->env, rResult);// 传递给TSnapi_value callback;napi_get_reference_value(SRGlobalvar::napi_CallbackReference->env,SRGlobalvar::napi_CallbackReference->roomCallBack_napi, &callback);napi_value jsMethod;napi_get_named_property(SRGlobalvar::napi_CallbackReference->env, callback, "onRoomJoinConfirm", &jsMethod);napi_value argv[] = {rResult_napi, roomInfo_napi};napi_value callbackResult = nullptr;napi_call_function(SRGlobalvar::napi_CallbackReference->env, nullptr, jsMethod, 2, argv, &callbackResult);
}

1.4. ets的接收c++传回的数据

1.4.1. index.d.ts 代码增加接口

function setRRoomCallBack(sroomCallback: ISRoomCallBack): SRReult

1.4.2. 回调接口SRoomCallBack.ets

export class SRoomCallBack implements ISRoomCallBack {onRoomJoinConfirm(rResult: SRReult, roomInfo: SRRoomInfo) {SRLog.i(TAG, `onRoomJoinConfirm==回调测试完成=rResult:${JsonUtil.jsonToString(rResult)}\n roomInfo:${JsonUtil.jsonToString(roomInfo)}`)}
}

1.4.3. 调用

import srrtcNapi from 'librtcvideo.so';setRRoomCallBackRtcEngine(callback: ISRoomCallBack) {let srResult = srrtcNapi.setRRoomCallBack(callback)SRLog.i(TAG, "setRRoomCallBackRtcEngine===srresult:" + JsonUtil.jsonToString(srResult))}

1.5. 数据转换

1.5.1. ArkTS转C++类型

  TS基本数据类型:
(1)数字类型: number
(2)字符串类型 :string
(3)布尔类型: boolean
(4)任意精度整型: bigint
(5)对象类型: object
  C++基本数据类型:
(6)整型i:nt、short、long、long long
(7)浮点型:float、double、long double
(8)字符型: char
(9)布尔型: bool

1.5.2. 基本数据类型转换

  NAPI提供了上面两种语言对应的类型的转换:
(1)转换为布尔类型:napi_get_value_bool
(2)转换为int32:napi_get_value_int32
(3)转换为int64:napi_get_value_int64
(4)转换为无符号32位:napi_get_value_uint32
(5)转换为double:napi_get_value_double
(6)bitint 64位:napi_get_value_bigint_int64
(7)bitint 无符号64位:`napi_get_value_bigint_uint64
  除了bool类型,其他基本类型就是数值类型,TS中的数值类型对应C++各种细分类型,分别调用上面不同函数即可,调用方式:

int intValue;
napi_get_value_int32(env, args[0], &intValue);

  上面代码通过napi_get_value_int32将TS中的number转换为int赋值给intValue变量。

1.5.3. 字符串类型转换

  对于字符串和object对象处理稍微复杂些,通过napi_get_value_string_utf8将js的字符串对象转换为c++的std::string对象。但是要创建std:string需要先知道TS中传来的字符串的长度,看napi_get_value_string_utf8函数说明:

napi_status napi_get_value_string_utf8(napi_env env,napi_value value,char* buf,size_t bufsize,size_t* result)

[in] env: The environment that the API is invoked under.

[in] value: napi_value representing JavaScript string.

[in] buf: Buffer to write the UTF8-encoded string into. If NULL is passed in, the length of the string in bytes and excluding the null terminator is returned in result.

[in] bufsize: Size of the destination buffer. When this value is insufficient, the returned string is truncated and null-terminated.

[out] result: Number of bytes copied into the buffer, excluding the null terminator.

Returns napi_ok if the API succeeded. If a non-string napi_value is passed in it returns napi_string_expected.

  我们需要先创建一个char类型的空间去接收转换后的字符串,创建char空间需要指定大小,可以先调用一次napi_get_value_string_utf8,buf传入空获取到传入的数据大小,然后创建对应大小buf,再次调用napi_get_value_string_utf8获取转换后的字符串:

void JsValueToString(const napi_env &env, const napi_value &value, std::string &target) {  size_t result = 0;  napi_get_value_string_utf8(env, value, nullptr, 0, &result);  std::unique_ptr<char[]> buf(new char[result+1]);  if (buf.get() == nullptr) {  return;  }  (void)memset(buf.get(), 0, result+1);  napi_get_value_string_utf8(env, value, buf.get(), result+1, &result);  target = buf.get();  
}

  上面方法就是封装了一个转换字符串对应的工具函数。

1.5.4. object类型转换

  解析object对象参数和前面参数一样,通过napi_get_cb_info转换为napi_value对象后,通过napi_get_named_property获取对象中的属性值:

//ts对象:
export class Task {  public id: number; //unique task identify  public channel?: number;   //...
}
napi_value Demo::startTask(napi_env env, napi_callback_info info){size_t argc = 1;napi_value js_cb;napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr);napi_value taskIdNapiValue;napi_get_named_property(env, js_cb, "id", &taskIdNapiValue);int32_t taskid;napi_get_value_int32(env, taskIdNapiValue, &taskid);napi_value channelSelectNapiValue;napi_get_named_property(env, js_cb, "channelSelect", &channelSelectNapiValue);int32_t channel_select;napi_get_value_int32(env, channelSelectNapiValue, &channel_select);//....//调用业务逻辑return nullptr;
}

1.5.5. 数组类型类型转换

  和string类似,数组类型参数需要先通过napi_get_array_length参数获取数组长度,再通过napi_get_element获取对应每个napi_value对象,通过上述基本类型转换为对应C++对象:

napi_value Demo::setArrays(napi_env env, napi_callback_info info){std::vector<std::string> backupip_list;size_t argc = 1;napi_value args[1] = {nullptr};napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);bool is_array;//判断是否为数组napi_status status = napi_is_array(env, args[0], &is_array);if (!is_array) {return nullptr;}napi_value array = args[0];uint32_t length;napi_get_array_length(env,array,&length);for(int i = 0; i < length; i++){napi_value element;napi_get_element(env, array, i, &element); // 获取返回值数组的每个元素std::string ipStr;NapiUtil::JsValueToString(env, element, ipStr);backupip_list.push_back(ipStr);}   //调用业务逻辑return nullptr;
}

1.5.6. ArrayBuffer类型转换

  如果TS向C++传输二进制流,需要用到ArrayBuffer类型数据,在C++侧通过napi_get_arraybuffer_info转换成C++字节流,接口说明:

napi_status napi_get_arraybuffer_info(napi_env env,napi_value arraybuffer,void** data,size_t* byte_length)

[in] env: The environment that the API is invoked under.

[in] arraybuffer: napi_value representing the ArrayBuffer being queried.

[out] data: The underlying data buffer of the ArrayBuffer. If byte_length is 0, this may be NULL or any other pointer value.

[out] byte_length: Length in bytes of the underlying data buffer.
Returns napi_ok if the API succeeded.
This API is used to retrieve the underlying data buffer of an ArrayBuffer and its length.

  第三个参数传入空时,只会获取字节流大小。第三个参数是指向指针的指针,我们不需要创建空间,函数内部会创建空间:

napi_value Demo::setArrayBufferData(napi_env env, napi_callback_info info){size_t argc = 1;napi_value js_cb;napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr);// 获取 ArrayBuffer 对象的指针和长度 void* buffer; size_t length; napi_get_arraybuffer_info(env, arrayBuffer, &buffer, &length); // 打印 ArrayBuffer 中的数据 ,也可以修改ArrayBuffer的值uint32_t* data = (uint32_t*) buffer;//调用业务逻辑return nullptr;
}

1.5.7. C++类型转ArkTS 类型

 &emsp上面介绍ArkTS转C++时看到的示例代码一般返回nullptr,如果要返回具体类型的数据,不能直接返回C++类型的变量,也需要转换成TS类型变量,NAPI提供了napi_value类型,表示TS类型数据,创建对应TS类型变量的函数:

napi_create_uint32

napi_create_int64

napi_create_double

napi_create_bigint_int64

napi_create_bigint_uint64

napi_create_bigint_words

napi_create_string_latin1

node_api_create_external_string_latin1

napi_create_string_utf16

node_api_create_external_string_utf16

napi_create_string_utf8

napi_get_boolean

1.5.8. 使用示例:

napi_value Demo::hasSon(napi_env env, napi_callback_info info){size_t argc = 1;napi_value args[1] = {nullptr};napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);int id;napi_get_value_int32(env, args[0], &id);napi_value result;bool hasSonById = _HasSon(id);//创建布尔型js对象napi_get_boolean(env, hasSonById, &result);return result;
}
http://www.hkea.cn/news/256075/

相关文章:

  • 哈尔滨网站开发电话电商培训基地
  • 如何用php数据库做网站搜索seo优化托管
  • 中国城乡建设部人力网站首页优化落实疫情防控
  • 做网站到底能不能赚钱网络优化工程师前景
  • 乌镇网站建设标书百度站长工具域名查询
  • 制作公司网站价格腾讯广告代理商加盟
  • 大学生活动网站开发文案苏州seo门户网
  • 阿里云认证网站建设题库seo助理
  • 凤岗网站仿做靠谱seo外包定制
  • xampp安装wordpress说明徐州seo外包
  • 啥网站都能看的浏览器下载百度收录查询工具
  • 福田附近公司做网站建设哪家效益快奶糖 seo 博客
  • 临沂免费自助建站模板品牌整合营销
  • iis做本地视频网站找客户资源的网站
  • 做调查用哪个网站网络推广有多少种方法
  • 开发一个交易网站多少钱在线工具
  • 网站平台怎么建立的软文范例
  • 移动应用开发专业学什么东莞seo软件
  • 做宣传网站的公司手机百度极速版app下载安装
  • 私人可以做慈善网站吗外贸如何推广
  • 网站页面模板页面布局如何成为百度广告代理商
  • 瑞安外贸网站建设曲靖百度推广
  • 先做网站还是服务器销售营销方案100例
  • 用卫生纸做的礼物街网站免费网页空间到哪申请
  • 手游网站做cpc还是cpm广告号厦门网页搜索排名提升
  • 人个做外贸用什么网站好宁波百度seo点击软件
  • 诈骗网站怎么做的企业网站seo案例分析
  • 如何做网站接口湖南营销型网站建设
  • 进入兔展网站做PPt软文营销ppt
  • app网站新闻危机公关