author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the tools applications of the Qt Toolkit. |
|
8 |
** |
|
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 |
|
14 |
** this package. |
|
15 |
** |
|
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. |
|
23 |
** |
|
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. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
#ifndef CODEGENERATOR_H |
|
42 |
#define CODEGENERATOR_H |
|
43 |
||
44 |
#include <QByteArray> |
|
45 |
#include <QString> |
|
46 |
#include <QList> |
|
47 |
#include <QStack> |
|
48 |
||
49 |
namespace CodeGenerator |
|
50 |
{ |
|
51 |
enum GeneratorType {NoopType, CompoundType, TextType, RepeaterType, CounterType, GroupType}; |
|
52 |
class BaseGenerator; |
|
53 |
typedef QStack<BaseGenerator *> GeneratorStack; |
|
54 |
||
55 |
template <typename ValueType> |
|
56 |
class Stacker { |
|
57 |
public: |
|
58 |
Stacker(QStack<ValueType> *stack, ValueType value) : stack(stack) { stack->push(value); } |
|
59 |
~Stacker() { stack->pop();} |
|
60 |
private: |
|
61 |
QStack<ValueType> *stack; |
|
62 |
}; |
|
63 |
typedef Stacker<BaseGenerator *> GeneratorStacker; |
|
64 |
||
65 |
class BaseGenerator |
|
66 |
{ |
|
67 |
public: |
|
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; |
|
73 |
GeneratorType type; |
|
74 |
}; |
|
75 |
||
76 |
class Item |
|
77 |
{ |
|
78 |
public: |
|
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; |
|
86 |
}; |
|
87 |
||
88 |
class CompoundGenerator : public BaseGenerator |
|
89 |
{ |
|
90 |
public: |
|
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); }; |
|
95 |
protected: |
|
96 |
BaseGenerator * const a; |
|
97 |
BaseGenerator * const b; |
|
98 |
}; |
|
99 |
||
100 |
class Compound : public Item |
|
101 |
{ |
|
102 |
public: |
|
103 |
Compound(const Item &a, const Item &b) : Item(new CompoundGenerator(a.generator, b.generator)) {} |
|
104 |
}; |
|
105 |
||
106 |
class TextGenerator : public BaseGenerator |
|
107 |
{ |
|
108 |
public: |
|
109 |
TextGenerator(const QByteArray &text) : BaseGenerator(TextType), text(text) {} |
|
110 |
virtual QByteArray generate(GeneratorStack *) { return text; }; |
|
111 |
protected: |
|
112 |
QByteArray text; |
|
113 |
}; |
|
114 |
||
115 |
class Text : public Item { |
|
116 |
public: |
|
117 |
Text(const QByteArray &text) : Item(new TextGenerator(text)) {} |
|
118 |
Text(const char * const text) : Item(new TextGenerator(QByteArray(text))) {} |
|
119 |
}; |
|
120 |
||
121 |
class RepeaterGenerator : public BaseGenerator |
|
122 |
{ |
|
123 |
public: |
|
124 |
RepeaterGenerator(BaseGenerator * const childGenerator) |
|
125 |
: BaseGenerator(RepeaterType), repeatCount(1), repeatOffset(0), childGenerator(childGenerator) {} |
|
126 |
virtual QByteArray generate(GeneratorStack *stack); |
|
127 |
||
128 |
int repeatCount; |
|
129 |
int repeatOffset; |
|
130 |
int currentRepeat; |
|
131 |
BaseGenerator * const childGenerator; |
|
132 |
}; |
|
133 |
||
134 |
class Repeater : public Item { |
|
135 |
public: |
|
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; } |
|
141 |
}; |
|
142 |
||
143 |
class CounterGenerator : public BaseGenerator |
|
144 |
{ |
|
145 |
public: |
|
146 |
CounterGenerator() : BaseGenerator(CounterType), offset(0), increment(1), reverse(false) {} |
|
147 |
QByteArray generate(GeneratorStack *stack) |
|
148 |
{ |
|
149 |
if (reverse) |
|
150 |
return QByteArray::number(repeatCount(stack) - (currentCount(stack) * increment) + offset + 1); |
|
151 |
else |
|
152 |
return QByteArray::number((currentCount(stack) * increment) + offset); |
|
153 |
} |
|
154 |
int offset; |
|
155 |
int increment; |
|
156 |
bool reverse; |
|
157 |
}; |
|
158 |
||
159 |
class Counter : public Item { |
|
160 |
public: |
|
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; } |
|
169 |
||
170 |
}; |
|
171 |
||
172 |
class GroupGenerator : public BaseGenerator |
|
173 |
{ |
|
174 |
public: |
|
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); |
|
179 |
int currentRepeat; |
|
180 |
BaseGenerator * const childGenerator; |
|
181 |
BaseGenerator *separator; |
|
182 |
BaseGenerator *prefix; |
|
183 |
BaseGenerator *postfix; |
|
184 |
}; |
|
185 |
||
186 |
class Group : public Item |
|
187 |
{ |
|
188 |
public: |
|
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; } |
|
196 |
}; |
|
197 |
||
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); |
|
201 |
||
202 |
} //namespace CodeGenerator |
|
203 |
||
204 |
#endif |