The open source OpenXR runtime
1// Copyright 2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Expose std::vector to C
6 * @author Mateo de Mayo <mateo.demayo@collabora.com>
7 * @ingroup aux_util
8 */
9
10#include "u_vector.h"
11#include <vector>
12
13using std::vector;
14
15#define U_VECTOR_IMPLEMENTATION(TYPE) \
16 u_vector_##TYPE u_vector_##TYPE##_create() \
17 { \
18 u_vector_##TYPE uv{new vector<TYPE>}; \
19 return uv; \
20 } \
21 \
22 void u_vector_##TYPE##_push_back(u_vector_##TYPE uv, TYPE e) \
23 { \
24 vector<TYPE> *v = static_cast<vector<TYPE> *>(uv.ptr); \
25 v->push_back(e); \
26 } \
27 \
28 TYPE u_vector_##TYPE##_at(u_vector_##TYPE uv, size_t i) \
29 { \
30 vector<TYPE> *v = static_cast<vector<TYPE> *>(uv.ptr); \
31 return v->at(i); \
32 } \
33 \
34 void u_vector_##TYPE##_destroy(u_vector_##TYPE *uv) \
35 { \
36 vector<TYPE> *v = static_cast<vector<TYPE> *>(uv->ptr); \
37 delete v; \
38 uv->ptr = nullptr; \
39 }
40
41extern "C" {
42U_VECTOR_IMPLEMENTATION(int)
43U_VECTOR_IMPLEMENTATION(float)
44}