7.3. Generic parameters Arg

7.3.1. Header file

#include "dataArg.h"

7.3.2. Overview

  1. arg The generic argument API is a set of functions prefixed with arg_.

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

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

7.3.3. Data types

The data type of a generic argument is Arg.

typedef struct Arg Arg;
struct Arg {
    Arg* next;
    uint32_t size;
    uint8_t type;
    Hash name_hash;
    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.

Be careful not to access the internal members of arg directly, use the arg API to access arg. for maximum backward compatibility.

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

Arg* arg_newInt(int val);
Arg* arg_newFloat(double val);
Arg* arg_newPtr(ArgType type, void* pointer);
Arg* arg_newStr(char* val);
Arg* arg_newNull(void);
Arg* arg_newBytes(uint8_t* src, size_t size);

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

  • Destroy generic arguments.

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

Arg* arg_copy(Arg* self);

Pass in a pointer to the generic argument and destroy the generic argument.

7.3.5. Getting the value of a generic argument

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

ArgType arg_getType(Arg* self);

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

int64_t arg_getInt(Arg* self);
float arg_getFloat(Arg* self);
void* arg_getPtr(Arg* self);
char* arg_getStr(Arg* self);
uint8_t* arg_getBytes(Arg* self);
size_t arg_getBytesSize(Arg* self);

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.

7.3.7. Case

Build a list of strings using arg

## Include "PikaStdData_List.h"
...
    /* Create a list object */
    PikaObj* list = newNormalObj(New_PikaStdData_List);
    /* initialize list */
    PikaStdData_List___init__(list);
    /* Create arg with api of arg_new<type> */
    Arg* str_arg1 = arg_newStr("aaa");
    /* add to list object */
    PikaStdData_List_append(list, str_arg1);
    /* destroy arg */
    arg_deinit(str_arg1);
...