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