北大黃雨松同學為北太天元開發(fā)插件的知識點-typeid的使用

/**
?* 北太天元的插件開發(fā)示例,這個還不是完整的,我這幾天的視頻的想法是
?* 每次介紹很少的幾個知識點。 這是和前一個視頻相比增加了
?* 類型檢查。
?*/
#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::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++){
???????????????std::cout<<"類型"<<i<<std::endl;
???????????????std::cout<<infos[i]->name() <<std::endl;
???????????????std::cout<<"const double *的類型名稱:"<<typeid(const double *).name() <<std::endl;
???????????????std::cout<<"double *的類型名稱:" <<typeid(double *).name() <<std::endl;
???????????????std::cout<<typeid(const int32_t *).name() <<std::endl;
???????????????std::cout<<typeid(int32_t *).name() <<std::endl;
???????????????std::cout<<typeid(const int64_t *).name() <<std::endl;
???????????????std::cout<<typeid(int64_t *).name() <<std::endl;
???????????????std::cout<<typeid(const std::string *).name() <<std::endl;
???????????????std::cout<<typeid(std::string *).name() <<std::endl;
???????????????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])));
??????????????????/**
??????????????????* 這兒new 的東西,沒有delete,會有內(nèi)存泄漏
??????????????????*/
???????????????}
???????????????else {
?????????????????????bxPrintf("第%d輸入?yún)?shù)類型不對",i);
?????????????????????return 1;
???????????????}
????????????}
????????????return 0;
?????????}
??????};
}
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();
}
static bexfun_info_t flist[] = {
?{"cmd_h", cmd_h, nullptr},
?{"", nullptr, nullptr},
};
bexfun_info_t *bxPluginFunctions() {
?return flist;
}