Jump to content

Development/Tutorials/KDevelop/Creating a class template: Difference between revisions

From KDE TechBase
Miha (talk | contribs)
Miha (talk | contribs)
Line 55: Line 55:


Now, let's move on to the class declaration.
Now, let's move on to the class declaration.
In most free software project, source files start with a license header. This header varies with the chosen license, author, and the programming language's comment characters. When using our template, the user choose one of the suggested licenses or write his own. In either case, the full text of the license header is available to the template as the <tt>license</tt> variable.
Since C++ has block-style comments, we could write the license simply as
<syntaxhighlight lang="cpp">
/*
{{ license }}
*/
</syntaxhighlight>
However, it might be easier to read if the license had some kind of formatting. For this purpose, '''KDevPlatform''' includes a template filter library that makes it possible to write a nicely-formatted license header. We can use it like this:
<syntaxhighlight lang="cpp">
{% load kdev_filters %}
/*
{{ license|lines_prepend:" * " }}
*/
</syntaxhighlight>
The <tt>lines_prened</tt> filter prepends its ''argument'' (in our case " * ") to every line of its input (in our case the license). This can produce licenses like the following
{{Output|1=<nowiki>
/*
* This file is part of KDevelop
* Copyright 2012 Example Developer <[email protected]>
*/
</nowiki>}}
If our file is to be included somewhere, it is good practice to provide an include guard macro. '''KDevPlatform''' ships a convenience template for generating such a macro called <tt>include_guard_cpp.txt</tt>. We can use it like this
<syntaxhighlight lang="smarty">
#ifndef {% include "include_guard_cpp.txt" %}
#define {% include "include_guard_cpp.txt" %}
// Class declaration goes here
#endif // {% include "include_guard_cpp.txt" %}
</syntaxhighlight>
{{Note|The included template will expand into an uppercase class name with an appended <tt>_H</tt>, which could also be achieved by typing <code><nowiki>{{ name|upper }}_H</nowiki></code>. However, the include guard template also supports namespaces.}}
{{Note|See the [https://docs.djangoproject.com/en/1.1/ref/templates/builtins/#include Django documentation] for details about the <tt>include</tt> command}}


== Installing the template ==
== Installing the template ==

Revision as of 12:11, 11 July 2012

 
Under Construction
This is a new page, currently under construction!


Creating a project template
Tutorial Series   KDevelop Templates
Previous   Grantlee for theme artists, Django template language
What's Next  
Further Reading   Class template specification

Introduction

In this tutorial, we will create a template for a basic C++ class. It will demonstrate using the Grantlee template language, as well features specific to KDevelop class templates.

We chose C++ because even the simplest class definition often consists of two separate files, header and implementation. It is possible to include any number of files in a class template (there should be at least one, obviously).

Creating the directory structure

Note
There is a project template for a C++ class template available here. The resulting project is similar to what we will create here. You may use it to create the files but still follow this tutorial for explanation.


We will start with an empty directory. Name it something unique, like kdev_class_tutorial.

The description file

Every class template needs a description file. It is a regular desktop file with an extension of .desktop. Its base name must match the name of the generated archive, so let's call it kdev_class_tutorial.desktop. Paste in these contents:

[General]
Name=Tutorial
Comment=An example C++ class template, suitable for a tutorial
Category=C++
Files=Header,Implementation

[Header]
Name=Header
File=class.h
OutputFile={{ name }}.h

[Implementation]
Name=Implementation
File=class.cpp
OutputFile={{ name }}.cpp

You can see there are three sections in the description file: one for general properties, and one for each output file. In order for a file to be generated, it has to be listed in the Files entry, as well as have its own section with the same name.

In the output file sections, we already used template variables: {{ name }} will be replaced with the class name. The actual generated header file will not be {{ name }}.h, but rather Exmample.h, assuming we name our class Example.

The class header file

Now, let's move on to the class declaration.

In most free software project, source files start with a license header. This header varies with the chosen license, author, and the programming language's comment characters. When using our template, the user choose one of the suggested licenses or write his own. In either case, the full text of the license header is available to the template as the license variable.

Since C++ has block-style comments, we could write the license simply as

/*
{{ license }}
*/

However, it might be easier to read if the license had some kind of formatting. For this purpose, KDevPlatform includes a template filter library that makes it possible to write a nicely-formatted license header. We can use it like this:

{% load kdev_filters %}
/*
{{ license|lines_prepend:" * " }}
 */

The lines_prened filter prepends its argument (in our case " * ") to every line of its input (in our case the license). This can produce licenses like the following

/*
 * This file is part of KDevelop
 * Copyright 2012 Example Developer <[email protected]>
 */

If our file is to be included somewhere, it is good practice to provide an include guard macro. KDevPlatform ships a convenience template for generating such a macro called include_guard_cpp.txt. We can use it like this

#ifndef {% include "include_guard_cpp.txt" %}
#define {% include "include_guard_cpp.txt" %}

// Class declaration goes here

#endif // {% include "include_guard_cpp.txt" %}
Note
The included template will expand into an uppercase class name with an appended _H, which could also be achieved by typing {{ name|upper }}_H. However, the include guard template also supports namespaces.
Note
See the Django documentation for details about the include command


Installing the template

Now we have all the template contents prepared. We only need to compress the directory and make it available to KDevelop.

There are three ways for doing that:

  • compressing and loading it manually
  • loading it from within KDevelop
  • using a CMake macro.

Manually

First create an archive out of the kdev_tutorial directory. Many archive formats are accepted, but for best compatibility use .zip in Windows and .tar.bz2 everywhere else. In Dolphin, this can be achieved by right clicking within the directory, choosing Compress -> Compress To..., and entering the filename as kdev_tutorial.tar.bz2.

Now copy the new archive somewhere where KDevelop will find it. It looks for template archives in the ${PREFIX}/share/apps/kdevappwizard/templates, where prefix is either the system directory where KDE is installed (for example /usr) or the the local KDE configuration (for example ~/.kde or ~/.kde4). Copying our template to either is fine, choose dependeng on whether you want the template available only to you or all users on your computer.

After a run of kbuildsycoca4, KDevelop should pick up and offer you new template when creating a new project.

From KDevelop

Note
This method requires an unreleased version of KDevelop. If you are not using the latest development version, use one of the other two options.


You can have KDevelop do all this. Select Project -> New From Template, then in the dialog click the Load Template From File. Navigate to the kdev_tutorial directory, and open the kdev_tutorial.kdevtemplate file. Your template directory will be compressed and install into the local directory (usually ~/.kde/share/apps/kdevappwizard/templates).

With CMake

KDevPlatform includes a CMake macro that takes care of compressing and installing templates. This has the disadvantage of requiring a large library for installing a simple archive file, but may be useful especially for project that already depend on it. Alternatively, you may copy KDevPlatformMacros.cmake and put it into your project.

The macro requires that each template is in a separate directory, so create a new kdev_tutorial_templates directory and move kdev_tutorial into it. Then add the following CMakeLists.txt file to the top-level directory

project(kdevelop_template_tutorial)

find_package(KDE4 REQUIRED)
find_package(KDevPlatform REQUIRED)

set(TEMPLATE_DIRS kdev_tutorial)
kdevplatform_add_app_templates(${TEMPLATE_DIRS})

Build and install the project with the usual CMake steps

mkdir build
cd build
cmake ..
make
make install

Note that this will install the template into ${CMAKE_INSTALL_PREFIX}, for which you may need root privileges.