YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ybasemac.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2010 - 2013.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YSL_INC_Adaptor_ybasemac_h_
29 #define YSL_INC_Adaptor_ybasemac_h_ 1
30 
32 
33 /*
34 \def yJOIN
35 \brief 记号连接。
36  ISO/IEC C++ 未确定宏定义内 # 和 ## 操作符求值顺序。
37  GCC 中,宏定义内 ## 操作符修饰的形式参数为宏时,此宏不会被展开。
38 详见:http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html 。
39 解决方案来源:
40  https://www.securecoding.cert.org/confluence/display/cplusplus/ \
41  PRE05-CPP.+Understand+macro+replacement+when+concatenating \
42  +tokens+or+performing+stringification 。
43 */
44 #define yJOIN(x, y) x ## y
45 
46 
47 // YSLib 命名空间宏。
48 
49 #define YSL_BEGIN namespace YSLib {
50 #define YSL_END }
51 
52 #define YSL_BEGIN_NAMESPACE(_n) namespace _n {
53 #define YSL_END_NAMESPACE(_n) }
54 
55 
56 /* \defgroup cmacro Macros For Code Compressing
57 \brief 缩写代码用的宏。
58 
59 以下名构词缩写的含义:
60 Ctor constructor
61 Cvt converter
62 De default
63 Decl declare
64 Def define
65 Del deleted
66 Dtor destructor
67 Expr expression
68 Fn function
69 Fwd forward
70 H head
71 I interface
72 Impl implement
73 Mem member
74 Op operator
75 P partially
76 PDecl pre-declare
77 Pred predicate
78 Ret returning
79 S statically
80 Tmpl template
81 
82 以下宏参数缩写的含义:
83 _a argument
84 _alist arguments list
85 _attr attributes
86 _b base
87 _e expression
88 _i interface
89 _m member
90 _n name
91 _op operator
92 _p parameter
93 _plist parameters list
94 _q qualifier(s)
95 _sig signature
96 _t type
97 \since 早于 build 132
98 */
100 //接口类型宏。
101 
102 #define _yInterface struct
103 
104 #define implements public
105 
111 #define _yInterfaceHead(_n) { \
112  virtual ~_n() {}
113 
114 #define FwdDeclI(_n) _yInterface _n;
115 
116 /*
117 \def DeclI
118 \brief 定义接口类型。
119 \since build 362
120 */
121 #define DeclI(_attr, _n) \
122  _yInterface _attr _n \
123  _yInterfaceHead(_n)
124 
125 //对于基接口需要显式指定访问权限和继承方式。
126 
127 /*
128 \def DeclDerivedI
129 \brief 定义派生接口类型。
130 \note 由于接口定义为 struct 类型,因此通常只需指定是否为 virtual 继承。
131 \since build 362
132 */
133 #define DeclDerivedI(_attr, _n, ...) \
134  _yInterface _attr _n : __VA_ARGS__ \
135  _yInterfaceHead(_n)
136 
137 // ImplI = Implements Interface;
138 #define ImplI(...) virtual
139 
140 //抽象实现:保留接口供派生类实现(可以提供接口函数的默认实现)。
141 // ImplA = Implements Abstractly;
142 #define ImplA(...)
143 
144 #define DeclIEntry(_sig) virtual _sig = 0;
145 
146 #define EndDecl };
147 
148 
153 #define DeclSEntry(...)
154 
158 #define ImplS(...)
159 
160 
166 #define DefExtendClass(_attr, _n, ...) \
167  class _attr _n : __VA_ARGS__ \
168  { \
169  public: \
170  _n(); \
171  };
172 
173 
174 //函数宏。
175 
176 //通用头定义。
177 #define PDefH(_t, _n, ...) \
178  _t _n(__VA_ARGS__)
179 #define PDefHOp(_t, _op, ...) \
180  PDefH(_t, operator _op, __VA_ARGS__)
181 
182 #define PDefCvt(_t) \
183  operator _t()
184 
185 
186 //简单通用函数实现。
187 //prefix "Impl" = Implementation;
188 #define ImplExpr(...) \
189  { \
190  (__VA_ARGS__), void(); \
191  }
192 #define ImplRet(...) \
193  { \
194  return (__VA_ARGS__); \
195  }
196 // NOTE: Need 'ydef.h'.
197 // NOTE: GCC complains about 'void(yunseq(__VA_ARGS__))'.
198 #define ImplUnseq(...) \
199  { \
200  static_cast<void>(yunseq(__VA_ARGS__)); \
201  }
202 
203 //基类同名函数映射和成员同名函数映射实现。
204 //prefix "Impl" = Implement;
205 #define ImplBodyBase(_b, _n, ...) \
206  ImplRet(_b::_n(__VA_ARGS__))
207 #define ImplBodyMem(_m, _n, ...) \
208  ImplRet((_m)._n(__VA_ARGS__))
209 
210 
211 //简单通用成员函数定义。
212 //prefix "Def" = Define;
220 #define DefEmptyDtor(_t) \
221  ~_t() \
222  {}
223 #define ImplEmptyDtor(_t) \
224  inline _t::DefEmptyDtor(_t)
225 
226 #define DefDeCtor(_t) \
227  _t() = default;
228 #define DefDelCtor(_t) \
229  _t() = delete;
230 
231 #define DefDeCopyCtor(_t) \
232  _t(const _t&) = default;
233 #define DefDelCopyCtor(_t) \
234  _t(const _t&) = delete;
235 
236 #define DefDeMoveCtor(_t) \
237  _t(_t&&) = default;
238 #define DefDelMoveCtor(_t) \
239  _t(_t&&) = delete;
240 
241 #define DefDeDtor(_t) \
242  ~_t() = default;
243 #define DefDelDtor(_t) \
244  ~_t() = delete;
245 
246 #define DefDeCopyAssignment(_t) \
247  _t& operator=(const _t&) = default;
248 #define DefDelCopyAssignment(_t) \
249  _t& operator=(const _t&) = delete;
250 
251 #define DefDeMoveAssignment(_t) \
252  _t& operator=(_t&&) = default;
253 #define DefDelMoveAssignment(_t) \
254  _t& operator=(_t&&) = delete;
255 
256 #define DefCvt(_q, _t, ...) \
257  operator _t() _q \
258  ImplRet(__VA_ARGS__)
259 #define DefCvtBase(_q, _t, _b) \
260  DefCvt(_q, _t, _b::operator _t())
261 #define DefCvtMem(_q, _t, _m) \
262  DefCvt(_q, _t, (_m).operator _t())
263 
264 #define DefPred(_q, _n, ...) \
265  bool yJOIN(Is, _n)() _q \
266  ImplRet(__VA_ARGS__)
267 #define DefPredBase(_q, _n, _b) \
268  DefPred(_q, _n, _b::yJOIN(Is, _n)())
269 #define DefPredMem(_q, _n, _m) \
270  DefPred(_q, _n, (_m).yJOIN(Is, _n)())
271 
272 #define DefGetter(_q, _t, _n, ...) \
273  _t yJOIN(Get, _n)() _q \
274  ImplRet(__VA_ARGS__)
275 #define DefGetterBase(_q, _t, _n, _b) \
276  DefGetter(_q, _t, _n, _b::yJOIN(Get, _n)())
277 #define DefGetterMem(_q, _t, _n, _m) \
278  DefGetter(_q, _t, _n, (_m).yJOIN(Get, _n)())
279 
280 #define DefSetter(_t, _n, _m) \
281  void yJOIN(Set, _n)(_t _tempArgName) \
282  ImplExpr((_m) = _tempArgName)
283 #define DefSetterDe(_t, _n, _m, _defv) \
284  void yJOIN(Set, _n)(_t _tempArgName = _defv) \
285  ImplExpr((_m) = _tempArgName)
286 #define DefSetterBase(_t, _n, _b) \
287  void yJOIN(Set, _n)(_t _tempArgName) \
288  ImplExpr(_b::yJOIN(Set, _n)(_tempArgName))
289 #define DefSetterBaseDe(_t, _n, _b, _defv) \
290  void yJOIN(Set, _n)(_t _tempArgName = _defv) \
291  ImplExpr(_b::yJOIN(Set, _n)(_tempArgName))
292 #define DefSetterMem(_t, _n, _m) \
293  void yJOIN(Set, _n)(_t _tempArgName) \
294  ImplExpr((_m).yJOIN(Set, _n)(_tempArgName))
295 #define DefSetterMemDe(_t, _n, _m, _defv) \
296  void yJOIN(Set, _n)(_t _tempArgName = _defv) \
297  ImplExpr((_m).yJOIN(Set, _n)(_tempArgName))
298 #define DefSetterEx(_t, _n, _m, ...) \
299  void yJOIN(Set, _n)(_t _tempArgName) \
300  ImplExpr((_m) = (__VA_ARGS__))
301 #define DefSetterDeEx(_t, _n, _m, _defv, ...) \
302  void yJOIN(Set, _n)(_t _tempArgName = _defv) \
303  ImplExpr((_m) = (__VA_ARGS__))
304 
305 
313 #define DefClone(_q, _t, _n) \
314  _t* \
315  _n() _q \
316  { \
317  return new _t(*this); \
318  }
319 
320 
321 //成员函数和模板映射。
322 
323 
328 #define DefFwdFn(_q, _t, _n, ...) \
329  inline _t \
330  _n() _q \
331  { \
332  return (__VA_ARGS__); \
333  }
334 
339 #define DefFwdTmpl(_q, _t, _n, ...) \
340  template<typename... _tParams> \
341  inline _t \
342  _n(_tParams&&... args) _q \
343  { \
344  return (__VA_ARGS__); \
345  }
346 
347 
354 #define DefBitmaskAnd(_tBitmask, _tInt) \
355  yconstfn _tBitmask operator&(_tBitmask _x, _tBitmask _y) \
356  ImplRet(static_cast<_tBitmask>( \
357  static_cast<_tInt>(_x) & static_cast<_tInt>(_y)))
358 
359 #define DefBitmaskOr(_tBitmask, _tInt) \
360  yconstfn _tBitmask operator|(_tBitmask _x, _tBitmask _y) \
361  ImplRet(static_cast<_tBitmask>( \
362  static_cast<_tInt>(_x) | static_cast<_tInt>(_y)))
363 
364 #define DefBitmaskXor(_tBitmask, _tInt) \
365  yconstfn _tBitmask operator^(_tBitmask _x, _tBitmask _y) \
366  ImplRet(static_cast<_tBitmask>( \
367  static_cast<_tInt>(_x) ^ static_cast<_tInt>(_y)))
368 
369 #define DefBitmaskNot(_tBitmask, _tInt) \
370  yconstfn _tBitmask operator~(_tBitmask _x) \
371  ImplRet(static_cast<_tBitmask>(~static_cast<_tInt>(_x)))
372 
373 #define DefBitmaskAndAssignment(_tBitmask, _tInt) \
374  inline _tBitmask& operator&=(_tBitmask& _x, _tBitmask _y) \
375  ImplRet(_x = _x & _y)
376 
377 #define DefBitmaskOrAssignment(_tBitmask, _tInt) \
378  inline _tBitmask& operator|=(_tBitmask& _x, _tBitmask _y) \
379  ImplRet(_x = _x | _y)
380 
381 #define DefBitmaskXorAssignment(_tBitmask, _tInt) \
382  inline _tBitmask& operator^=(_tBitmask& _x, _tBitmask _y) \
383  ImplRet(_x = _x ^ _y)
384 
385 #define DefBitmaskOperations(_tBitmask, _tInt) \
386  DefBitmaskAnd(_tBitmask, _tInt) \
387  DefBitmaskOr(_tBitmask, _tInt) \
388  DefBitmaskXor(_tBitmask, _tInt) \
389  DefBitmaskNot(_tBitmask, _tInt) \
390  DefBitmaskAndAssignment(_tBitmask, _tInt) \
391  DefBitmaskOrAssignment(_tBitmask, _tInt) \
392  DefBitmaskXorAssignment(_tBitmask, _tInt)
393 
394 
396 
397 #endif
398