mirror of
https://gitee.com/const-zpc/param.git
synced 2025-01-16 04:12:52 +08:00
🎈 perf: 删除按字符串查找参数,同时支持文本ID
1. 按字符串查找参数,字符串不唯一
This commit is contained in:
parent
270b38ca34
commit
7454ee0382
43
cot_param.c
43
cot_param.c
@ -353,49 +353,6 @@ cotParamInfo_t *cotParam_IterateList(const cotParamManager_t *pManager, size_t *
|
||||
return p;
|
||||
}
|
||||
|
||||
#if ( COT_PARAM_NAME_MAX_LENGTH > 1)
|
||||
static cotParamInfo_t *FindParamByName(const cotParamManager_t *pManager, const char *pszName)
|
||||
{
|
||||
for (uint16_t i = 0; i < pManager->count; ++i)
|
||||
{
|
||||
if (strcmp(pManager->pParamTable[i].pszName, pszName) == 0)
|
||||
{
|
||||
return &pManager->pParamTable[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 根据参数名称查找参数信息
|
||||
*
|
||||
* @attention 无可读权限时会查找失败
|
||||
* @param pManager 参数表管理句柄
|
||||
* @param pszName 参数名称
|
||||
* @return 参数信息
|
||||
*/
|
||||
const cotParamInfo_t *cotParam_FindParamByName(const cotParamManager_t *pManager, const char *pszName)
|
||||
{
|
||||
#if ( COT_PARAM_NAME_MAX_LENGTH > 1)
|
||||
cotParamInfo_t *pInfo;
|
||||
|
||||
if (pManager != NULL)
|
||||
{
|
||||
pInfo = FindParamByName(pManager, pszName);
|
||||
|
||||
if (pInfo->attr & COT_PARAM_ATTR_READ)
|
||||
{
|
||||
return pInfo;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static cotParamInfo_t *FindParamByID(const cotParamManager_t *pManager, uint16_t id)
|
||||
{
|
||||
for (uint16_t i = 0; i < pManager->count; ++i)
|
||||
|
@ -82,7 +82,8 @@
|
||||
#if COT_PARAM_USE_CUSTOM_CHECK
|
||||
#if (COT_PARAM_NAME_MAX_LENGTH > 1)
|
||||
#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \
|
||||
.pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \
|
||||
.name = {.textId = (size_t)(_name), .pTextString = (char*)(_name)}, \
|
||||
.id = _id, .type = _type, .length = _length, .attr = _attr, \
|
||||
.unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \
|
||||
.unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal, .pfnParamCheck = _check}
|
||||
#else
|
||||
@ -95,7 +96,8 @@
|
||||
#else
|
||||
#if (COT_PARAM_NAME_MAX_LENGTH > 1)
|
||||
#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \
|
||||
.pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \
|
||||
.name = {.textId = (size_t)(_name), .pTextString = (char*)(_name)}, \
|
||||
.id = _id, .type = _type, .length = _length, .attr = _attr, \
|
||||
.unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \
|
||||
.unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal}
|
||||
#else
|
||||
@ -148,7 +150,6 @@ extern int cotParam_Deserialization(const cotParamManager_t* pManager, const uin
|
||||
|
||||
extern cotParamInfo_t *cotParam_IterateList(const cotParamManager_t *pManager, size_t *psIdx);
|
||||
|
||||
extern const cotParamInfo_t *cotParam_FindParamByName(const cotParamManager_t *pManager, const char *pszName);
|
||||
extern const cotParamInfo_t *cotParam_FindParamByID(const cotParamManager_t* pManager, uint16_t id);
|
||||
extern const cotParamInfo_t *cotParam_FindParamByParamPtr(const cotParamManager_t* pManager, const void *pCurParam);
|
||||
|
||||
|
@ -117,6 +117,13 @@ typedef union {
|
||||
|
||||
typedef int (*cotParamCheck_f)(const void *pCurParam);
|
||||
|
||||
typedef union
|
||||
{
|
||||
size_t textId; /*!< 文本ID */
|
||||
|
||||
char *pTextString; /*!< 文本字符串 */
|
||||
} cotParamDsecStr_u;
|
||||
|
||||
/**
|
||||
* @brief 定义无内存的参数结构体信息
|
||||
*
|
||||
@ -124,7 +131,7 @@ typedef int (*cotParamCheck_f)(const void *pCurParam);
|
||||
typedef struct stuParamInfo
|
||||
{
|
||||
#if ( COT_PARAM_NAME_MAX_LENGTH > 1)
|
||||
const char *pszName; /*!< 名称 */
|
||||
cotParamDsecStr_u name; /*!< 名称 */
|
||||
#endif
|
||||
uint16_t id; /*!< 唯一ID */
|
||||
uint8_t type; /*!< 类型, @enum ParamType_e */
|
||||
|
@ -235,7 +235,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
switch (paramInfo->type)
|
||||
{
|
||||
case COT_PARAM_INT8:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"int8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt8);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -255,7 +255,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_INT16:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"int16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt16);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -275,7 +275,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_INT32:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"int32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt32);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -295,7 +295,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_INT64:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"int64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt64);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -315,7 +315,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_UINT8:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"uint8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint8);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -335,7 +335,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_UINT16:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"uint16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint16);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -355,7 +355,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_UINT32:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"uint32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint32);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -375,7 +375,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_UINT64:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"uint64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint64);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -395,7 +395,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_FLOAT:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"float", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pFloat);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -415,7 +415,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
}
|
||||
break;
|
||||
case COT_PARAM_DOUBLE:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"double", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pDouble);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
@ -436,7 +436,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo)
|
||||
break;
|
||||
#if COT_PARAM_USE_STRING_TYPE
|
||||
case COT_PARAM_STRING:
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->pszName,
|
||||
printf(" %-4d %-24s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->name.pTextString,
|
||||
"string", paramInfo->length, Attr(paramInfo->attr), paramInfo->unCurValuePtr.pString);
|
||||
|
||||
if (paramInfo->attr & COT_PARAM_ATTR_RESET)
|
||||
|
@ -130,7 +130,7 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].length, 2);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].type, COT_PARAM_INT16);
|
||||
TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[0].attr, COT_PARAM_ATTR_WR);
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[0].pszName, "g_test_1");
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[0].name.pTextString, "g_test_1");
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unCurValuePtr.pVoid , &g_test_1);
|
||||
TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unDefValuePtr.pVoid);
|
||||
TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unMinValuePtr.pVoid);
|
||||
@ -140,7 +140,7 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].length, 2);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].type, COT_PARAM_UINT16);
|
||||
TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[1].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET);
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[1].pszName, "g_test_2");
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[1].name.pTextString, "g_test_2");
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unCurValuePtr.pVoid , &g_test_2);
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[1].unDefValuePtr.pVoid);
|
||||
TEST_ASSERT_NULL(sg_tParamManager.pParamTable[1].unMinValuePtr.pVoid);
|
||||
@ -150,7 +150,7 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].length, 4);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].type, COT_PARAM_FLOAT);
|
||||
TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[2].attr, COT_PARAM_ATTR_READ | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE);
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[2].pszName, "g_test_float");
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[2].name.pTextString, "g_test_float");
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unCurValuePtr.pVoid , &g_test_float);
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unDefValuePtr.pVoid);
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unMinValuePtr.pVoid);
|
||||
@ -162,7 +162,7 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].length, sizeof(g_test_str));
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].type, COT_PARAM_STRING);
|
||||
TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[4].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE);
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[4].pszName, "g_test_str");
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[4].name.pTextString, "g_test_str");
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unCurValuePtr.pVoid , g_test_str);
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unDefValuePtr.pVoid);
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unMinValuePtr.pVoid);
|
||||
@ -172,7 +172,7 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].length, 10);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].type, COT_PARAM_STRING);
|
||||
TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[3].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE);
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[3].pszName, "g_test_str");
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[3].name.pTextString, "g_test_str");
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unCurValuePtr.pVoid , COT_PARAM_STR_CUR_VALUE(g_test_str));
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unDefValuePtr.pVoid);
|
||||
TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unMinValuePtr.pVoid);
|
||||
@ -379,10 +379,7 @@ void test_IterateParam(void)
|
||||
void test_FindParam(void)
|
||||
{
|
||||
const cotParamInfo_t *paramInfo;
|
||||
#if COT_PARAM_USE_64_BIT_LENGTH
|
||||
paramInfo = cotParam_FindParamByName(&sg_tParamManager, "g_test_double");
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[3], paramInfo);
|
||||
#endif
|
||||
|
||||
paramInfo = cotParam_FindParamByID(&sg_tParamManager, 3);
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[2], paramInfo);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user