monotone

monotone Mtn Source Tree

Root/src/vocab_macros.hh

1// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10//HH
11
12#define hh_ENCODING(enc) \
13 \
14template<typename INNER> \
15class enc; \
16 \
17template <typename INNER> \
18std::ostream & operator<<(std::ostream &, \
19 enc<INNER> const &); \
20 \
21template <typename INNER> \
22void dump(enc<INNER> const &, std::string &); \
23 \
24template<typename INNER> \
25class enc : public origin_aware { \
26 immutable_string s; \
27public: \
28 enc() {} \
29 explicit enc(char const * const s); \
30 enc(std::string const & s, origin::type m); \
31 enc(enc<INNER> const & other); \
32 enc<INNER> const & \
33 operator=(enc<INNER> const & other); \
34 std::string const & operator()() const \
35 { return s.get(); } \
36 bool operator<(enc<INNER> const & x) const \
37 { return s.get() < x.s.get(); } \
38 bool operator==(enc<INNER> const & x) const \
39 { return s.get() == x.s.get(); } \
40 bool operator!=(enc<INNER> const & x) const \
41 { return s.get() != x.s.get(); } \
42 friend std::ostream & operator<< <>(std::ostream &, \
43 enc<INNER> const &); \
44};
45
46#define hh_ENCODING_NOVERIFY(enc) hh_ENCODING(enc)
47
48
49#define hh_DECORATE(dec) \
50 \
51template<typename INNER> \
52class dec; \
53 \
54template <typename INNER> \
55std::ostream & operator<<(std::ostream &, \
56 dec<INNER> const &); \
57 \
58template <typename INNER> \
59void dump(dec<INNER> const &, std::string &); \
60 \
61template<typename INNER> \
62class dec { \
63 INNER i; \
64public: \
65 dec() {} \
66 explicit dec(char const * const s); \
67 dec(std::string const & s, origin::type m); \
68 explicit dec(INNER const & inner); \
69 dec(dec<INNER> const & other); \
70 bool operator<(dec<INNER> const & x) const \
71 { return i < x.i; } \
72 INNER const & inner() const \
73 { return i; } \
74 dec<INNER> const & \
75 operator=(dec<INNER> const & other); \
76 bool operator==(dec<INNER> const & x) const \
77 { return i == x.i; } \
78 bool operator!=(dec<INNER> const & x) const \
79 { return !(i == x.i); } \
80 friend std::ostream & operator<< <>(std::ostream &, \
81 dec<INNER> const &); \
82};
83
84
85#define hh_ATOMIC_HOOKED(ty, hook) \
86class ty; \
87 \
88std::ostream & operator<<(std::ostream &, ty const &); \
89 \
90template <> \
91void dump(ty const &, std::string &); \
92 \
93class ty : public origin_aware { \
94 immutable_string s; \
95public: \
96 ty() {} \
97 explicit ty(char const * const str); \
98 ty(std::string const & str, origin::type m); \
99 ty(ty const & other); \
100 ty const & operator=(ty const & other); \
101 std::string const & operator()() const \
102 { return s.get(); } \
103 bool operator<(ty const & x) const \
104 { return s.get() < x.s.get(); } \
105 bool operator==(ty const & x) const \
106 { return s.get() == x.s.get(); } \
107 bool operator!=(ty const & x) const \
108 { return s.get() != x.s.get(); } \
109 friend std::ostream & operator<<(std::ostream &, \
110 ty const &); \
111 hook \
112 struct symtab \
113 { \
114 symtab(); \
115 ~symtab(); \
116 }; \
117};
118
119#define hh_ATOMIC(ty) hh_ATOMIC_HOOKED(ty,)
120#define hh_ATOMIC_NOVERIFY(ty) hh_ATOMIC(ty)
121#define hh_ATOMIC_BINARY(ty) hh_ATOMIC(ty)
122
123//CC
124
125
126#define cc_ATOMIC(ty) \
127 \
128static symtab_impl ty ## _tab; \
129static size_t ty ## _tab_active = 0; \
130 \
131ty::ty(char const * const str) : \
132 origin_aware(), \
133 s((ty ## _tab_active > 0) \
134 ? (ty ## _tab.unique(str)) \
135 : str) \
136{ verify(*this); } \
137 \
138ty::ty(string const & str, \
139 origin::type m) : \
140 origin_aware(m), \
141 s((ty ## _tab_active > 0) \
142 ? (ty ## _tab.unique(str)) \
143 : str) \
144{ verify(*this); } \
145 \
146ty::ty(ty const & other) : \
147 origin_aware(other), s(other.s) {} \
148 \
149ty const & ty::operator=(ty const & other) \
150{ \
151 s = other.s; \
152 made_from = other.made_from; \
153 return *this; \
154} \
155 \
156std::ostream & operator<<(std::ostream & o, \
157 ty const & a) \
158{ return (o << a.s.get()); } \
159 \
160template <> \
161void dump(ty const & obj, std::string & out) \
162{ out = obj(); } \
163 \
164ty::symtab::symtab() \
165{ ty ## _tab_active++; } \
166 \
167ty::symtab::~symtab() \
168{ \
169 I(ty ## _tab_active > 0); \
170 ty ## _tab_active--; \
171 if (ty ## _tab_active == 0) \
172 ty ## _tab.clear(); \
173}
174
175
176#define cc_ATOMIC_NOVERIFY(ty) \
177inline void verify(ty const &) {} \
178cc_ATOMIC(ty)
179
180
181#define cc_ATOMIC_BINARY(ty) \
182 \
183static symtab_impl ty ## _tab; \
184static size_t ty ## _tab_active = 0; \
185 \
186ty::ty(char const * const str) : \
187 origin_aware(), \
188 s((ty ## _tab_active > 0) \
189 ? (ty ## _tab.unique(str)) \
190 : str) \
191{ verify(*this); } \
192 \
193ty::ty(string const & str, \
194 origin::type m) : \
195 origin_aware(m), \
196 s((ty ## _tab_active > 0) \
197 ? (ty ## _tab.unique(str)) \
198 : str) \
199{ verify(*this); } \
200 \
201ty::ty(ty const & other) : \
202 origin_aware(other), s(other.s) {} \
203 \
204ty const & ty::operator=(ty const & other) \
205{ \
206 s = other.s; \
207 made_from = other.made_from; \
208 return *this; \
209} \
210 \
211std::ostream & operator<<(std::ostream & o, \
212 ty const & a) \
213{ return (o << encode_hexenc(a(), a.made_from)); } \
214 \
215ty::symtab::symtab() \
216{ ty ## _tab_active++; } \
217 \
218ty::symtab::~symtab() \
219{ \
220 I(ty ## _tab_active > 0); \
221 ty ## _tab_active--; \
222 if (ty ## _tab_active == 0) \
223 ty ## _tab.clear(); \
224}
225
226
227
228#define cc_ENCODING(enc) \
229 \
230template<typename INNER> \
231enc<INNER>::enc(char const * const s) : \
232 origin_aware(), s(s) \
233 { verify(*this); } \
234 \
235template<typename INNER> \
236enc<INNER>::enc(string const & s, origin::type m) : \
237 origin_aware(m), s(s) \
238 { verify(*this); } \
239 \
240template<typename INNER> \
241enc<INNER>::enc(enc<INNER> const & other) \
242 : origin_aware(other), s(other.s) {} \
243 \
244template<typename INNER> \
245enc<INNER> const & \
246enc<INNER>::operator=(enc<INNER> const & other) \
247{ \
248 s = other.s; \
249 made_from = other.made_from; \
250 return *this; \
251} \
252 \
253template <typename INNER> \
254std::ostream & operator<<(std::ostream & o, \
255 enc<INNER> const & e) \
256{ return (o << e.s.get()); } \
257 \
258template <typename INNER> \
259void dump(enc<INNER> const & obj, std::string & out) \
260{ out = obj(); }
261
262#define cc_ENCODING_NOVERIFY(enc) \
263template<typename INNER> \
264inline void verify(enc<INNER> const &) {} \
265cc_ENCODING(enc)
266
267
268
269#define cc_DECORATE(dec) \
270 \
271template<typename INNER> \
272dec<INNER>::dec(dec<INNER> const & other) \
273 : i(other.i) {} \
274 \
275template<typename INNER> \
276dec<INNER>::dec(char const * const s) \
277 : i(s) { verify(i); } \
278 \
279template<typename INNER> \
280dec<INNER>::dec(std::string const & s, \
281 origin::type m) \
282 : i(s, m) { verify(i); } \
283 \
284template<typename INNER> \
285dec<INNER>::dec(INNER const & inner) \
286 : i(inner) {} \
287 \
288template<typename INNER> \
289dec<INNER> const & \
290dec<INNER>::operator=(dec<INNER> const & other) \
291 { i = other.i; return *this; } \
292 \
293template <typename INNER> \
294std::ostream & operator<<(std::ostream & o, \
295 dec<INNER> const & d) \
296{ return (o << d.i); } \
297 \
298template <typename INNER> \
299void dump(dec<INNER> const & obj, std::string & out) \
300{ dump(obj.inner(), out); }
301
302// Local Variables:
303// mode: C++
304// fill-column: 76
305// c-file-style: "gnu"
306// indent-tabs-mode: nil
307// End:
308// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status