Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / tools / qtconcurrent / codegenerator / src / codegenerator.h
blobc1aefe9bd578a80d6f736bc2398e223e855e10c9
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
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.
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.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
41 #ifndef CODEGENERATOR_H
42 #define CODEGENERATOR_H
44 #include <QByteArray>
45 #include <QString>
46 #include <QList>
47 #include <QStack>
49 namespace CodeGenerator
51 enum GeneratorType {NoopType, CompoundType, TextType, RepeaterType, CounterType, GroupType};
52 class BaseGenerator;
53 typedef QStack<BaseGenerator *> GeneratorStack;
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;
63 typedef Stacker<BaseGenerator *> GeneratorStacker;
65 class BaseGenerator
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;
76 class Item
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;
88 class CompoundGenerator : public BaseGenerator
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;
100 class Compound : public Item
102 public:
103 Compound(const Item &a, const Item &b) : Item(new CompoundGenerator(a.generator, b.generator)) {}
106 class TextGenerator : public BaseGenerator
108 public:
109 TextGenerator(const QByteArray &text) : BaseGenerator(TextType), text(text) {}
110 virtual QByteArray generate(GeneratorStack *) { return text; };
111 protected:
112 QByteArray text;
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))) {}
121 class RepeaterGenerator : public BaseGenerator
123 public:
124 RepeaterGenerator(BaseGenerator * const childGenerator)
125 : BaseGenerator(RepeaterType), repeatCount(1), repeatOffset(0), childGenerator(childGenerator) {}
126 virtual QByteArray generate(GeneratorStack *stack);
128 int repeatCount;
129 int repeatOffset;
130 int currentRepeat;
131 BaseGenerator * const childGenerator;
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; }
143 class CounterGenerator : public BaseGenerator
145 public:
146 CounterGenerator() : BaseGenerator(CounterType), offset(0), increment(1), reverse(false) {}
147 QByteArray generate(GeneratorStack *stack)
149 if (reverse)
150 return QByteArray::number(repeatCount(stack) - (currentCount(stack) * increment) + offset + 1);
151 else
152 return QByteArray::number((currentCount(stack) * increment) + offset);
154 int offset;
155 int increment;
156 bool reverse;
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; }
172 class GroupGenerator : public BaseGenerator
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;
186 class Group : public Item
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; }
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
204 #endif