IMP  2.0.0
The Integrative Modeling Platform
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-2013 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 
14 #ifdef IMP_DOXYGEN
15 /** Implement operator[] for C++ and python. The index type is
16  Index and the expression that returns the value is expr. If the
17  bounds_check_expr is false, then a UsageException is thrown
18  in C++ or and IndexException if called from python.
19 */
20 #define IMP_BRACKET(Value, Index, bounds_check_expr, expr) \
21  const Value operator[](Index) const; \
22  Value& operator[](Index);
23 
24 /** Implement operator[] for C++ and python. The index type is
25  Index and the expression that returns the value is expr.
26  The value returned is not mutable. If the
27  bounds_check_expr is false, then a UsageException is thrown
28  in C++ or and IndexException if called from python.
29 */
30 #define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr) \
31  const Value operator[](Index) const;
32 
33 
34 
35 #elif !defined(SWIG)
36 #define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr) \
37  const Value& operator[](Index i) const { \
38  IMP_USAGE_CHECK((bounds_check_expr), "Index out of range: "<< i); \
39  expr; \
40  } \
41  const Value& __getitem__(Index i) const { \
42  if (!(bounds_check_expr)) { \
43  IMP_THROW("Bad index " << i, IMP::base::IndexException); \
44  } \
45  expr; \
46  } \
47 
48 #define IMP_BRACKET(Value, Index, bounds_check_expr, expr) \
49  Value& operator[](Index i) { \
50  IMP_USAGE_CHECK((bounds_check_expr), "Index out of range: "<< i); \
51  expr; \
52  } \
53  void __setitem__(Index i, const Value &v) { \
54  operator[](i)=v; \
55  } \
56  IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr)
57 
58 
59 
60 #else
61 #define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr) \
62  const Value& __getitem__(Index i) const { \
63  if (!(bounds_check_expr)) { \
64  IMP_THROW("Bad index " << i, IMP::base::IndexException); \
65  } \
66  expr; \
67  }
68 
69 #define IMP_BRACKET(Value, Index, bounds_check_expr, expr) \
70  void __setitem__(Index i, const Value &v) { \
71  operator[](i)=v; \
72  } \
73  IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr)
74 
75 #endif
76 
77 
78 #endif /* IMPBASE_BRACKET_MACROS_H */