/*
 * Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Buffer view
 *
 */
typedef struct {
    /// Const pointer to data array
    const void* data;

    /// Length of data
    size_t len;
} ibmw_buffer_view_t;

/**
 * @brief Buffer array view
 *
 */
typedef struct {
    /// Const pointer to buffer array
    const ibmw_buffer_view_t* data;

    /// Length of data
    size_t len;
} ibmw_buffer_array_view_t;

/**
 * @brief Buffer
 *
 */
typedef struct {
    /// Pointer to data array
    void* data;

    /// Length of data
    size_t len;
} ibmw_buffer_t;

/**
 * @brief Buffer array
 *
 */
typedef struct {
    /// Pointer to buffer array
    ibmw_buffer_t* data;

    /// Length of buffer array
    size_t len;

    /// Capacity of buffer array
    size_t capacity;
} ibmw_buffer_array_t;

/**
 * @brief Buffer array allocator interface
 * @note
 * A buffer array should use the same allocator to allocate and release memory
 */
typedef struct {
    /**
     * @brief Function to reserve new capacity for a buffer array
     * @note
     * 1. The new capacity size must be greater than or equal to the old value
     * 2. The value of buffer_array->data may changed after reserve
     * 3. Return false if reserve failed, and the value of buffer_array will not be changed
     *
     * Input 1: Pointer to ctx
     * Input 2: Pointer to buffer array
     * Input 3: Size of new capacity
     * Output: True if reserve success, false if failed
     */
    bool (*reserve)(void* ctx, ibmw_buffer_array_t* bufferArray, size_t newCap);

    /**
     * @brief Function to allocate a new buffer for a buffer array
     * @note
     * 1. The length of buffer_array will increase by 1 after allocate
     * 2. The reserve function will be called if the capacity of buffer_array is too small
     * 3. The new buffer will be added to the end of buffer_array
     * 4. Return empty buffer if allocate failed
     *
     * Input 1: Pointer to ctx
     * Input 2: Pointer to buffer array
     * Input 3: Size of new buffer
     * Output: New buffer
     */
    ibmw_buffer_t (*allocate)(void* ctx, ibmw_buffer_array_t* bufferArray, size_t newBufferSize);

    /**
     * @brief Function to release the buffer array
     * @note
     * 1. The value of buffer_array->data will be NULL after release
     * 2. The value of buffer_array->len and buffer_array->capacity will be zero after release
     *
     * Input 1: Pointer to ctx
     * Input 2: Pointer to buffer array
     */
    void (*release)(void* ctx, ibmw_buffer_array_t* bufferArray);

    /// Implement pointer
    void* ctx;
} ibmw_buffer_array_allocator_t;

/**
 * @brief Buffer array with allocator
 *
 */
typedef struct {
    /// Pointer to buffer array
    ibmw_buffer_array_t* buffer_array;

    /// Pointer to allocator
    const ibmw_buffer_array_allocator_t* allocator;
} ibmw_buffer_array_with_allocator_t;

#ifdef __cplusplus
}
#endif