Skip to content

Instantly share code, notes, and snippets.

@StefanFabian
Last active April 26, 2019 12:56
Show Gist options
  • Save StefanFabian/0cd83fb5e51b1ce019405b1b13ab05ed to your computer and use it in GitHub Desktop.
Save StefanFabian/0cd83fb5e51b1ce019405b1b13ab05ed to your computer and use it in GitHub Desktop.
doT C++ Header file template for the VSCode extension File Template Manager
{{
let date = new Date($.DATE);
let date_string = date.getDate().toString().padStart(2, '0') + '.' + (date.getMonth() + 1).toString().padStart(2, '0') + '.' + date.getFullYear();
let paths = $.DIR.split('/');
let index = paths.lastIndexOf('include');
let has_ns = index >= 0 && index < paths.length;
let ns = "";
let include_guard = $.NAME.toUpperCase() + '_H';
if (has_ns) {
ns = paths[index + 1];
include_guard = ns.toUpperCase() + "_" + include_guard;
}
let class_name = "";
let next_uppercase = true;
for ( let index = 0; index < $.NAME.length; ++index) {
if ($.NAME[index] === '_') {
next_uppercase = true;
while (++index < $.NAME.length && $.NAME[index] === '_');
}
if (index === $.NAME.length) break;
class_name += next_uppercase ? $.NAME[index].toUpperCase() : $.NAME[index];
next_uppercase = false;
}
}}
{{#def.select('TYPE', 'Select header type', ['class', 'struct', 'qobject', 'none'])}}
//
// Created by {{= $.AUTHOR }} on {{= date_string }}.
//
#ifndef {{= include_guard }}
#define {{= include_guard }}
{{? $.TYPE === 'qobject' }}
#include <QObject>
{{?}}
{{? has_ns }}
namespace {{=ns}}
{
{{?}}
{{? $.TYPE != 'none' }}
{{= ($.TYPE == 'class' || $.TYPE == 'struct' ? $.TYPE : 'class') }} {{= class_name }}{{? $.TYPE == 'qobject' }} : public QObject{{?}}
{
{{? $.TYPE === 'qobject' }}
Q_OBJECT
{{?}}
{{? $.TYPE !== 'struct'}}
public:
{{?}}
};
{{?}}
{{? has_ns }}
} // namespace {{=ns}}
{{?}}
#endif // {{= include_guard }}
@StefanFabian
Copy link
Author

This template may be too specific for your use case but it could be a good starting point.
It assumes a folder structure of
[...]/include/${PROJECT}/.../header_file.h
where ${PROJECT} is also the namespace.

The header file name is lowercase with underscores between words whereas the class name generated from the file name will be PascalCase.
Example: If you choose class when creating some_class.h, the class will be named SomeClass.

Required variables
It requires a variable named AUTHOR to be set in File Templates Manager's settings.
To do so, open the VS Code settings search for "Custom Vars" and edit in the settings.json.
In the settings.json add the following line "templates.customVars": { "AUTHOR": "Your Name" }

Full example:
Creating a header named some_project/include/some_project/some_example_class.h and choosing class when it asks will create:

//
// Created by Your Name on 26.04.2019.
//

#ifndef SOME_PROJECT_SOME_EXAMPLE_CLASS_H
#define SOME_PROJECT_SOME_EXAMPLE_CLASS_H

namespace some_project
{

class SomeExampleClass{
public:

};
} // some_project

#endif // SOME_PROJECT_SOME_EXAMPLE_CLASS_H

You can change the format of the date by changing the date_string variable in the third line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment