1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
41 #ifndef CODEGENERATOR_H
42 #define CODEGENERATOR_H
49 namespace CodeGenerator
51 enum GeneratorType
{NoopType
, CompoundType
, TextType
, RepeaterType
, CounterType
, GroupType
};
53 typedef QStack
<BaseGenerator
*> GeneratorStack
;
55 template <typename ValueType
>
58 Stacker(QStack
<ValueType
> *stack
, ValueType value
) : stack(stack
) { stack
->push(value
); }
59 ~Stacker() { stack
->pop();}
61 QStack
<ValueType
> *stack
;
63 typedef Stacker
<BaseGenerator
*> GeneratorStacker
;
68 BaseGenerator(GeneratorType type
= NoopType
) : type(type
) {}
69 virtual ~BaseGenerator() {};
70 virtual QByteArray
generate(GeneratorStack
*stack
) { Q_UNUSED(stack
); return QByteArray(); };
71 int currentCount(GeneratorStack
*stack
) const;
72 int repeatCount(GeneratorStack
*stack
) const;
79 Item(BaseGenerator
* const base
) : generator(base
) {}
80 Item(const char * const text
);
81 QByteArray
generate() const
82 { GeneratorStack stack
; return generator
->generate(&stack
); }
83 // ### TODO: Fix memory leak!
84 // QExplicitlySharedDataPointer<BaseGenerator> generator;
85 BaseGenerator
* const generator
;
88 class CompoundGenerator
: public BaseGenerator
91 CompoundGenerator(BaseGenerator
* const a
, BaseGenerator
* const b
)
92 : BaseGenerator(CompoundType
), a(a
), b(b
) {}
93 virtual QByteArray
generate(GeneratorStack
*stack
)
94 { return a
->generate(stack
) + b
->generate(stack
); };
96 BaseGenerator
* const a
;
97 BaseGenerator
* const b
;
100 class Compound
: public Item
103 Compound(const Item
&a
, const Item
&b
) : Item(new CompoundGenerator(a
.generator
, b
.generator
)) {}
106 class TextGenerator
: public BaseGenerator
109 TextGenerator(const QByteArray
&text
) : BaseGenerator(TextType
), text(text
) {}
110 virtual QByteArray
generate(GeneratorStack
*) { return text
; };
115 class Text
: public Item
{
117 Text(const QByteArray
&text
) : Item(new TextGenerator(text
)) {}
118 Text(const char * const text
) : Item(new TextGenerator(QByteArray(text
))) {}
121 class RepeaterGenerator
: public BaseGenerator
124 RepeaterGenerator(BaseGenerator
* const childGenerator
)
125 : BaseGenerator(RepeaterType
), repeatCount(1), repeatOffset(0), childGenerator(childGenerator
) {}
126 virtual QByteArray
generate(GeneratorStack
*stack
);
131 BaseGenerator
* const childGenerator
;
134 class Repeater
: public Item
{
136 Repeater(const Item
&item
) : Item(new RepeaterGenerator(item
.generator
)) {}
137 void setRepeatCount(int count
)
138 { static_cast<RepeaterGenerator
* const>(generator
)->repeatCount
= count
; }
139 void setRepeatOffset(int offset
)
140 { static_cast<RepeaterGenerator
* const>(generator
)->repeatOffset
= offset
; }
143 class CounterGenerator
: public BaseGenerator
146 CounterGenerator() : BaseGenerator(CounterType
), offset(0), increment(1), reverse(false) {}
147 QByteArray
generate(GeneratorStack
*stack
)
150 return QByteArray::number(repeatCount(stack
) - (currentCount(stack
) * increment
) + offset
+ 1);
152 return QByteArray::number((currentCount(stack
) * increment
) + offset
);
159 class Counter
: public Item
{
161 Counter() : Item(new CounterGenerator()) {}
162 Counter(int offset
) : Item(new CounterGenerator()) { setOffset(offset
); }
163 void setOffset(int offset
)
164 { static_cast<CounterGenerator
*>(generator
)->offset
= offset
; }
165 void setIncrement(int increment
)
166 { static_cast<CounterGenerator
*>(generator
)->increment
= increment
; }
167 void setReverse(bool reverse
)
168 { static_cast<CounterGenerator
*>(generator
)->reverse
= reverse
; }
172 class GroupGenerator
: public BaseGenerator
175 GroupGenerator(BaseGenerator
* const childGenerator
)
176 : BaseGenerator(GroupType
), currentRepeat(0), childGenerator(childGenerator
),
177 separator(new BaseGenerator()), prefix(new BaseGenerator()), postfix(new BaseGenerator()) { }
178 virtual QByteArray
generate(GeneratorStack
*stack
);
180 BaseGenerator
* const childGenerator
;
181 BaseGenerator
*separator
;
182 BaseGenerator
*prefix
;
183 BaseGenerator
*postfix
;
186 class Group
: public Item
189 Group(const Item
&item
) : Item(new GroupGenerator(item
.generator
)) { setSeparator(", "); }
190 void setSeparator(const Item
&separator
)
191 { static_cast<GroupGenerator
*>(generator
)->separator
= separator
.generator
; }
192 void setPrefix(const Item
&prefix
)
193 { static_cast<GroupGenerator
*>(generator
)->prefix
= prefix
.generator
; }
194 void setPostfix(const Item
&postfix
)
195 { static_cast<GroupGenerator
*>(generator
)->postfix
= postfix
.generator
; }
198 const Compound
operator+(const Item
&a
, const Item
&b
);
199 const Compound
operator+(const Item
&a
, const char * const text
);
200 const Compound
operator+(const char * const text
, const Item
&b
);
202 } //namespace CodeGenerator