Debian Codegraph @

c_api/macros_impl.h

raw
1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 // -*- c++ -*-
9
10 /// Utility macros for the C wrapper implementation.
11
12 #ifndef MACROS_IMPL_H
13 #define MACROS_IMPL_H
14
15 #include <faiss/impl/FaissException.h>
16 #include <iostream>
17 #include <stdexcept>
18 #include "error_impl.h"
19 #include "faiss_c.h"
20
21 #ifdef NDEBUG
22 #define CATCH_AND_HANDLE \
23 catch (faiss::FaissException & e) { \
24 faiss_last_exception = std::make_exception_ptr(e); \
25 return -2; \
26 } \
27 catch (std::exception & e) { \
28 faiss_last_exception = std::make_exception_ptr(e); \
29 return -4; \
30 } \
31 catch (...) { \
32 faiss_last_exception = \
33 std::make_exception_ptr(std::runtime_error("Unknown error")); \
34 return -1; \
35 } \
36 return 0;
37 #else
38 #define CATCH_AND_HANDLE \
39 catch (faiss::FaissException & e) { \
40 std::cerr << e.what() << '\n'; \
41 faiss_last_exception = std::make_exception_ptr(e); \
42 return -2; \
43 } \
44 catch (std::exception & e) { \
45 std::cerr << e.what() << '\n'; \
46 faiss_last_exception = std::make_exception_ptr(e); \
47 return -4; \
48 } \
49 catch (...) { \
50 std::cerr << "Unrecognized exception!\n"; \
51 faiss_last_exception = \
52 std::make_exception_ptr(std::runtime_error("Unknown error")); \
53 return -1; \
54 } \
55 return 0;
56 #endif
57
58 #define DEFINE_GETTER(clazz, ty, name) \
59 ty faiss_##clazz##_##name(const Faiss##clazz* obj) { \
60 return static_cast<ty>( \
61 reinterpret_cast<const faiss::clazz*>(obj)->name); \
62 }
63
64 #define DEFINE_GETTER_SUBCLASS(clazz, parent, ty, name) \
65 ty faiss_##clazz##_##name(const Faiss##clazz* obj) { \
66 return static_cast<ty>( \
67 reinterpret_cast<const faiss::parent::clazz*>(obj)->name); \
68 }
69
70 #define DEFINE_GETTER_PERMISSIVE(clazz, ty, name) \
71 ty faiss_##clazz##_##name(const Faiss##clazz* obj) { \
72 return (ty)(reinterpret_cast<const faiss::clazz*>(obj)->name); \
73 }
74
75 #define DEFINE_GETTER_SUBCLASS_PERMISSIVE(clazz, parent, ty, name) \
76 ty faiss_##clazz##_##name(const Faiss##clazz* obj) { \
77 return (ty)(reinterpret_cast<const faiss::parent::clazz*>(obj)->name); \
78 }
79
80 #define DEFINE_SETTER(clazz, ty, name) \
81 void faiss_##clazz##_set_##name(Faiss##clazz* obj, ty val) { \
82 reinterpret_cast<faiss::clazz*>(obj)->name = val; \
83 }
84
85 #define DEFINE_SETTER_STATIC(clazz, ty_to, ty_from, name) \
86 void faiss_##clazz##_set_##name(Faiss##clazz* obj, ty_from val) { \
87 reinterpret_cast<faiss::clazz*>(obj)->name = static_cast<ty_to>(val); \
88 }
89
90 #define DEFINE_DESTRUCTOR(clazz) \
91 void faiss_##clazz##_free(Faiss##clazz* obj) { \
92 delete reinterpret_cast<faiss::clazz*>(obj); \
93 }
94
95 #define DEFINE_INDEX_DOWNCAST(clazz) \
96 Faiss##clazz* faiss_##clazz##_cast(FaissIndex* index) { \
97 return reinterpret_cast<Faiss##clazz*>(dynamic_cast<faiss::clazz*>( \
98 reinterpret_cast<faiss::Index*>(index))); \
99 }
100
101 #define DEFINE_INDEX_BINARY_DOWNCAST(clazz) \
102 Faiss##clazz* faiss_##clazz##_cast(FaissIndexBinary* index) { \
103 return reinterpret_cast<Faiss##clazz*>(dynamic_cast<faiss::clazz*>( \
104 reinterpret_cast<faiss::IndexBinary*>(index))); \
105 }
106
107 #define DEFINE_SEARCH_PARAMETERS_DOWNCAST(clazz) \
108 Faiss##clazz* faiss_##clazz##_cast(FaissSearchParameters* sp) { \
109 return reinterpret_cast<Faiss##clazz*>(dynamic_cast<faiss::clazz*>( \
110 reinterpret_cast<faiss::SearchParameters*>(sp))); \
111 }
112
113 #endif