Convenience macros for defining copy/move ctor and assign
[platform/upstream/libzypp.git] / zypp / base / Easy.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Easy.h
10  *
11 */
12 #ifndef ZYPP_BASE_EASY_H
13 #define ZYPP_BASE_EASY_H
14
15 #include <cstdio>
16
17 /** Convenient for-loops using iterator.
18  * \code
19  *  std::set<std::string>; _store;
20  *  for_( it, _store.begin(), _store.end() )
21  *  {
22  *    cout << *it << endl;
23  *  }
24  * \endcode
25 */
26 #ifndef __GXX_EXPERIMENTAL_CXX0X__
27 #define for_(IT,BEG,END) for ( typeof(BEG) IT = BEG, _for_end = END; IT != _for_end; ++IT )
28 #else
29 #define for_(IT,BEG,END) for ( auto IT = BEG, _for_end = END; IT != _for_end; ++IT )
30 #endif
31 #define for_each_(IT,CONT) for_( IT, CONT.begin(), CONT.end() )
32
33 /** Simple C-array iterator
34  * \code
35  *  const char * defstrings[] = { "",  "a", "default", "two words" };
36  *  for_( it, arrayBegin(defstrings), arrayEnd(defstrings) )
37  *    cout << *it << endl;
38  * \endcode
39 */
40 #define arrayBegin(A) (&A[0])
41 #define arraySize(A)  (sizeof(A)/sizeof(*A))
42 #define arrayEnd(A)   (&A[0] + arraySize(A))
43
44 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100  + __GNUC_PATCHLEVEL__)
45 #if GCC_VERSION < 40600 || not defined(__GXX_EXPERIMENTAL_CXX0X__)
46 #define nullptr NULL
47 #endif
48
49 /** Delete copy ctor and copy assign */
50 #define NON_COPYABLE(CLASS)                     \
51   CLASS( const CLASS & ) = delete;              \
52   CLASS & operator=( const CLASS & ) = delete
53
54 /** Default copy ctor and copy assign */
55 #define DEFAULT_COPYABLE(CLASS)                 \
56   CLASS( const CLASS & ) = default;             \
57   CLASS & operator=( const CLASS & ) = default
58
59 /** Delete move ctor and move assign */
60 #define NON_MOVABLE(CLASS)                      \
61   CLASS( CLASS && ) = delete;                   \
62   CLASS & operator=( CLASS && ) = delete
63
64 /** Default move ctor and move assign */
65 #define DEFAULT_MOVABLE(CLASS)                  \
66   CLASS( CLASS && ) = default;                  \
67   CLASS & operator=( CLASS && ) = default
68
69 /** Delete copy ctor and copy assign but enable default move */
70 #define NON_COPYABLE_BUT_MOVE( CLASS )          \
71   NON_COPYABLE(CLASS);                          \
72   DEFAULT_MOVABLE(CLASS)
73
74 /** Default move ctor and move assign but enable default copy */
75 #define NON_MOVABLE_BUT_COPY( CLASS )           \
76   NON_MOVABLE(CLASS);                           \
77   DEFAULT_COPYABLE(CLASS)
78
79 ///////////////////////////////////////////////////////////////////
80 namespace zypp
81 { /////////////////////////////////////////////////////////////////
82   /////////////////////////////////////////////////////////////////
83 } // namespace zypp
84 ///////////////////////////////////////////////////////////////////
85 #endif // ZYPP_BASE_EASY_H