IMP  2.2.0
The Integrative Modeling Platform
base/bracket_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/bracket_macros.h
3  * \brief Various general useful macros for IMP.
4  *
5  * Copyright 2007-2014 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_BRACKET_MACROS_H
10 #define IMPBASE_BRACKET_MACROS_H
11 #include <IMP/base/base_config.h>
12 
13 #ifdef IMP_DOXYGEN
14 /** Implement operator[] for C++ and python. The index type is
15  Index and the expression that returns the value is expr. If the
16  bounds_check_expr is false, then a UsageException is thrown
17  in C++ or and IndexException if called from python.
18 */
19 #define IMP_BRACKET(Value, Index, bounds_check_expr, expr) \
20  const Value operator[](Index) const; \
21  Value& operator[](Index);
22 
23 /** Implement operator[] for C++ and python. The index type is
24  Index and the expression that returns the value is expr.
25  The value returned is not mutable. If the
26  bounds_check_expr is false, then a UsageException is thrown
27  in C++ or and IndexException if called from python.
28 */
29 #define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr) \
30  const Value operator[](Index) const;
31 
32 #elif !defined(SWIG)
33 #define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr) \
34  const Value& operator[](Index i) const { \
35  IMP_USAGE_CHECK((bounds_check_expr), "Index out of range: " << i); \
36  expr; \
37  } \
38  const Value& __getitem__(Index i) const { \
39  if (!(bounds_check_expr)) { \
40  IMP_THROW("Bad index " << i, IMP::base::IndexException); \
41  } \
42  expr; \
43  }
44 
45 #define IMP_BRACKET(Value, Index, bounds_check_expr, expr) \
46  Value& operator[](Index i) { \
47  IMP_USAGE_CHECK((bounds_check_expr), "Index out of range: " << i); \
48  expr; \
49  } \
50  void __setitem__(Index i, const Value& v) { operator[](i) = v; } \
51  IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr)
52 
53 #else
54 #define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr) \
55  const Value& __getitem__(Index i) const { \
56  if (!(bounds_check_expr)) { \
57  IMP_THROW("Bad index " << i, IMP::base::IndexException); \
58  } \
59  expr; \
60  }
61 
62 #define IMP_BRACKET(Value, Index, bounds_check_expr, expr) \
63  void __setitem__(Index i, const Value& v) { operator[](i) = v; } \
64  IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr)
65 
66 #endif
67 
68 #endif /* IMPBASE_BRACKET_MACROS_H */