137cbcac63c4a1c7893b748444e9ff0c335c5e5d
[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 #include "zypp/base/SafeBool.h"
104 #include "zypp/base/NonCopyable.h"
105
106 $(nsopen)
107 ${INDENT}///////////////////////////////////////////////////////////////////
108 ${INDENT}/// \class ${CLASS}
109 ${INDENT}/// \brief
110 ${INDENT}///////////////////////////////////////////////////////////////////
111 ${INDENT}class ${CLASS} : protected base::SafeBool<${CLASS}>, private base::NonCopyable
112 ${INDENT}{
113 ${INDENT}  friend std::ostream & operator<<( std::ostream & str, const ${CLASS} & obj );
114 ${INDENT}  friend std::ostream & dumpOn( std::ostream & str, const ${CLASS} & obj );
115 ${INDENT}  friend bool operator==( const ${CLASS} & lhs, const ${CLASS} & rhs );
116
117 ${INDENT}  public:
118 ${INDENT}    /** Default ctor */
119 ${INDENT}    ${CLASS}();
120
121 ${INDENT}    /** Dtor */
122 ${INDENT}    ~${CLASS}();
123
124 ${INDENT}  public:
125 ${INDENT}    /**  Validate object in a boolean context. */
126 ${INDENT}    using base::SafeBool<${CLASS}>::operator bool_type;
127
128 ${INDENT}  private:
129 ${INDENT}    friend base::SafeBool<${CLASS}>::operator bool_type() const;
130 ${INDENT}    /**  Validate object in a boolean context. */
131 ${INDENT}    bool boolTest() const
132 ${INDENT}    {
133 ${INDENT}      /* !!! Perform Boolean logic here AND check implememtation of operator==!!!
134 ${INDENT}       * NOTE: SafeBool requires operator== otherwise equality is reduced to
135 ${INDENT}       *       ( bool(${CLASS}) == bool(${CLASS}) ).
136 ${INDENT}       */
137 ${INDENT}    }
138 ${INDENT}  public:
139 ${INDENT}    class Impl;                 ///< Implementation class.
140 ${INDENT}  private:
141 ${INDENT}    RWCOW_pointer<Impl> _pimpl; ///< Pointer to implementation.
142 ${INDENT}};
143
144 ${INDENT}/** \relates ${CLASS} Stream output */
145 ${INDENT}std::ostream & operator<<( std::ostream & str, const ${CLASS} & obj );
146
147 ${INDENT}/** \relates ${CLASS} Verbose stream output */
148 ${INDENT}std::ostream & dumOn( std::ostream & str, const ${CLASS} & obj );
149
150 ${INDENT}/** \relates ${CLASS} */
151 ${INDENT}bool operator==( const ${CLASS} & lhs, const ${CLASS} & rhs );
152
153 ${INDENT}/** \relates ${CLASS} */
154 ${INDENT}inline bool operator!=( const ${CLASS} & lhs, const ${CLASS} & rhs )
155 ${INDENT}{ return !( lhs == rhs ); }
156
157 $(nsclose)
158 #endif // $INCLUDE_DEF
159 EOF
160 }
161
162 ######################################################################
163 function genCC() {
164 ######################################################################
165 cat <<EOF
166 $(intro $CLASS_CC)
167 #include <iostream>
168 //#include "zypp/base/LogTools.h"
169
170 #include "${INCLUDE_H}"
171
172 using std::endl;
173
174 $(nsopen)
175
176 ${INDENT}///////////////////////////////////////////////////////////////////
177 ${INDENT}/// \class ${CLASS}::Impl
178 ${INDENT}/// \brief ${CLASS} implementation.
179 ${INDENT}///////////////////////////////////////////////////////////////////
180 ${INDENT}struct ${CLASS}::Impl
181 ${INDENT}{
182 ${INDENT}  friend std::ostream & operator<<( std::ostream & str, const Impl & obj );
183 ${INDENT}  friend std::ostream & dumpOn( std::ostream & str, const Impl & obj );
184
185 ${INDENT}  public:
186
187 ${INDENT}  public:
188 ${INDENT}    /** Offer default Impl. */
189 ${INDENT}    static shared_ptr<Impl> nullimpl()
190 ${INDENT}    {
191 ${INDENT}      static shared_ptr<Impl> _nullimpl( new Impl );
192 ${INDENT}      return _nullimpl;
193 ${INDENT}    }
194 ${INDENT}  private:
195 ${INDENT}    friend Impl * rwcowClone<Impl>( const Impl * rhs );
196 ${INDENT}    /** clone for RWCOW_pointer */
197 ${INDENT}    Impl * clone() const
198 ${INDENT}    { return new Impl( *this ); }
199 ${INDENT}};
200
201 ${INDENT}/** \relates ${CLASS}::Impl Stream output */
202 ${INDENT}inline std::ostream & operator<<( std::ostream & str, const ${CLASS}::Impl & obj )
203 ${INDENT}{ return str << "${CLASS}::Impl"; }
204
205 ${INDENT}/** \relates ${CLASS}::Impl Verbose stream output */
206 ${INDENT}inline std::ostream & dumpOn( std::ostream & str, const ${CLASS}::Impl & obj )
207 ${INDENT}{ return str << obj; }
208
209 ${INDENT}///////////////////////////////////////////////////////////////////
210 ${INDENT}//
211 ${INDENT}//     CLASS NAME : ${CLASS}
212 ${INDENT}//
213 ${INDENT}///////////////////////////////////////////////////////////////////
214
215 ${INDENT}${CLASS}::${CLASS}()
216 ${INDENT}  : _pimpl( Impl::nullimpl() )
217 ${INDENT}{}
218
219 ${INDENT}${CLASS}::~${CLASS}()
220 ${INDENT}{}
221
222 ${INDENT}std::ostream & operator<<( std::ostream & str, const ${CLASS} & obj )
223 ${INDENT}{ return str << *obj._pimpl; }
224
225 ${INDENT}std::ostream & dumpOn( std::ostream & str, const ${CLASS} & obj )
226 ${INDENT}{ return dumpOn( str, *obj._pimpl ); }
227
228 ${INDENT}bool operator==( const ${CLASS} & lhs, const ${CLASS} & rhs )
229 ${INDENT}{ return lhs._pimpl == rhs._pimpl || lhs._pimpl && rhs._pimpl && *lhs._pimpl == *rhs._pimpl; }
230
231 $(nsclose)
232 EOF
233 }
234
235 ######################################################################
236 ######################################################################
237 ######################################################################
238
239 genH >$OUT_CLASS_H
240 genCC >$OUT_CLASS_CC