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