Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/FaissAssert.h
1 /**
2  * Copyright (c) Facebook, Inc. and its 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 #ifndef FAISS_ASSERT_INCLUDED
11 #define FAISS_ASSERT_INCLUDED
12 
13 #include "FaissException.h"
14 #include <cstdlib>
15 #include <cstdio>
16 #include <string>
17 
18 ///
19 /// Assertions
20 ///
21 
22 #define FAISS_ASSERT(X) \
23  do { \
24  if (! (X)) { \
25  fprintf(stderr, "Faiss assertion '%s' failed in %s " \
26  "at %s:%d\n", \
27  #X, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
28  abort(); \
29  } \
30  } while (false)
31 
32 #define FAISS_ASSERT_MSG(X, MSG) \
33  do { \
34  if (! (X)) { \
35  fprintf(stderr, "Faiss assertion '%s' failed in %s " \
36  "at %s:%d; details: " MSG "\n", \
37  #X, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
38  abort(); \
39  } \
40  } while (false)
41 
42 #define FAISS_ASSERT_FMT(X, FMT, ...) \
43  do { \
44  if (! (X)) { \
45  fprintf(stderr, "Faiss assertion '%s' failed in %s " \
46  "at %s:%d; details: " FMT "\n", \
47  #X, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
48  abort(); \
49  } \
50  } while (false)
51 
52 ///
53 /// Exceptions for returning user errors
54 ///
55 
56 #define FAISS_THROW_MSG(MSG) \
57  do { \
58  throw faiss::FaissException(MSG, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
59  } while (false)
60 
61 #define FAISS_THROW_FMT(FMT, ...) \
62  do { \
63  std::string __s; \
64  int __size = snprintf(nullptr, 0, FMT, __VA_ARGS__); \
65  __s.resize(__size + 1); \
66  snprintf(&__s[0], __s.size(), FMT, __VA_ARGS__); \
67  throw faiss::FaissException(__s, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
68  } while (false)
69 
70 ///
71 /// Exceptions thrown upon a conditional failure
72 ///
73 
74 #define FAISS_THROW_IF_NOT(X) \
75  do { \
76  if (!(X)) { \
77  FAISS_THROW_FMT("Error: '%s' failed", #X); \
78  } \
79  } while (false)
80 
81 #define FAISS_THROW_IF_NOT_MSG(X, MSG) \
82  do { \
83  if (!(X)) { \
84  FAISS_THROW_FMT("Error: '%s' failed: " MSG, #X); \
85  } \
86  } while (false)
87 
88 #define FAISS_THROW_IF_NOT_FMT(X, FMT, ...) \
89  do { \
90  if (!(X)) { \
91  FAISS_THROW_FMT("Error: '%s' failed: " FMT, #X, __VA_ARGS__); \
92  } \
93  } while (false)
94 
95 #endif