refactor: changed error check
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
#include "zh_avr_vector.h"
|
||||
|
||||
#define ZH_ERROR_CHECK(cond, err, ...) \
|
||||
if (!(cond)) \
|
||||
{ \
|
||||
return err; \
|
||||
}
|
||||
|
||||
static avr_err_t _resize(zh_avr_vector_t *vector, uint16_t capacity);
|
||||
|
||||
avr_err_t zh_avr_vector_init(zh_avr_vector_t *vector, uint16_t unit)
|
||||
{
|
||||
if (vector == NULL || unit == 0)
|
||||
{
|
||||
return AVR_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == true)
|
||||
{
|
||||
return AVR_ERR_INVALID_STATE;
|
||||
}
|
||||
ZH_ERROR_CHECK((vector != NULL || unit != 0), AVR_ERR_INVALID_ARG);
|
||||
ZH_ERROR_CHECK(vector->status != true, AVR_ERR_INVALID_STATE);
|
||||
vector->capacity = 0;
|
||||
vector->size = 0;
|
||||
vector->unit = unit;
|
||||
@@ -21,14 +21,8 @@ avr_err_t zh_avr_vector_init(zh_avr_vector_t *vector, uint16_t unit)
|
||||
|
||||
avr_err_t zh_avr_vector_free(zh_avr_vector_t *vector)
|
||||
{
|
||||
if (vector == NULL)
|
||||
{
|
||||
return AVR_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
return AVR_ERR_INVALID_STATE;
|
||||
}
|
||||
ZH_ERROR_CHECK(vector != NULL, AVR_ERR_INVALID_ARG);
|
||||
ZH_ERROR_CHECK(vector->status != false, AVR_ERR_INVALID_STATE);
|
||||
for (uint16_t i = 0; i < vector->size; ++i)
|
||||
{
|
||||
free(vector->items[i]);
|
||||
@@ -39,49 +33,28 @@ avr_err_t zh_avr_vector_free(zh_avr_vector_t *vector)
|
||||
|
||||
avr_err_t zh_avr_vector_get_size(zh_avr_vector_t *vector)
|
||||
{
|
||||
if (vector == NULL || vector->status == false)
|
||||
{
|
||||
return AVR_FAIL;
|
||||
}
|
||||
ZH_ERROR_CHECK((vector != NULL || vector->status != false), AVR_FAIL);
|
||||
return vector->size;
|
||||
}
|
||||
|
||||
avr_err_t zh_avr_vector_push_back(zh_avr_vector_t *vector, void *item)
|
||||
{
|
||||
if (vector == NULL || item == NULL)
|
||||
{
|
||||
return AVR_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
return AVR_ERR_INVALID_STATE;
|
||||
}
|
||||
ZH_ERROR_CHECK((vector != NULL || item != NULL), AVR_ERR_INVALID_ARG);
|
||||
ZH_ERROR_CHECK(vector->status != false, AVR_ERR_INVALID_STATE);
|
||||
if (vector->capacity == vector->size)
|
||||
{
|
||||
if (_resize(vector, vector->capacity + 1) == AVR_ERR_NO_MEM)
|
||||
{
|
||||
return AVR_ERR_NO_MEM;
|
||||
}
|
||||
ZH_ERROR_CHECK((_resize(vector, vector->capacity + 1) != AVR_ERR_NO_MEM), AVR_ERR_NO_MEM);
|
||||
}
|
||||
vector->items[vector->size] = calloc(1, vector->unit);
|
||||
if (vector->items[vector->size] == NULL)
|
||||
{
|
||||
return AVR_ERR_NO_MEM;
|
||||
}
|
||||
ZH_ERROR_CHECK(vector->items[vector->size] != NULL, AVR_ERR_NO_MEM);
|
||||
memcpy(vector->items[vector->size++], item, vector->unit);
|
||||
return AVR_OK;
|
||||
}
|
||||
|
||||
avr_err_t zh_avr_vector_change_item(zh_avr_vector_t *vector, uint16_t index, void *item)
|
||||
{
|
||||
if (vector == NULL || item == NULL)
|
||||
{
|
||||
return AVR_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
return AVR_ERR_INVALID_STATE;
|
||||
}
|
||||
ZH_ERROR_CHECK((vector != NULL || item != NULL), AVR_ERR_INVALID_ARG);
|
||||
ZH_ERROR_CHECK(vector->status != false, AVR_ERR_INVALID_STATE);
|
||||
if (index < vector->size)
|
||||
{
|
||||
memcpy(vector->items[index], item, vector->unit);
|
||||
@@ -92,14 +65,7 @@ avr_err_t zh_avr_vector_change_item(zh_avr_vector_t *vector, uint16_t index, voi
|
||||
|
||||
void *zh_avr_vector_get_item(zh_avr_vector_t *vector, uint16_t index)
|
||||
{
|
||||
if (vector == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
ZH_ERROR_CHECK((vector != NULL || vector->status != false), NULL);
|
||||
if (index < vector->size)
|
||||
{
|
||||
void *item = vector->items[index];
|
||||
@@ -113,14 +79,8 @@ void *zh_avr_vector_get_item(zh_avr_vector_t *vector, uint16_t index)
|
||||
|
||||
avr_err_t zh_avr_vector_delete_item(zh_avr_vector_t *vector, uint16_t index)
|
||||
{
|
||||
if (vector == NULL)
|
||||
{
|
||||
return AVR_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
return AVR_ERR_INVALID_STATE;
|
||||
}
|
||||
ZH_ERROR_CHECK(vector != NULL, AVR_ERR_INVALID_ARG);
|
||||
ZH_ERROR_CHECK(vector->status != false, AVR_ERR_INVALID_STATE);
|
||||
if (index < vector->size)
|
||||
{
|
||||
free(vector->items[index]);
|
||||
@@ -141,10 +101,7 @@ static avr_err_t _resize(zh_avr_vector_t *vector, uint16_t capacity)
|
||||
if (capacity != 0)
|
||||
{
|
||||
vector->items = realloc(vector->items, sizeof(void *) * capacity);
|
||||
if (vector->items == NULL)
|
||||
{
|
||||
return AVR_ERR_NO_MEM;
|
||||
}
|
||||
ZH_ERROR_CHECK(vector->items != NULL, AVR_ERR_NO_MEM);
|
||||
}
|
||||
vector->capacity = capacity;
|
||||
return AVR_OK;
|
||||
|
Reference in New Issue
Block a user