u/vector: Add generic wrapper for std::vector

This commit is contained in:
Mateo de Mayo 2022-07-07 15:35:04 +00:00 committed by Jakob Bornecrantz
parent f4cc2f3bf4
commit afa7f7ade9
5 changed files with 118 additions and 0 deletions

View file

@ -80,6 +80,8 @@ add_library(
u_trace_marker.h
u_var.cpp
u_var.h
u_vector.cpp
u_vector.h
u_config_json.c
u_config_json.h
u_verify.h

View file

@ -0,0 +1,44 @@
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Expose std::vector to C
* @author Mateo de Mayo <mateo.demayo@collabora.com>
* @ingroup aux_util
*/
#include "u_vector.h"
#include <vector>
using std::vector;
#define U_VECTOR_IMPLEMENTATION(TYPE) \
u_vector_##TYPE u_vector_##TYPE##_create() \
{ \
u_vector_##TYPE uv{new vector<TYPE>}; \
return uv; \
} \
\
void u_vector_##TYPE##_push_back(u_vector_##TYPE uv, TYPE e) \
{ \
vector<TYPE> *v = static_cast<vector<TYPE> *>(uv.ptr); \
v->push_back(e); \
} \
\
TYPE u_vector_##TYPE##_at(u_vector_##TYPE uv, size_t i) \
{ \
vector<TYPE> *v = static_cast<vector<TYPE> *>(uv.ptr); \
return v->at(i); \
} \
\
void u_vector_##TYPE##_destroy(u_vector_##TYPE *uv) \
{ \
vector<TYPE> *v = static_cast<vector<TYPE> *>(uv->ptr); \
delete v; \
uv->ptr = nullptr; \
}
extern "C" {
U_VECTOR_IMPLEMENTATION(int)
U_VECTOR_IMPLEMENTATION(float)
}

View file

@ -0,0 +1,33 @@
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Expose std::vector to C
* @author Mateo de Mayo <mateo.demayo@collabora.com>
* @ingroup aux_util
*/
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#define U_VECTOR_DECLARATION(TYPE) \
struct u_vector_##TYPE \
{ \
void *ptr; \
}; \
struct u_vector_##TYPE u_vector_##TYPE##_create(); \
void u_vector_##TYPE##_push_back(struct u_vector_##TYPE uv, TYPE e); \
TYPE u_vector_##TYPE##_at(struct u_vector_##TYPE uv, size_t i); \
void u_vector_##TYPE##_destroy(struct u_vector_##TYPE *uv);
U_VECTOR_DECLARATION(int)
U_VECTOR_DECLARATION(float)
#ifdef __cplusplus
}
#endif

View file

@ -21,6 +21,7 @@ set(tests
tests_pacing
tests_quatexpmap
tests_rational
tests_vector
tests_worker
tests_pose
)

38
tests/tests_vector.cpp Normal file
View file

@ -0,0 +1,38 @@
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Test u_vector C interface.
* @author Mateo de Mayo <mateo.demayo@collabora.com>
*/
#include "catch/catch.hpp"
#include "util/u_vector.h"
TEST_CASE("u_vector")
{
SECTION("Test interface generated from macros")
{
struct u_vector_float vf = u_vector_float_create();
CHECK(vf.ptr != NULL);
constexpr float A = 2.71f;
constexpr float B = 1.61f;
constexpr float C = 3.14f;
u_vector_float_push_back(vf, A);
u_vector_float_push_back(vf, B);
u_vector_float_push_back(vf, C);
float a = u_vector_float_at(vf, 0);
float b = u_vector_float_at(vf, 1);
float c = u_vector_float_at(vf, 2);
CHECK(a == A);
CHECK(b == B);
CHECK(c == C);
u_vector_float_destroy(&vf);
CHECK(vf.ptr == NULL);
}
}