1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2016 The Qt Company Ltd. 4 ** Contact: https://www.qt.io/licensing/ 5 ** 6 ** This file is part of the tools applications of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see https://www.qt.io/terms-conditions. For further 15 ** information use the contact form at https://www.qt.io/contact-us. 16 ** 17 ** GNU General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU 19 ** General Public License version 3 as published by the Free Software 20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT 21 ** included in the packaging of this file. Please review the following 22 ** information to ensure the GNU General Public License requirements will 23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html. 24 ** 25 ** $QT_END_LICENSE$ 26 ** 27 ****************************************************************************/ 28 #include <QDebug> 29 #include "codegenerator.h" 30 using namespace CodeGenerator; 31 main()32int main() 33 { 34 // The code generator works on items. Each item has a generate() function: 35 Item item(""); 36 qDebug() << item.generate(); // produces "". 37 38 // There are several Item subclasses. Text items contains a text string which they 39 // reproduce when generate is called: 40 Text text(" Hi there"); 41 qDebug() << text.generate(); // produces " Hi there". 42 43 // Items can be concatenated: 44 Item sentence = text + Text(" Bye there") ; 45 qDebug() << sentence.generate(); // produces "Hi there Bye there". 46 // (Internally, this creates a tree of items, and generate is called recursively 47 // for items that have children.) 48 49 // Repeater items repeat their content when generate is called: 50 Repeater repeater = text; 51 repeater.setRepeatCount(3); 52 qDebug() << repeater.generate(); // produces "Hi there Hi there Hi there". 53 54 // Counters evaluate to the current repeat index. 55 Repeater repeater2 = text + Counter(); 56 repeater2.setRepeatCount(3); 57 qDebug() << repeater2.generate(); // produces "Hi there0 Hi there1 Hi there2". 58 59 // Groups provide sub-groups which are repeated according to the current repeat index. 60 // Counters inside Groups evaluate to the local repeat index for the Group. 61 Group arguments("Arg" + Counter() + " arg" + Counter()); 62 Repeater function("void foo(" + arguments + ");\n"); 63 function.setRepeatCount(3); 64 qDebug() << function.generate(); 65 66 // Produces: 67 // void foo(Arg1 arg1); 68 // void foo(Arg1 arg1, Arg2 arg2); 69 // void foo(Arg1 arg1, Arg2 arg2, Arg3 arg3); 70 } 71