7.3. Generic parameters Arg

    7.3.2. Overview

    1. arg can hold a value of any type in it. The types supported by arg are: int, float, pointer, string, null, bytes. 1.

    2. arg can be put into an object and the value of arg can be accessed directly in the python script.

    The data type of a generic argument is Arg.

    1. Arg* next;
    2. uint32_t size;
    3. uint8_t type;
    4. Hash name_hash;
    5. uint8_t content[];

    The generic arguments internally include header information ( size, type, name_hash ), the data body (content), and a pointer (next) used to form the chain.

    7.3.4. Creating and destroying generic arguments

    • Creating a new generic argument

    Creates a new generic argument from the heap and returns a pointer to the generic argument.

    **Note that newly created generic parameters need to be manually destroyed to reclaim memory. Constantly creating new generic parameters but not destroying them can lead to memory leaks. **

    [Note] The following api requires a kernel version of at least v1.9.2

    New arg The argument passed in is the value of arg.

    • Destroy generic arguments.
    1. void arg_deinit(Arg* self);
    • Copy generic arguments

    Use the following API to determine the current type of a generic argument.

    1. ArgType arg_getType(Arg* self);

    Use the following API to convert a generic argument to a basic type.

    7.3.6. Important Notes

    Direct use of the arg_new<Type>() api **is highly likely to cause **memory leaks or dangling references, resulting in fatal flaws.

    Please develop under docker development environment to ensure sufficient unit testing and memory checking.

    1. ...
    2. /* Create a list object */
    3. PikaObj* list = newNormalObj(New_PikaStdData_List);
    4. PikaStdData_List___init__(list);
    5. /* Create arg with api of arg_new<type> */
    6. Arg* str_arg1 = arg_newStr("aaa");
    7. /* add to list object */
    8. PikaStdData_List_append(list, str_arg1);
    9. /* destroy arg */