From ff9fe486dac846ee71f658b63278a51924f66a15 Mon Sep 17 00:00:00 2001 From: Neucrack Date: Wed, 25 Sep 2024 16:00:22 +0800 Subject: [PATCH] fix complicated std::funtion python api compile failed error --- components/basic/include/maix_api_example.hpp | 15 ++++++++++++++- tools/doc_tool/gen_api.py | 14 ++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/components/basic/include/maix_api_example.hpp b/components/basic/include/maix_api_example.hpp index a0b638c5..bf692530 100644 --- a/components/basic/include/maix_api_example.hpp +++ b/components/basic/include/maix_api_example.hpp @@ -329,7 +329,7 @@ namespace maix /** * Callback example * @param cb callback function, param is two int type, return is int type - * @return int type, return value is cb(1, 2) + * @return int type, return value is cb's return value. * @maixpy maix.example.Example.callback */ static int callback(std::function cb) @@ -337,6 +337,19 @@ namespace maix return cb(1, 2); } + /** + * Callback example + * @param cb callback function, param is a int list type and int type, return is int type + * @return int type, return value is cb's return value. + * @maixpy maix.example.Example.callback2 + */ + static int callback2(std::function, int)> cb) + { + std::vector a; + a.push_back(1); + return cb(a, 2); + } + /** * Dict param example * @param dict dict type param, key is string type, value is int type diff --git a/tools/doc_tool/gen_api.py b/tools/doc_tool/gen_api.py index 5de7fdb0..83e5ac51 100644 --- a/tools/doc_tool/gen_api.py +++ b/tools/doc_tool/gen_api.py @@ -177,6 +177,7 @@ def get_var_name_value(definition): def get_func_def_info(code): ''' std::map get_dict(std::map in, + std::function, int)> cb, int i, const char *j = "10", std::vector v = {1, 2, 3}, std::vector &v2 = std::vector()), @@ -251,16 +252,19 @@ def get_func_def_info(code): } args_str = params_code.replace("\n", " ") if args_str: - except_start = None + except_start = [] param_str = "" + # e.g. std::function, int)> cb for i in range(len(args_str)): if except_start: - if args_str[i] == except_pair[except_start]: - except_start = None + if args_str[i] in ["<", "{"]: # more < + except_start.append(args_str[i]) + if args_str[i] == except_pair[except_start[-1]]: + except_start.pop() param_str += args_str[i] continue if args_str[i] in ["<", "{"]: - except_start = args_str[i] + except_start = [args_str[i]] param_str += args_str[i] continue if args_str[i] == ",": @@ -657,6 +661,8 @@ def parse_api_from_header(header_path, api_tree = {}, sdks = ["maixpy"], module_ if api_tree is None: raise Exception("parse_api_from_header {} error: {}".format(header_path, msg)) except Exception as e: + import traceback + traceback.print_exc() raise Exception("parse_api_from_header {} error: {}".format(header_path, e)) return api_tree, updated, keys