The open source OpenXR runtime
at main 163 lines 3.0 kB view raw
1// Copyright 2021, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief A collection of strings, like a list of extensions to enable 6 * 7 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 8 * @ingroup aux_util 9 * 10 */ 11 12#include "u_string_list.h" 13#include "u_string_list.hpp" 14 15using xrt::auxiliary::util::StringList; 16 17 18struct u_string_list 19{ 20 u_string_list() = default; 21 u_string_list(StringList &&sl) : list(std::move(sl)) {} 22 23 StringList list; 24}; 25 26struct u_string_list * 27u_string_list_create() 28{ 29 try { 30 auto ret = std::make_unique<u_string_list>(); 31 return ret.release(); 32 } catch (std::exception const &) { 33 return nullptr; 34 } 35} 36 37 38struct u_string_list * 39u_string_list_create_with_capacity(uint32_t capacity) 40{ 41 42 try { 43 auto ret = std::make_unique<u_string_list>(xrt::auxiliary::util::StringList{capacity}); 44 return ret.release(); 45 } catch (std::exception const &) { 46 return nullptr; 47 } 48} 49 50struct u_string_list * 51u_string_list_create_from_list(struct u_string_list *usl) 52{ 53 try { 54 auto ret = std::make_unique<u_string_list>(xrt::auxiliary::util::StringList{usl->list}); 55 return ret.release(); 56 } catch (std::exception const &) { 57 return nullptr; 58 } 59} 60 61struct u_string_list * 62u_string_list_create_from_array(const char *const *arr, uint32_t size) 63{ 64 if (arr == nullptr || size == 0) { 65 return u_string_list_create(); 66 } 67 try { 68 auto ret = std::make_unique<u_string_list>(xrt::auxiliary::util::StringList{size}); 69 for (uint32_t i = 0; i < size; ++i) { 70 ret->list.push_back(arr[i]); 71 } 72 return ret.release(); 73 } catch (std::exception const &) { 74 return nullptr; 75 } 76} 77 78uint32_t 79u_string_list_get_size(const struct u_string_list *usl) 80{ 81 82 if (usl == nullptr) { 83 return 0; 84 } 85 return usl->list.size(); 86} 87 88 89const char *const * 90u_string_list_get_data(const struct u_string_list *usl) 91{ 92 93 if (usl == nullptr) { 94 return nullptr; 95 } 96 return usl->list.data(); 97} 98 99 100int 101u_string_list_append(struct u_string_list *usl, const char *str) 102{ 103 if (usl == nullptr) { 104 return -1; 105 } 106 try { 107 usl->list.push_back(str); 108 return 1; 109 } catch (std::exception const &) { 110 return -1; 111 } 112} 113 114int 115u_string_list_append_array(struct u_string_list *usl, const char *const *arr, uint32_t size) 116{ 117 118 if (usl == nullptr) { 119 return -1; 120 } 121 try { 122 for (uint32_t i = 0; i < size; ++i) { 123 usl->list.push_back(arr[i]); 124 } 125 return 1; 126 } catch (std::exception const &) { 127 return -1; 128 } 129} 130 131int 132u_string_list_append_unique(struct u_string_list *usl, const char *str) 133{ 134 if (usl == nullptr) { 135 return -1; 136 } 137 try { 138 auto added = usl->list.push_back_unique(str); 139 return added ? 1 : 0; 140 } catch (std::exception const &) { 141 return -1; 142 } 143} 144 145bool 146u_string_list_contains(struct u_string_list *usl, const char *str) 147{ 148 return usl->list.contains(str); 149} 150 151void 152u_string_list_destroy(struct u_string_list **list_ptr) 153{ 154 if (list_ptr == nullptr) { 155 return; 156 } 157 u_string_list *list = *list_ptr; 158 if (list == nullptr) { 159 return; 160 } 161 delete list; 162 *list_ptr = nullptr; 163}