国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

利用std::shared_ptr 回收不同類型對象的內(nèi)存

2023-05-16 22:44 作者:盧朓  | 我要投稿

/**
?* 北太天元的插件開發(fā)示例,
?* 每次介紹很少的幾個知識點,面向的對象是對c++11之后的特性不熟悉的同學(xué)。
?* 這一次是增加了用 std::shared_ptr<void> 回收不同類型對象的內(nèi)存的功能
?*/

#include <memory>
#include <cstdlib>
#include <iostream>
#include <typeinfo>
#include <utility>
#include <array>
#include <map>
#include <vector>
#include "bex/bex.hpp"

namespace ParseParams {
?? ?template <class _T>
?? ??? ?class FunTrait;
?? ?template <typename R, typename... Args>
?? ??? ?class FunTrait<R(Args...)>{
?? ??? ??? ?public:
?? ??? ??? ??? ?static constexpr size_t n_args = sizeof...(Args);
?? ??? ??? ??? ?static constexpr const std::array<const std::type_info *, n_args> infos = {&typeid(Args)...};
?? ??? ??? ?
?? ??? ??? ?public:
?? ??? ??? ?int required_params;
?? ??? ??? ?std::vector<std::shared_ptr<void>> trash_bin;
?? ??? ??? ?std::array<void *, n_args> passed_args_ptr;
?? ??? ??? ?//變量類型函數(shù)句柄, 變量名是decorated_func
?? ??? ??? ?R(*decorated_func)
?? ??? ??? ??? ?(Args...);


?? ??? ??? ?public:
?? ??? ??? ?FunTrait(R (*func)(Args...), int num_required = 0){
?? ??? ??? ??? ?decorated_func = func;
?? ??? ??? ?? required_params = num_required;
?? ??? ??? ?}

?? ??? ??? ?template <size_t... I>
?? ??? ??? ?R eval_impl(std::index_sequence<I...>){
?? ??? ??? ??? ?std::cout<<"必須的參數(shù)個數(shù)是 = " << required_params << std::endl;
?? ??? ??? ??? ?return decorated_func((Args)passed_args_ptr[I]...);
?? ??? ??? ?}

?? ??? ??? ?R eval(){
?? ??? ??? ??? ?return eval_impl(std::make_index_sequence<n_args>());
?? ??? ??? ?}

?? ??? ??? ?int check_in_args_type(int nrhs, const bxArray * prhs[]){
?? ??? ??? ??? ?for(size_t i= 0; i< n_args; i++){
?? ??? ??? ??? ??? ?if(infos[i]->name() == typeid(const double *).name()){
?? ??? ??? ??? ??? ??? ?if(!bxIsDouble(prhs[i])){
?? ??? ??? ??? ??? ??? ??? ?bxPrintf("第%d輸入?yún)?shù)必須是double類型",i);
?? ??? ??? ??? ??? ??? ??? ?return 1;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?passed_args_ptr[i] = (void *)(bxGetDoubles(prhs[i]));
?? ??? ??? ??? ??? ?}?? ?
?? ??? ??? ??? ??? ?else if(infos[i]->name() == typeid(const int32_t *).name()){
?? ??? ??? ??? ??? ??? ?if(!bxIsInt32(prhs[i])){
?? ??? ??? ??? ??? ??? ??? ?bxPrintf("第%d輸入?yún)?shù)必須是int32類型",i);
?? ??? ??? ??? ??? ??? ??? ?return 1;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?passed_args_ptr[i] = (void *)(bxGetInt32s(prhs[i]));
?? ??? ??? ??? ??? ?}?? ?
?? ??? ??? ??? ??? ?else if(infos[i]->name() == typeid(const int64_t *).name()){
?? ??? ??? ??? ??? ??? ?if(!bxIsInt64(prhs[i])){
?? ??? ??? ??? ??? ??? ??? ?bxPrintf("第%d輸入?yún)?shù)必須是int64類型",i);
?? ??? ??? ??? ??? ??? ??? ?return 1;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?passed_args_ptr[i] = (void *)(bxGetInt64s(prhs[i]));
?? ??? ??? ??? ??? ?}?? ?
?? ??? ??? ??? ??? ?else if(infos[i]->name() == typeid(const std::string *).name()){
?? ??? ??? ??? ??? ??? ?if(!bxIsString(prhs[i])){
?? ??? ??? ??? ??? ??? ??? ?bxPrintf("第%d輸入?yún)?shù)必須是string類型",i);
?? ??? ??? ??? ??? ??? ??? ?return 1;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?passed_args_ptr[i] = (void *)(new std::string(bxGetStringDataPr(prhs[i])));
?? ??? ??? ??? ??? ??? ?trash_bin.push_back(std::shared_ptr<std::string>((std::string *)passed_args_ptr[i]));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ??? ??? ?bxPrintf("第%d輸入?yún)?shù)類型不對",i);
?? ??? ??? ??? ??? ??? ??? ?return 1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return 0;
?? ??? ??? ?}

?? ??? ??? ?void return_to_bxArray(R result, int nlhs, bxArray *plhs[]){
?? ??? ??? ??? ?if(nlhs <= 0 ) return;
?? ??? ??? ??? ?if constexpr (std::is_same<char, R>::value){
?? ??? ??? ??? ??? ?char tmp[2] ={result, '\0'};
?? ??? ??? ??? ??? ?plhs[0] = bxCreateString(tmp);?? ?
?? ??? ??? ??? ?}?? ?
?? ??? ??? ??? ?else if constexpr (std::is_same<int32_t,R>::value){
?? ??? ??? ??? ??? ?plhs[0] = bxCreateInt32Scalar(result);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if constexpr (std::is_same<double,R>::value){
?? ??? ??? ??? ??? ?plhs[0] = bxCreateDoubleMatrix(1,1,bxREAL);
?? ??? ??? ??? ??? ?double * ptr = bxGetDoubles(plhs[0]);
?? ??? ??? ??? ??? ?*ptr = result;
?? ??? ??? ??? ?}?? ?
?? ??? ??? ??? ?else if constexpr (std::is_same<std::string,R>::value){
?? ??? ??? ??? ??? ?plhs[0] = bxCreateString(result.c_str());
?? ??? ??? ??? ?}
?? ??? ??? ?}

?? ??? ?};
}


char h(const int *j , const std::string? *str ){
?? ?std::cout<<" *j = "<<*j << std::endl;
?? ?std::cout<<" *str = "<<*str << std::endl;
?? ?std::cout<<" (*str)[*j] = "<<(*str)[*j]<< std::endl;
?? ?return (*str)[*j];
}

void cmd_h(int nlhs, bxArray *plhs[], int nrhs, const bxArray *prhs[]) {
?? ?ParseParams::FunTrait<decltype(h)> q(h,0);
?? ?if(nrhs < q.n_args ){
?? ??? ?bxPrintf("輸入?yún)?shù)%d < %d", nrhs, q.n_args);
?? ??? ?return;
?? ?}?? ?
?? ?if(0 != q.check_in_args_type(nrhs, prhs)){
?? ??? ?bxPrintf("輸入?yún)?shù)賦值出錯\n");
?? ??? ?return;
?? ?}
?? ?auto result = q.eval();
?? ?q.return_to_bxArray(result, nlhs, plhs);

}


static bexfun_info_t flist[] = {
? {"cmdd_h", cmd_h, nullptr},
? {"", nullptr, nullptr},
};

bexfun_info_t *bxPluginFunctions() {
? return flist;
}


利用std::shared_ptr 回收不同類型對象的內(nèi)存的評論 (共 條)

分享到微博請遵守國家法律
红原县| 讷河市| 阿尔山市| 忻城县| 无极县| 丹阳市| 望江县| 确山县| 舞阳县| 金乡县| 石首市| 潢川县| 紫阳县| 遂宁市| 新乡县| 菏泽市| 枣强县| 甘谷县| 九寨沟县| 襄樊市| 大丰市| 望都县| 仁布县| 简阳市| 伊川县| 鄂托克前旗| 东光县| 江川县| 砀山县| 连山| 北海市| 浙江省| 通山县| 柳林县| 荔波县| 平昌县| 南郑县| 尤溪县| 西乡县| 伊宁县| 宝应县|