Skip to content

Commit f5aa089

Browse files
committed
cpp generator for enums
1 parent 0ee2ca3 commit f5aa089

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

js/scripts/generate-enums.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ const baseDir = path.resolve(scriptDir, '..');
1212

1313
const jsSrcDir = path.resolve(baseDir, 'src/');
1414
const pySrcDir = path.resolve(baseDir, '..', 'pythreejs');
15+
const cppSrcDir = path.resolve(baseDir, '..', 'xthreejs/xthreejs');
1516
const templateDir = path.resolve(scriptDir, 'templates');
1617

1718
const threeSrcDir = path.resolve(baseDir, 'node_modules', 'three', 'src');
1819

1920
const jsEnumDst = path.resolve(jsSrcDir, '_base', 'enums.js');
2021
const pyEnumDst = path.resolve(pySrcDir, 'enums.py');
22+
const cppEnumDst = path.resolve(cppSrcDir, '_base', 'enums.hpp');
2123

2224
//
2325
// Actual THREE constants:
@@ -47,6 +49,7 @@ function compileTemplate(templateName) {
4749

4850
var jsEnumTemplate = compileTemplate('js_enums');
4951
var pyEnumTemplate = compileTemplate('py_enums');
52+
var cppEnumTemplate = compileTemplate('cpp_enums');
5053

5154

5255
//
@@ -150,11 +153,45 @@ function createPythonFiles() {
150153
});
151154
}
152155

156+
function writeCppFile() {
157+
// Here we generate lists of enum keys
158+
159+
var categories = [];
160+
161+
_.keys(enumConfigs).map(category => {
162+
var categoryObj = {key: category, enums: []};
163+
categories.push(categoryObj);
164+
enumConfigs[category].forEach(function(enumKey) {
165+
if (Array.isArray(enumKey)) {
166+
// Several keys share the same value, use the first one.
167+
enumKey = enumKey[0];
168+
}
169+
categoryObj.enums.push({ key: enumKey, value: threeEnums[enumKey] });
170+
}, this);
171+
}, this);
172+
173+
var content = cppEnumTemplate({
174+
now: new Date(),
175+
generatorScriptName: path.basename(__filename),
176+
177+
categories: categories
178+
});
179+
180+
return fse.outputFile(cppEnumDst, content);
181+
}
182+
183+
function createCppFiles() {
184+
return new Promise(function(resolve) {
185+
resolve(writeCppFile());
186+
});
187+
}
188+
153189
function generateFiles() {
154190

155191
return Promise.all([
156192
createJavascriptFiles(),
157193
createPythonFiles(),
194+
createCppFiles(),
158195
checkUnused(),
159196
]);
160197

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef XTHREE_ENUMS_HPP
2+
#define XTHREE_ENUMS_HPP
3+
4+
//
5+
// This file auto-generated with {{ generatorScriptName }}
6+
// Date: {{ now }}
7+
//
8+
9+
#include "xwidgets/xeither.hpp"
10+
11+
namespace xthree{
12+
namespace xenums{
13+
{{#each categories as |category|}}
14+
15+
auto {{ category.key }} = XEITHER(
16+
{{#each category.enums as |enum|}}
17+
"{{ enum.key }}",
18+
{{/each}}
19+
);
20+
{{/each}}
21+
}
22+
}
23+
#endif

js/src/_base/enums.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// This file auto-generated with generate-enums.js
3-
// Date: Wed Nov 15 2017 17:14:48 GMT+0100 (W. Europe Standard Time)
3+
// Date: Thu Jan 25 2018 04:46:33 GMT+0100 (CET)
44
//
55

66

0 commit comments

Comments
 (0)