Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions code-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,20 @@ class CppCodeGenerator {
*/
writeBodySkeletonCode(elem, options, funct) {
var codeWriter = new codegen.CodeWriter(this.getIndentString(options));

// Check if the "Docs in headerfile only" option is active
var suppressDocs = options && options.docsInHeadersOnly === true;
this._suppressBodyDocs = suppressDocs;

codeWriter.writeLine(copyrightHeader);
codeWriter.writeLine();
codeWriter.writeLine('#include "' + elem.name + '.h"');
codeWriter.writeLine();
funct(codeWriter, elem, this);

// Reset for safety, this should not leak to other codegen
// but each time a .cpp is encountered it is set again.
this._suppressBodyDocs = false;
return codeWriter.getData();
}

Expand Down Expand Up @@ -572,10 +581,10 @@ class CppCodeGenerator {
if (_modifiers.length > 0) {
terms.push(_modifiers.join(" "));
}
// type
terms.push(this.getType(elem));
// name
terms.push(elem.name);
// type and name (apply name-based array to pointer conversion if enabled)
var _norm = this.normalizeArrayNameAndType(elem, this.getType(elem));
terms.push(_norm.type);
terms.push(_norm.name);
// initial value
if (elem.defaultValue && elem.defaultValue.length > 0) {
terms.push("= " + elem.defaultValue);
Expand Down Expand Up @@ -613,10 +622,12 @@ class CppCodeGenerator {
var inputParamStrings = [];
for (i = 0; i < inputParams.length; i++) {
var inputParam = inputParams[i];
inputParamStrings.push(
this.getType(inputParam) + " " + inputParam.name,
var _normParam = this.normalizeArrayNameAndType(
inputParam,
this.getType(inputParam),
);
docs += "\n@param " + inputParam.name;
inputParamStrings.push(_normParam.type + " " + _normParam.name);
docs += "\n@param " + _normParam.name;
}

methodStr +=
Expand Down Expand Up @@ -689,6 +700,11 @@ class CppCodeGenerator {
* @return {Object} string
*/
getDocuments(text) {
// When generating .cpp body and docsInHeadersOnly is enabled, skip docs
if (this._suppressBodyDocs === true) {
return "";
}

var docs = "";
if (typeof text === "string" && text.length !== 0) {
var lines = text.trim().split("\n");
Expand Down Expand Up @@ -740,6 +756,34 @@ class CppCodeGenerator {
return modifiers;
}

/**
* Normalize name-based static array declarations (e.g., myList[], myList[10])
* into pointer types when the option convertBracketArrayToPointer is enabled.
* Returns adjusted type and name.
*
* @param {Object} elem
* @param {string} typeStr
* @return {{type: string, name: string}}
*/
normalizeArrayNameAndType(elem, typeStr) {
var shouldConvert =
this.genOptions && this.genOptions.convertBracketArrayToPointer === true;
var nameStr = typeof elem.name === "string" ? elem.name : "";
if (!shouldConvert || nameStr.indexOf("[") === -1) {
return { type: typeStr, name: nameStr };
}

var matches = nameStr.match(/\[[^\]]*\]/g);
if (!matches || matches.length === 0) {
return { type: typeStr, name: nameStr };
}

var pointerDepth = matches.length;
var cleanedName = nameStr.replace(/\[[^\]]*\]/g, "");
var adjustedType = typeStr + new Array(pointerDepth + 1).join("*");
return { type: adjustedType, name: cleanedName };
}

/**
* parsing type from element
*
Expand Down
4 changes: 4 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ function getGenOptions() {
useTab: app.preferences.get("cpp.gen.useTab"),
indentSpaces: app.preferences.get("cpp.gen.indentSpaces"),
useVector: app.preferences.get("cpp.gen.useVector"),
convertBracketArrayToPointer: app.preferences.get(
"cpp.gen.convertBracketArrayToPointer",
),
includeHeader: app.preferences.get("cpp.gen.includeHeader"),
genCpp: app.preferences.get("cpp.gen.genCpp"),
docsInHeadersOnly: app.preferences.get("cpp.gen.docsInHeadersOnly"),
};
}

Expand Down
12 changes: 12 additions & 0 deletions preferences/preference.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@
"type": "check",
"default": true
},
"cpp.gen.convertBracketArrayToPointer": {
"text": "Convert [] arrays in names to pointers",
"description": "If an attribute or parameter name includes brackets (e.g., myList[]), generate a pointer type (e.g., int* myList) instead of a static array.",
"type": "check",
"default": false
},
"cpp.gen.genCpp": {
"text": "Generate *.cpp file",
"description": "Generate cpp file",
"type": "check",
"default": true
},
"cpp.gen.docsInHeadersOnly": {
"text": "Documentation only in headers",
"description": "Suppress Doxygen comments in .cpp and generate them only in .h files.",
"type": "check",
"default": true
},
"cpp.rev": {
"text": "C++ Reverse Engineering",
"type": "section"
Expand Down