Imported Upstream version 17.2.1
[platform/upstream/libzypp.git] / devel / genclass.in
1 #! /bin/bash
2
3 function usage() {
4    echo $@ >&2
5    echo <<EOF >&2
6 Usage: genclass [path/]stem
7 EOF
8    exit 1
9 }
10
11 test -z "$1" && usage "Missing name!"
12
13 TOPSRCDIR=${XTOPSRCDIR:-$(cd @CMAKE_SOURCE_DIR@ && pwd)}
14 test -z "$TOPSRCDIR" && {
15    echo "Dir does not exist '$TOPSRCDIR'" >&2
16    exit 1
17 }
18
19 OUTDIR=$(dirname $1)
20 STEM=$(basename $1)
21 STEMDIR=$( cd $OUTDIR && pwd )
22 test -z "$STEMDIR" && {
23    echo "Dir does not exist '$(dirname $1)'" >&2
24    exit 1
25 }
26 STEMDIR=${STEMDIR#$TOPSRCDIR/}
27
28 CLASS=$STEM
29 CLASS_H=$STEMDIR/$STEM.h
30 CLASS_CC=$STEMDIR/$STEM.cc
31
32 OUT_CLASS_H=$OUTDIR/$STEM.h
33 OUT_CLASS_CC=$OUTDIR/$STEM.cc
34 test -e $OUT_CLASS_H -o -e $OUT_CLASS_CC && {
35    test -e $OUT_CLASS_H && echo "File exists '$OUT_CLASS_H' using '$OUT_CLASS_H.new'" >&2
36    test -e $OUT_CLASS_CC && echo "File exists '$OUT_CLASS_CC' using '$OUT_CLASS_CC.new'" >&2
37    OUT_CLASS_H="$OUT_CLASS_H.new"
38    OUT_CLASS_CC="$OUT_CLASS_CC.new"
39 }
40
41 INCLUDE_H=$CLASS_H
42 INCLUDE_DEF=$(echo $INCLUDE_H | sed 's/[./]/_/g' | awk '{print toupper($0)}')
43 NSLIST=$(echo $(dirname $INCLUDE_H) | awk '{l=tolower($0);gsub("/"," ",l);print l}')
44 SNLIST=
45 INDENT=
46 for N in $NSLIST; do
47    SNLIST="$N $SNLIST"
48    INDENT="$INDENT  "
49 done
50
51 ######################################################################
52 function intro() {
53 ######################################################################
54    local FILE=$1
55 cat <<EOF
56 /*---------------------------------------------------------------------\\
57 |                          ____ _   __ __ ___                          |
58 |                         |__  / \ / / . \ . \                         |
59 |                           / / \ V /|  _/  _/                         |
60 |                          / /__ | | | | | |                           |
61 |                         /_____||_| |_| |_|                           |
62 |                                                                      |
63 \---------------------------------------------------------------------*/
64 /** \file       ${FILE}
65  */
66 EOF
67 }
68
69 ######################################################################
70 function nsopen() {
71 ######################################################################
72    local I=
73    for N in $NSLIST; do
74       echo "${I}///////////////////////////////////////////////////////////////////"
75       echo "${I}namespace $N"
76       echo "${I}{"
77       I="$I  "
78    done
79 }
80
81 ######################################################################
82 function nsclose() {
83 ######################################################################
84    local I=${INDENT}
85    for N in $SNLIST; do
86       I=${I#  }
87       echo "${I}} // namespace $N"
88       echo "${I}///////////////////////////////////////////////////////////////////"
89    done
90 }
91
92 ######################################################################
93 function genH() {
94 ######################################################################
95 cat <<EOF
96 $(intro $CLASS_H)
97 #ifndef $INCLUDE_DEF
98 #define $INCLUDE_DEF
99
100 #include <iosfwd>
101
102 #include "zypp/base/PtrTypes.h"
103
104 $(nsopen)
105 ${INDENT}///////////////////////////////////////////////////////////////////
106 ${INDENT}/// \class ${CLASS}
107 ${INDENT}/// \brief
108 ${INDENT}///////////////////////////////////////////////////////////////////
109 ${INDENT}class ${CLASS}
110 ${INDENT}{
111 ${INDENT}  friend std::ostream & operator<<( std::ostream & str, const ${CLASS} & obj );
112 ${INDENT}  friend std::ostream & dumpOn( std::ostream & str, const ${CLASS} & obj );
113 ${INDENT}  friend bool operator==( const ${CLASS} & lhs, const ${CLASS} & rhs );
114
115 ${INDENT}  public:
116 ${INDENT}    /** Default ctor */
117 ${INDENT}    ${CLASS}();
118
119 ${INDENT}    /** Dtor */
120 ${INDENT}    ~${CLASS}();
121
122 ${INDENT}  public:
123 ${INDENT}    /**  Validate object in a boolean context. */
124 ${INDENT}    explicit operator bool() const
125 ${INDENT}    {
126 ${INDENT}      /* !!! Perform Boolean logic here AND check implememtation of operator==!!!
127 ${INDENT}       * NOTE: SafeBool requires operator== otherwise equality is reduced to
128 ${INDENT}       *       ( bool(${CLASS}) == bool(${CLASS}) ).
129 ${INDENT}       */
130 ${INDENT}    }
131 ${INDENT}  public:
132 ${INDENT}    class Impl;                 ///< Implementation class.
133 ${INDENT}  private:
134 ${INDENT}    RWCOW_pointer<Impl> _pimpl; ///< Pointer to implementation.
135 ${INDENT}};
136
137 ${INDENT}/** \relates ${CLASS} Stream output */
138 ${INDENT}std::ostream & operator<<( std::ostream & str, const ${CLASS} & obj );
139
140 ${INDENT}/** \relates ${CLASS} Verbose stream output */
141 ${INDENT}std::ostream & dumOn( std::ostream & str, const ${CLASS} & obj );
142
143 ${INDENT}/** \relates ${CLASS} */
144 ${INDENT}bool operator==( const ${CLASS} & lhs, const ${CLASS} & rhs );
145
146 ${INDENT}/** \relates ${CLASS} */
147 ${INDENT}inline bool operator!=( const ${CLASS} & lhs, const ${CLASS} & rhs )
148 ${INDENT}{ return !( lhs == rhs ); }
149
150 $(nsclose)
151 #endif // $INCLUDE_DEF
152 EOF
153 }
154
155 ######################################################################
156 function genCC() {
157 ######################################################################
158 cat <<EOF
159 $(intro $CLASS_CC)
160 #include <iostream>
161 //#include "zypp/base/LogTools.h"
162 #include "zypp/base/NonCopyable.h"
163
164 #include "${INCLUDE_H}"
165
166 using std::endl;
167
168 $(nsopen)
169
170 ${INDENT}///////////////////////////////////////////////////////////////////
171 ${INDENT}/// \class ${CLASS}::Impl
172 ${INDENT}/// \brief ${CLASS} implementation.
173 ${INDENT}///////////////////////////////////////////////////////////////////
174 ${INDENT}class ${CLASS}::Impl : private base::NonCopyable
175 ${INDENT}{
176 ${INDENT}  friend std::ostream & operator<<( std::ostream & str, const Impl & obj );
177 ${INDENT}  friend std::ostream & dumpOn( std::ostream & str, const Impl & obj );
178
179 ${INDENT}  public:
180
181 ${INDENT}  public:
182 ${INDENT}    /** Offer default Impl. */
183 ${INDENT}    static shared_ptr<Impl> nullimpl()
184 ${INDENT}    {
185 ${INDENT}      static shared_ptr<Impl> _nullimpl( new Impl );
186 ${INDENT}      return _nullimpl;
187 ${INDENT}    }
188 ${INDENT}  private:
189 ${INDENT}    friend Impl * rwcowClone<Impl>( const Impl * rhs );
190 ${INDENT}    /** clone for RWCOW_pointer */
191 ${INDENT}    Impl * clone() const
192 ${INDENT}    { return new Impl( *this ); }
193 ${INDENT}};
194
195 ${INDENT}/** \relates ${CLASS}::Impl Stream output */
196 ${INDENT}inline std::ostream & operator<<( std::ostream & str, const ${CLASS}::Impl & obj )
197 ${INDENT}{ return str << "${CLASS}::Impl"; }
198
199 ${INDENT}/** \relates ${CLASS}::Impl Verbose stream output */
200 ${INDENT}inline std::ostream & dumpOn( std::ostream & str, const ${CLASS}::Impl & obj )
201 ${INDENT}{ return str << obj; }
202
203 ${INDENT}///////////////////////////////////////////////////////////////////
204 ${INDENT}//
205 ${INDENT}//     CLASS NAME : ${CLASS}
206 ${INDENT}//
207 ${INDENT}///////////////////////////////////////////////////////////////////
208
209 ${INDENT}${CLASS}::${CLASS}()
210 ${INDENT}  : _pimpl( Impl::nullimpl() )
211 ${INDENT}{}
212
213 ${INDENT}${CLASS}::~${CLASS}()
214 ${INDENT}{}
215
216 ${INDENT}std::ostream & operator<<( std::ostream & str, const ${CLASS} & obj )
217 ${INDENT}{ return str << *obj._pimpl; }
218
219 ${INDENT}std::ostream & dumpOn( std::ostream & str, const ${CLASS} & obj )
220 ${INDENT}{ return dumpOn( str, *obj._pimpl ); }
221
222 ${INDENT}bool operator==( const ${CLASS} & lhs, const ${CLASS} & rhs )
223 ${INDENT}{ return lhs._pimpl == rhs._pimpl || lhs._pimpl && rhs._pimpl && *lhs._pimpl == *rhs._pimpl; }
224
225 $(nsclose)
226 EOF
227 }
228
229 ######################################################################
230 ######################################################################
231 ######################################################################
232
233 genH >$OUT_CLASS_H
234 genCC >$OUT_CLASS_CC