​

C++ programming with DRAKON Editor

Home | Download

This tutorial shows how to create classes and methods in DRAKON-C++. See the C tutorial to learn how to build algorithms inside the method bodies.

A more advanced tutorial can be found in examples/C++ folder in the DRAKON Editor .zip file.

Contents

  • Getting started
  • class section
  • Methods
  • Method examples
  • options section
  • Other sections

Getting started

To create a DRAKON-C++ file:

  1. Create a new file with DRAKON Editor.

  2. Select C++ as the programming language. In the main menu, go to File / File properties... and choose "C++".

If you press Ctrl+B (Command-B on Mac), DRAKON Editor will generate an .h and a .cpp files in the same folder as the .drn file.

The DRAKON / Generate code menu command can be used instead.

You can also use drakon_gen.tcl command-line utility to generate the C++ files.

A DRAKON Editor file can contain one class and many free functions (functions that are not members of any class).

Creating free functions is described in the C tutorial.

Creating a class involves two actions:

  1. Add a class section to the file description.
  2. Add diagrams and mark them as methods.

class section

A section is a block of plain text in the file description. A section starts with the section header and contains all the text below the header until the next section or end of file.

The text before the first section is the actual "file description" and is ignored by the code generator.

To edit the file description, go to File / File description...

The class section header should be:

=== class ===

Type in the class declaration without the closing bracket under the header. You could add member fields, hand-written functions, inner classes and enums.

Do not place there the declarations of the methods that are generated from diagrams. Those declarations will be put there automatically.

Here is an example of a class section. Both class and struct keywords are accepted in the section body (but not in the header).

=== class ===
struct Easy
{

Another one:

=== class ===

class GoodClass
{

A more elaborate class:

=== class ===

// This is a very good class.
class GoodClass : public BadClass
{
private:
    int _count;
    float _width;

    struct Record
    {
        int First;
        int Second;
    };

public:
    // Manually added method declaration

    void DoIt();

Note that there are no closing brackets in these examples.

Methods

In order to turn the diagram into a class method, add the method keyword to the Parameters icon of the diagram.

For example, this diagram:

Print method

will produce the following definition in the .cpp file:

void Nice::Print() {
    /* item 6 */
    cout << "Nice::print: " << _value << endl;
    return;    
}

Here is the declaration which is inserted into the class:

private:
    void Print();

In order to make the method public, add the public keyword to the first line of the Parameters icon. To make it protected, use protected instead.

Public Print method

You can also use other keywords to change the function interface:

  • const will append the C++ keyword const to the function signature.
  • virtual will make the function virtual.
  • static will make the function static in the C++ sense. Needless to say that static and virtual should not be assigned to the same method.
  • inline will make the function inline.
  • abstract will append this smiley =0; to the function declaration. The function definition will not be generated.

In order to create a constructor, use the ctr keyword instead of method.

constructor.png

The dtr keyword turns the diagram into a destructor.

destructor.png

There can be several constructors per file, but only one destructor. The constructors must have different signatures.

Overloading for methods is not allowed.

Method examples

Parameters iconGenerated signature
method private: void Foo();
public method public: void Foo();
public method
int a
int b
returns const char*
public: const char* Foo(int a, int b);
public inline method
int a
int b
returns const char*
public: inline const char* Foo(int a, int b) {
// Generated method body.
}
public virtual method
int a
int b
returns int
public: virtual int Foo(int a, int b);
public virtual const method
int a
int b
returns int
public: virtual int Foo(int a, int b) const;
public virtual const abstract method
int a
int b
returns int
public: virtual int Foo(int a, int b) const = 0;
protected static method
int a
int b
public: static int Foo(int a, int b);
protected ctr
int a
int b
protected: explicit NiceClass(int a, int b);
dtr public: virtual ~NiceClass();

options section

The options section is optional.

By default, DRAKON Editor generates a private copy constructor and a private assignment operator in order to prevent accidental copying.

private:
    // non-copyable
    Nice(const Nice& other) {}
    void operator=(const Nice& other) {}

In order to get rid of these, add a copying option with the value "yes":

=== options ===
copying=yes

Other sections

DRAKON Editor supports a few sections intended for adding hand-written code to the generated files. Depending on the section, its text is added either to the top or to the bottom of the file. The section also determines which file will the section text go to - .h file or .cpp file.

SectionGoes to filePosition
h_header.h fileStart of file
h_footer.h fileEnd of file
c_header.cpp fileStart of file
c_footer.cpp fileEnd of file