-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusearch_wrapper.cpp
More file actions
164 lines (141 loc) · 4.63 KB
/
Copy pathusearch_wrapper.cpp
File metadata and controls
164 lines (141 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Wrapper that catches C++ exceptions and converts to C error strings
// This prevents exceptions from escaping into the Bare runtime (compiled with -fno-exceptions)
#include <cstring>
#include <cstdlib>
#include <exception>
extern "C" {
#include "usearch.h"
}
// Thread-local buffer for error messages
static thread_local char error_buffer[256];
static const char* capture_exception() {
try {
throw; // rethrow current exception
} catch (const std::exception& e) {
std::strncpy(error_buffer, e.what(), sizeof(error_buffer) - 1);
error_buffer[sizeof(error_buffer) - 1] = '\0';
return error_buffer;
} catch (...) {
return "Unknown C++ exception";
}
}
// Wrapped versions of usearch functions that catch exceptions
extern "C" {
USEARCH_EXPORT usearch_index_t usearch_init_safe(usearch_init_options_t* options, usearch_error_t* error) {
try {
return usearch_init(options, error);
} catch (...) {
*error = capture_exception();
return nullptr;
}
}
USEARCH_EXPORT void usearch_free_safe(usearch_index_t index, usearch_error_t* error) {
try {
usearch_free(index, error);
} catch (...) {
*error = capture_exception();
}
}
USEARCH_EXPORT size_t usearch_size_safe(usearch_index_t index, usearch_error_t* error) {
try {
return usearch_size(index, error);
} catch (...) {
*error = capture_exception();
return 0;
}
}
USEARCH_EXPORT size_t usearch_capacity_safe(usearch_index_t index, usearch_error_t* error) {
try {
return usearch_capacity(index, error);
} catch (...) {
*error = capture_exception();
return 0;
}
}
USEARCH_EXPORT size_t usearch_dimensions_safe(usearch_index_t index, usearch_error_t* error) {
try {
return usearch_dimensions(index, error);
} catch (...) {
*error = capture_exception();
return 0;
}
}
USEARCH_EXPORT void usearch_reserve_safe(usearch_index_t index, size_t capacity, usearch_error_t* error) {
try {
usearch_reserve(index, capacity, error);
} catch (...) {
*error = capture_exception();
}
}
USEARCH_EXPORT void usearch_add_safe(usearch_index_t index, usearch_key_t key, void const* vector,
usearch_scalar_kind_t kind, usearch_error_t* error) {
try {
usearch_add(index, key, vector, kind, error);
} catch (...) {
*error = capture_exception();
}
}
USEARCH_EXPORT size_t usearch_search_safe(usearch_index_t index, void const* query, usearch_scalar_kind_t kind,
size_t count, usearch_key_t* keys, usearch_distance_t* distances,
usearch_error_t* error) {
try {
return usearch_search(index, query, kind, count, keys, distances, error);
} catch (...) {
*error = capture_exception();
return 0;
}
}
USEARCH_EXPORT size_t usearch_remove_safe(usearch_index_t index, usearch_key_t key, usearch_error_t* error) {
try {
return usearch_remove(index, key, error);
} catch (...) {
*error = capture_exception();
return 0;
}
}
USEARCH_EXPORT bool usearch_contains_safe(usearch_index_t index, usearch_key_t key, usearch_error_t* error) {
try {
return usearch_contains(index, key, error);
} catch (...) {
*error = capture_exception();
return false;
}
}
USEARCH_EXPORT size_t usearch_get_safe(usearch_index_t index, usearch_key_t key, size_t count,
void* vector, usearch_scalar_kind_t kind, usearch_error_t* error) {
try {
return usearch_get(index, key, count, vector, kind, error);
} catch (...) {
*error = capture_exception();
return 0;
}
}
USEARCH_EXPORT void usearch_save_safe(usearch_index_t index, char const* path, usearch_error_t* error) {
try {
usearch_save(index, path, error);
} catch (...) {
*error = capture_exception();
}
}
USEARCH_EXPORT void usearch_load_safe(usearch_index_t index, char const* path, usearch_error_t* error) {
try {
usearch_load(index, path, error);
} catch (...) {
*error = capture_exception();
}
}
USEARCH_EXPORT void usearch_view_safe(usearch_index_t index, char const* path, usearch_error_t* error) {
try {
usearch_view(index, path, error);
} catch (...) {
*error = capture_exception();
}
}
USEARCH_EXPORT void usearch_clear_safe(usearch_index_t index, usearch_error_t* error) {
try {
usearch_clear(index, error);
} catch (...) {
*error = capture_exception();
}
}
} // extern "C"