The open source OpenXR runtime

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

authored by

Mateo de Mayo and committed by
Jakob Bornecrantz
afa7f7ad f4cc2f3b

+118
+2
src/xrt/auxiliary/util/CMakeLists.txt
··· 80 80 u_trace_marker.h 81 81 u_var.cpp 82 82 u_var.h 83 + u_vector.cpp 84 + u_vector.h 83 85 u_config_json.c 84 86 u_config_json.h 85 87 u_verify.h
+44
src/xrt/auxiliary/util/u_vector.cpp
··· 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 + 13 + using 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 + 41 + extern "C" { 42 + U_VECTOR_IMPLEMENTATION(int) 43 + U_VECTOR_IMPLEMENTATION(float) 44 + }
+33
src/xrt/auxiliary/util/u_vector.h
··· 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 + #pragma once 11 + 12 + #include <stddef.h> 13 + 14 + #ifdef __cplusplus 15 + extern "C" { 16 + #endif 17 + 18 + #define U_VECTOR_DECLARATION(TYPE) \ 19 + struct u_vector_##TYPE \ 20 + { \ 21 + void *ptr; \ 22 + }; \ 23 + struct u_vector_##TYPE u_vector_##TYPE##_create(); \ 24 + void u_vector_##TYPE##_push_back(struct u_vector_##TYPE uv, TYPE e); \ 25 + TYPE u_vector_##TYPE##_at(struct u_vector_##TYPE uv, size_t i); \ 26 + void u_vector_##TYPE##_destroy(struct u_vector_##TYPE *uv); 27 + 28 + U_VECTOR_DECLARATION(int) 29 + U_VECTOR_DECLARATION(float) 30 + 31 + #ifdef __cplusplus 32 + } 33 + #endif
+1
tests/CMakeLists.txt
··· 21 21 tests_pacing 22 22 tests_quatexpmap 23 23 tests_rational 24 + tests_vector 24 25 tests_worker 25 26 tests_pose 26 27 )
+38
tests/tests_vector.cpp
··· 1 + // Copyright 2022, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Test u_vector C interface. 6 + * @author Mateo de Mayo <mateo.demayo@collabora.com> 7 + */ 8 + 9 + #include "catch/catch.hpp" 10 + #include "util/u_vector.h" 11 + 12 + TEST_CASE("u_vector") 13 + { 14 + SECTION("Test interface generated from macros") 15 + { 16 + struct u_vector_float vf = u_vector_float_create(); 17 + CHECK(vf.ptr != NULL); 18 + 19 + constexpr float A = 2.71f; 20 + constexpr float B = 1.61f; 21 + constexpr float C = 3.14f; 22 + 23 + u_vector_float_push_back(vf, A); 24 + u_vector_float_push_back(vf, B); 25 + u_vector_float_push_back(vf, C); 26 + 27 + float a = u_vector_float_at(vf, 0); 28 + float b = u_vector_float_at(vf, 1); 29 + float c = u_vector_float_at(vf, 2); 30 + 31 + CHECK(a == A); 32 + CHECK(b == B); 33 + CHECK(c == C); 34 + 35 + u_vector_float_destroy(&vf); 36 + CHECK(vf.ptr == NULL); 37 + } 38 + }