CodeSonar C++ API
[For improved navigation, enable JavaScript.]
cs_ir_boilerplate.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, an unpublished work by CodeSecure, Inc.
3  * ALL RIGHTS RESERVED
4  *
5  * Copyright (c) 2013-2023, an unpublished work by GrammaTech, Inc.
6  * ALL RIGHTS RESERVED
7  *
8  * This software is furnished under a license and may be used and
9  * copied only in accordance with the terms of such license and the
10  * inclusion of the above copyright notice. This software or any
11  * other copies thereof may not be provided or otherwise made
12  * available to any other person. Title to and ownership of the
13  * software is retained by CodeSecure, Inc.
14  */
15 
16 #ifndef CS_IR_BOILERPLATE_HPP
17 #define CS_IR_BOILERPLATE_HPP
18 
19 #include "cs_cglue.hpp"
20 #include "cs_api_defs.hpp"
21 
24 #define CS_IR_BOILERPLATE_NONMEMBERS(cls, cs) \
25  \
26  \
27  \
28  \
29  \
30  inline std::ostream &operator<<( std::ostream &out, const cls &a ) \
31  { \
32  return out << CS_AS_REPR_PREFIX << #cls " " \
33  << a.as_string() << CS_AS_REPR_SUFFIX; \
34  } \
35  CS_STD_HASH(cls, cs)
36 
37 #define CS_IR_BOILERPLATE_FORWARD(cls, CTYPE, friends) \
38  CS_NODEFCTOR_TYPE(cls) \
39  class cls; \
40  template<> \
41  class cglue<cls > \
42  { \
43  public: \
44  typedef CTYPE ctype; \
45  typedef cls type; \
46  private: \
47  friend class cls; \
48  friend class scratchpad<cls >; \
49  cglue(); \
50  static bool is_null(const ctype &x) \
51  { return !!CTYPE ## _is_null(x); } \
52  static int ccmp(const ctype &a, const ctype &b) \
53  { return CTYPE ## _compare(a, b); } \
54  static cs_hash_t chash(const ctype &a) \
55  { return CTYPE ## _hash(a); } \
56  friends \
57  CS_CXX_API_CGLUE_ACCESS_MODIFIER: \
58  static type wrap(const ctype &c); \
59  static ctype unwrap(const type &c); \
60  }
61 
62 #define CS_IR_BOILERPLATE(cls) \
63  \
64  friend class cglue<cls>; \
65  \
66 public: \
67  \
68  typedef cglue<cls>::ctype ctype; \
69  \
70 private: \
71  cls(); \
72  cglue<cls>::ctype inner; \
73  cls(ctype _inner): inner(_inner) \
74  { \
75  assert( !cglue<cls>::is_null(_inner) ); \
76  } \
77  ctype unwrap() const \
78  { return inner; } \
79 public: \
80  \
81  \
82  \
83  \
84  \
85  \
86  \
87  \
88  \
89  int cmp(const cls& other) const \
90  { return cglue<cls>::ccmp(this->unwrap(), other.unwrap()); } \
91  \
92  \
93  CS_BOILERPLATE_HASH_TYPE hash() const \
94  { return cglue<cls>::chash(this->unwrap()); } \
95  SWIG_BUILD_ONLY( \
96  \
97  \
98  \
99  \
100  \
101  bool operator==(const cls& other) \
102  { return cmp(other) == 0; } \
103  \
104  \
105  \
106  \
107  \
108  \
109  bool operator!=(const cls& b) \
110  { return cmp(b) != 0; } \
111  \
112  \
113  \
114  \
115  \
116  \
117  bool operator<(const cls& b) \
118  { return cmp(b) < 0; } \
119  \
120  \
121  \
122  \
123  \
124  \
125  bool operator<=(const cls& b) \
126  { return cmp(b) <= 0; } \
127  \
128  \
129  \
130  \
131  \
132  \
133  bool operator>(const cls& b) \
134  { return cmp(b) > 0; } \
135  \
136  \
137  \
138  \
139  \
140  \
141  bool operator>=(const cls& b) \
142  { return cmp(b) >= 0; } ) \
143 
144 #define CS_CMP_COMPARATORS(cls) \
145  SWIG_BUILD_ONLY( \
146  bool operator==(const cls& b) \
147  { return cmp(b) == 0; } \
148  \
149  bool operator!=(const cls& b) \
150  { return cmp(b) != 0; } \
151  \
152  bool operator<(const cls& b) \
153  { return cmp(b) < 0; } \
154  \
155  bool operator<=(const cls& b) \
156  { return cmp(b) <= 0; } \
157  \
158  bool operator>(const cls& b) \
159  { return cmp(b) > 0; } \
160  \
161  bool operator>=(const cls& b) \
162  { return cmp(b) >= 0; } ) \
163 
164 #define CS_IR_BOILERPLATE_TYPENAME(cls) \
165  friend class cglue<cls>; \
166 public: \
167  \
168  typedef typename cglue<cls>::ctype ctype; \
169  \
170 private: \
171  cls(); \
172  typename cglue<cls>::ctype inner; \
173  cls(ctype _inner): inner(_inner){} \
174  ctype unwrap() const \
175  { return inner; } \
176 public: \
177  \
178  \
179  \
180  \
181  \
182  \
183  \
184  \
185  \
186  int cmp(const cls& other) const \
187  { return cglue<cls>::ccmp(this->unwrap(), other.unwrap()); } \
188  \
189  \
190  CS_BOILERPLATE_HASH_TYPE hash() const \
191  { return cglue<cls>::chash(this->unwrap()); } \
192  CS_CMP_COMPARATORS(cls)
193 
194 
195 #define CS_IR_BOILERPLATE_FRIENDS(cls) \
196  NOT_SWIG_BUILD_ONLY( \
197  \
198  \
199  \
200  \
201  \
202  \
203  \
204  \
205  inline bool operator==(const cls& a, const cls& b) \
206  { return a.cmp(b) == 0; } \
207  \
208  \
209  \
210  \
211  \
212  \
213  \
214  \
215  \
216  inline bool operator!=(const cls& a, const cls& b) \
217  { return a.cmp(b) != 0; } \
218  \
219  \
220  \
221  \
222  \
223  \
224  inline bool operator<(const cls& a, const cls& b) \
225  { return a.cmp(b) < 0; } \
226  \
227  \
228  \
229  \
230  \
231  \
232  inline bool operator<=(const cls& a, const cls& b) \
233  { return a.cmp(b) <= 0; } \
234  \
235  \
236  \
237  \
238  \
239  \
240  inline bool operator>(const cls& a, const cls& b) \
241  { return a.cmp(b) > 0; } \
242  \
243  \
244  \
245  \
246  \
247  \
248  inline bool operator>=(const cls& a, const cls& b) \
249  { return a.cmp(b) >= 0; } )
250 
251 #define CS_IR_BOILERPLATE_FRIENDS_PLUS_OSTREAM(cls, cs) \
252  CS_IR_BOILERPLATE_FRIENDS(cls) \
253  CS_IR_BOILERPLATE_NONMEMBERS(cls, cs)
254 
255 
256 #endif /* CS_IR_BOILERPLATE_HPP */