CLion new C++ Class file template - c++

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:
Set file names for class MyHandlerClass i want my_handler_class.cpp and .hpp
From one class name i want to generate 2 files header and source, don't know how to do that.
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.

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
Its impossible to generate 2 files from one click like it do C++ Class template. I have to templates, and use them both.
This function is for liveTemplates, for FileTemplate i used velocity: #set( $NeededString = $NAME.replaceAll('_', '-') )
To include header file from cpp use #[[#include]]# "${NAME}.hpp"

Related

Intellij file template: how to set file name dynamically

I'm making a intellij template MyClassController but I want to insert just MyClass as name because I need to create thousands with same pattern.
I tried
package ...
#set($BASENAME = $NAME)
#set($NAME=${BASENAME} + "Controller")
#set($OBJNAME = $BASENAME.substring(0, 1).toLowerCase() + $BASENAME.substring(1))
import ...
but if #set($NAME= ... ) is present filename popup is not showing.
BASENAME's value (that is "") became NAME's value so my file end being named "$BASENAME" or "$".
There's a way to force name popup to appear or rename file after file is generated?

In Buck, how do I expand a genrule output directory?

I have a genrule that outputs a directory containing C++ headers. I also have a cxx_library which uses the output of the genrule. The headers field of the cxx_library looks like this:
...
headers = [
':my-headers',
],
...
The problem is that my C++ source files include the headers like this:
#include "my_header.h"
But Buck prepares the staging area like this:
my-cxx-library#default,private-headers/out
Where out is an alias to the folder containing my generated headers.
So in order to use the headers, I would have to include them like this:
#include "out/my_header.h"
The library is not my own, so I would not like to change the source-code. Instead, I would like to use something like a subdir_glob to include everything inside :my-headers/out.
I tried this:
...
headers = subdir_glob([
(':my-headers/out', '**/*.h'),
]),
...
However, it seems that when done this way, the string :my-headers does not get resolved to the output path of :my-headers.
Is there a function in buck that can expand a rule to its output path?
This isn't trivially possible today, but there is a workaround you can use:
genrule(
name = 'headers',
cmd = 'generate_headers.py --outdir=$OUT'
out = 'headers-dir',
)
genrule(
name = 'generated_header_A.h',
cmd = 'cp $(location :headers)/genereated_header_A.h $OUT',
out = 'generated_header_A.h',
)
Then, in your cxx_library, you just need to declare your headers by referencing the location:
headers = {
'some_namespace/header_A.h': ':generated_header_A.h',
},
The above code assumes all of this is in the same build file, but if it isn't, you just need to use a fully-qualified build target instead.

How to change C++ include guards in CLion?

When CLion creates a header file it adds include guard strings like this:
#ifndef PROJECTNAME_FILENAME_H
#define PROJECTNAME_FILENAME_H
/* ... code ... */
#endif //PROJECTNAME_FILENAME_H
But I want just FILENAME_H without the PROJECTNAME_ prefix. How to change it in CLion settings?
Bit late to this question, but I've got a slightly more involved solution that'll handle this without the need for manual post-processing regardless of file extension:
Head into your File and Code Templates - The other answers already detail how to do this.
In the File and Code Templates settings page, change to the Includes tab.
Click the + to create a new Include template. Name it something like IncludeGuard and set the Extension to h.
Input the following for the contents. Make sure you don't include any blank lines before or after.
#macro( includeGuard $filename $ext )
#set( $ucfull = ${filename.toUpperCase().replace('-', '_')} )
#set( $extidx = ${ucfull.lastIndexOf(".")} )
#set( $extstart = $extidx + 1 )
#if( $extidx > -1 )
#set( $ucname = ${ucfull.substring(0,$extidx)} )
#set( $ucext = ${ucfull.substring($extstart)} )
#else
#set( $ucname = $!{ucfull} )
#set( $ucext = ${ext.toUpperCase()} )
#end
${ucname}_${ucext}##
#end##
Change back to the Files tab, and find the C Header File or C++ Class Header file depending on which language you're looking to update.
Change the contents of this file template to:
#parse("IncludeGuard.h")##
#set( $blank = "" )
#[[#ifndef]]# #includeGuard(${NAME} "h")${blank}
#[[#define]]# #includeGuard(${NAME} "h")${blank}
// ...
#[[#endif]]# // #includeGuard(${NAME} "h")
If everything works as intended, attempting to create a C Header File using the name test-include-guard or test-include-guard.h should both result in the following:
#ifndef TEST_INCLUDE_GUARD_H
#define TEST_INCLUDE_GUARD_H
// ...
#endif /* TEST_INCLUDE_GUARD_H */
Few notes:
If you need to change the file extension, change the includeGuard(${NAME} "h") parts to use whatever extension you want for the second parameter. The template will attempt to parse the file extension from ${NAME}, but ${NAME} only contains the file extension if you explicitly enter it into the new filename dialog.
The current state of whitespace handling in the Velocity templates used by CLion is a shit show, so you'll need to work around this as I did if you decide to further customize the templates. General guidelines:
If you're experiencing undesired linebreaks, you'll want to try adding a terminating line comment ## to the ends of lines before it.
If you find yourself in the oppostie scenario, (missing an expected linebreak) you can work around this with the #set( $blank = "" ) strategy I utilized above.
Most of the IntelliJ-based IDEs seem to cache the compilation of Include templates the first time they get passed into #parse(). If you make changes to an Include template after this point, you'll usually need to use the File > Invalidate Caches/Restart menu command before the changes propagate.
Settings->Editor->File and Code Templates->Files
change ${INCLUDE_GUARD} into _${NAME}_H_
For example, if your file name is: clion.h, then _${NAME}_H_ is rendered as _clion_H_, because ${NAME} is rendered as the filename (without extension).
File | Settings | Editor | File and Code Templates for Windows and Linux
CLion | Preferences | Editor | File and Code Templates for OS X
#[[#ifndef]]# BASE_${HEADER_FILENAME}
#[[#define]]# BASE_${HEADER_FILENAME}
#[[#endif]]# //BASE_${HEADER_FILENAME}
>
#ifndef BASE_test_h
#define BASE_test_h
#endif //BASE_test_h
select BASE_test_h and press CTRL + SHIFT + U to upper case
According to the latest doc (2019.3, but it may work in earlier versions, too) you can navigate to the Naming Convention tab under Settings / Preferences | Editor | Code Style | C/C++.
There you'll find a field that allows you to easily change the header guard pattern. No need to add custom templates anymore.

Zend include template file in another template

I want to include a template file in another template file so I can get variable values.
Like I have two files _template1.phtml and template2.phtml
In _template1.phtml I have:
$this->text[] = 'Text here';
In template2.phtml I want to access the value of "text". I tried
$this->render('folder/template2.phtml');
var_dump($text);
var_dump($this->text);
but neither of this works.
What I need is _template1 file would be included from the template so the scope of $this would be the view object.
Thanks for any help.

SugarCRM customization of Basic template

I need to add a field in basic template. Can anyone help me how can i add another field in include/SugarObjects/templates/basic/vardefs.php in upgrade safe manner.
In VardefManager's function addTemplate not like general standards of Sugar it is not requiring the custom paths
include/SugarObjects/VardefManager.php near line 107 SugarCE6.5.5:
if(empty($templates[$template])){
$path = 'include/SugarObjects/templates/' . $template . '/vardefs.php';
if(file_exists($path)){
require($path);
$templates[$template] = $vardefs;
}else{
$path = 'include/SugarObjects/implements/' . $template . '/vardefs.php';
if(file_exists($path)){
require($path);
$templates[$template] = $vardefs;
}
}
}
Really waiting for awesome responses.
Create a file at the path custom/include/SugarObjects/VardefManager.php with the name VardefManager.php and in that file include your mail file it is include/SugarObjects/VardefManager.php.
Here you will create a class with same and and create a function with the name
static function addTemplate
with same the arguments pass in the main file. and override the method here with your custom code (as you want to add some lines of code in that).
This will be upgrade safe and will be workable to you.