KDevelop4/Manual/Code generation with templates: Difference between revisions

From KDE UserBase Wiki
(Replace tt with code)
m (Change the name of the PHP example class)
Line 12: Line 12:
The most common use for code generation is probably writing new classes. To create a new class in an existing project, right click on the project folder and choose <menuchoice>Create from Template</menuchoice>. Choose <tt>Class</tt> in the category selection view, and the desired language and template in the other two views. After you have selected a class template, you will have to specify the details of the new class.  
The most common use for code generation is probably writing new classes. To create a new class in an existing project, right click on the project folder and choose <menuchoice>Create from Template</menuchoice>. Choose <tt>Class</tt> in the category selection view, and the desired language and template in the other two views. After you have selected a class template, you will have to specify the details of the new class.  


First you have to specify an identifier for the new class. This can be a simple name (like <code>Bus</code>) or a complete identifier with namespaces (like <code>Transportation::Bus</code>). In the latter case, '''KDevelop''' will parse the identifier and correctly separate the namespaces from the actual name. On the same page, you can add base classes for the new class. You may notice that some templates choose a base class on their own, you are free to remove it and/or add other bases. You should write the full inheritance statement here, which is language-dependant, such as <code>public QObject</code> for C++, <code>extends Serializable</code> for PHP or simply the name of the class for Python.  
First you have to specify an identifier for the new class. This can be a simple name (like <code>Bus</code>) or a complete identifier with namespaces (like <code>Transportation::Bus</code>). In the latter case, '''KDevelop''' will parse the identifier and correctly separate the namespaces from the actual name. On the same page, you can add base classes for the new class. You may notice that some templates choose a base class on their own, you are free to remove it and/or add other bases. You should write the full inheritance statement here, which is language-dependant, such as <code>public QObject</code> for C++, <code>extends SomeClass</code> for PHP or simply the name of the class for Python.  


In the next page, you are offered a selection of virtual methods from all inherited classes, as well as some default constructors, destructors and operators. Cheking the check box next to a method signature will implement this method in the new class.  
In the next page, you are offered a selection of virtual methods from all inherited classes, as well as some default constructors, destructors and operators. Cheking the check box next to a method signature will implement this method in the new class.  

Revision as of 08:49, 17 August 2012

Other languages:

Code generation with templates

KDevelop uses templates for generating source code files and to aviod writing repeatable code.

Creating a new class

The most common use for code generation is probably writing new classes. To create a new class in an existing project, right click on the project folder and choose Create from Template. Choose Class in the category selection view, and the desired language and template in the other two views. After you have selected a class template, you will have to specify the details of the new class.

First you have to specify an identifier for the new class. This can be a simple name (like Bus) or a complete identifier with namespaces (like Transportation::Bus). In the latter case, KDevelop will parse the identifier and correctly separate the namespaces from the actual name. On the same page, you can add base classes for the new class. You may notice that some templates choose a base class on their own, you are free to remove it and/or add other bases. You should write the full inheritance statement here, which is language-dependant, such as public QObject for C++, extends SomeClass for PHP or simply the name of the class for Python.

In the next page, you are offered a selection of virtual methods from all inherited classes, as well as some default constructors, destructors and operators. Cheking the check box next to a method signature will implement this method in the new class.

Clicking Next brings up a page where you can add members to a class. Depending on the selected template, these may appear in the new class as member variables, or the template may create properties with setters and getters for them. In a language where variable types have to be declared, such as C++, you have to specify both the type and the name of the member, such as int number or QString name. In other languages, you may leave out the type, but it is good practice to enter it anyway, because the selected template could still make some use of it.

In the following pages, you can choose a license for you new class, set any custom options required by the selected template, and configure output locations for all the generated files. By clicking Finish, you complete the assistant and create the new class. The generated files will be opened in the editor, so you can start adding code right away.

Creating a new unit test

Managing templates