CLion new C++ Class file template

2k Views Asked by At

CLion has file template to generate C++ Class, which generates source file and header. In my project I have handler classes that have same code part, and i want to generate them by file templates. And templates that i created can't do this:

  1. Set file names for class MyHandlerClass i want my_handler_class.cpp and .hpp

  2. From one class name i want to generate 2 files header and source, don't know how to do that.

  3. I also want to have string like MyClass -> my-class, found function $lowercaseAndDash($NAME) but don't know why its not works

I have template for header:

#pragma once
// includes

namespace handlers {

class ${NAME}: public Parent {
  public:
  // methods
};

}

I tried to solve first problem by setting the file name like this: #set ($FILE_NAME = "test_class.hpp"). But i don't know hot to set CamelCase to snake, and this don't works.

Also find in docs function to snake case, but its not works for me in file template.

1

There are 1 best solutions below

0
On BEST ANSWER
  1. Its impossible to change file name from template. When you create a C++ Class, you set class name and select file name encoding. When you create file from template its saves file with your file name. I found solution this way, enter file_name (in snake case), and with Velocity create CamelCaseName for class name:

    #set( $CamelCaseName = "" )

    #set( $part = "" )

    #foreach($part in $NAME.split("_"))

    #set( $CamelCaseName = "${CamelCaseName}$part.substring(0,1).toUpperCase()$part.substring(1).toLowerCase()" )

    #end

  2. Its impossible to generate 2 files from one click like it do C++ Class template. I have to templates, and use them both.

  3. This function is for liveTemplates, for FileTemplate i used velocity: #set( $NeededString = $NAME.replaceAll('_', '-') )

    To include header file from cpp use #[[#include]]# "${NAME}.hpp"