The Kode library is a library for generating C++ code. It contains classes to represent the different element of code like files, classes, functions, etc. and helpers to write them to nicely formatted C++ source code. This makes writing C++ code generators a lot more easy. It also comes with a command line tool, which provides some code generation utilities.

Code Generation Library

The goal of the Kode Library (libkode) is to provide an abstraction for generating code, which makes it easy to write code generators, and takes away the confusion code generating code usually inherently contains.

Of course it's possible to write code generating code just by using some string output functions, but this usually results in code snippets embedded in code, and long-winded if-else cascades which make it hard to see the structure of the generated code. libkode goal is to remove this confusion by providing a clean abstraction layer representing the structure of the generated code.

libkode provides classes to represent C++ classes, functions, header and implementation files, and the elements which are used there, e.g. function parameters, license headers, or namespace declarations. It also provides helper functions to generate properly indented code, and write nicely formatted C++ code to the required files.

Example

// Define file and meta information
KODE::File file;
file.setProject( "Example Project" );
file.addCopyright( QDate::currentDate().year(),
  "Cornelius Schumacher", "schumacher@kde.org" );
file.setLicense( KODE::License( Kode::License::GPL ) );

// Setup class definition
KODE::Class c( "Hello" );
c.setDocs( "This class implements a hello world example." );

// Define function
KODE::Function sayHello( "sayHello", "void" );
sayHello.addArgument( "const QString &to" );

// Define body of function
KODE::Code code;
code += "cout << \"Hello, \" << to << \"!\"";
sayHello.setBody( code );

// Add function to class
c.addFunction( sayHello );

// Generate code
KODE::Printer p;
p.printHeader( file );
p.printImplementation( file );

kode command line tool

The kode command line tool provides some utilities for generating code. It for example can generate skeletons for a class to be filled in with actual code, it can generate a complete kioslave skeleton.

It can also convert a snippet of code into code creating this code. This is useful when writing code generators. The generated code makes use of libkode for maximum readability of the code generating code. This kind of code can be mind-twisting, so it helps to have support tools at hand.

kode command line options

Usage: kode [options] [filename]

Options:
  -c, --create-class        Create class
  -d, --create-dialog       Create dialog
  --create-kioslave         Create kioslave
  --create-main             Create main function template
  -y, --codify              Create generator code for given source
  --add-property            Add property to class
  --inplace                 Change file in place
  --author-email      Add author with given email address
  --project           Name of project
  --gpl                     Use GPL as license
  --lgpl                    Use LGPL as license
  --classname         Name of class
  --filename          Name of file
  --namespace         Namespace
  --warning                 Create warning about code generation
  --qt-exception            Add Qt exception to GPL
  --singleton               Create a singleton class
  --protocol                kioslave protocol

Arguments:
  filename                  Source code file name