fixup Fix to build with libxml 2.12.x (fixes #505)
[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 #include <type_traits>
17
18 /** Convenient for-loops using iterator.
19  * \code
20  *  std::set<std::string>; _store;
21  *  for_( it, _store.begin(), _store.end() )
22  *  {
23  *    cout << *it << endl;
24  *  }
25  * \endcode
26 */
27 #ifndef __GXX_EXPERIMENTAL_CXX0X__
28 #define for_(IT,BEG,END) for ( __typeof__(BEG) IT = BEG, _for_end = END; IT != _for_end; ++IT )
29 #else
30 #define for_(IT,BEG,END) for ( auto IT = BEG, _for_end = END; IT != _for_end; ++IT )
31 #endif
32 #define for_each_(IT,CONT) for_( IT, (CONT).begin(), (CONT).end() )
33
34 /** Simple C-array iterator
35  * \code
36  *  const char * defstrings[] = { "",  "a", "default", "two words" };
37  *  for_( it, arrayBegin(defstrings), arrayEnd(defstrings) )
38  *    cout << *it << endl;
39  * \endcode
40 */
41 #define arrayBegin(A) (&A[0])
42 #define arraySize(A)  (sizeof(A)/sizeof(*A))
43 #define arrayEnd(A)   (&A[0] + arraySize(A))
44
45 /**
46  * \code
47  * defConstStr( strANY(), "ANY" );
48  * std::str str = strANY();
49  * \endcode
50  */
51 #define defConstStr(FNC,STR) inline const std::string & FNC { static const std::string val( STR ); return val; }
52
53 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100  + __GNUC_PATCHLEVEL__)
54 #if GCC_VERSION < 40600 || not defined(__GXX_EXPERIMENTAL_CXX0X__)
55 #define nullptr NULL
56 #endif
57
58 /** Delete copy ctor and copy assign */
59 #define NON_COPYABLE(CLASS)                     \
60   CLASS( const CLASS & ) = delete;              \
61   CLASS & operator=( const CLASS & ) = delete
62
63 /** Default copy ctor and copy assign */
64 #define DEFAULT_COPYABLE(CLASS)                 \
65   CLASS( const CLASS & ) = default;             \
66   CLASS & operator=( const CLASS & ) = default
67
68 #ifndef SWIG // Swig treats it as syntax error
69 /** Delete move ctor and move assign */
70 #define NON_MOVABLE(CLASS)                      \
71   CLASS( CLASS && ) = delete;                   \
72   CLASS & operator=( CLASS && ) = delete
73
74 /** Default move ctor and move assign */
75 #define DEFAULT_MOVABLE(CLASS)                  \
76   CLASS( CLASS && ) = default;                  \
77   CLASS & operator=( CLASS && ) = default
78 #else
79 #define NON_MOVABLE(CLASS)
80 #define DEFAULT_MOVABLE(CLASS)
81 #endif
82
83 /** Delete copy ctor and copy assign but enable default move */
84 #define NON_COPYABLE_BUT_MOVE( CLASS )          \
85   NON_COPYABLE(CLASS);                          \
86   DEFAULT_MOVABLE(CLASS)
87
88 /** Default move ctor and move assign but enable default copy */
89 #define NON_MOVABLE_BUT_COPY( CLASS )           \
90   NON_MOVABLE(CLASS);                           \
91   DEFAULT_COPYABLE(CLASS)
92
93
94 /** Prevent an universal ctor to be chosen as copy ctor.
95  * \code
96  *  struct FeedStrings
97  *  {
98  *    template<typename TARG, typename X = disable_use_as_copy_ctor<FeedStrings,TARG>>
99  *    FeedStrings( TARG && arg_r )
100  *    : _value { std::forward<TARG>( arg_r ) }
101  *    {}
102  *
103  *    // Same with variadic template. Could be chosen as copy_ctor.
104  *    template<typename ... Us>
105  *    FeedStrings( Us &&... us )
106  *    : ...
107  *
108  *  private:
109  *    std::string _value;
110  * \endcode
111  */
112 template<typename TBase, typename TDerived>
113 using disable_use_as_copy_ctor = typename std::enable_if<!std::is_base_of<TBase,typename std::remove_reference<TDerived>::type>::value>::type;
114
115 ///////////////////////////////////////////////////////////////////
116 namespace zypp
117 { /////////////////////////////////////////////////////////////////
118   /////////////////////////////////////////////////////////////////
119 } // namespace zypp
120 ///////////////////////////////////////////////////////////////////
121 #endif // ZYPP_BASE_EASY_H