I have an app that runs a C++ server backend and Javascript on the client. I would like to define certain strings once only, for both pieces of code. For example, I might have a CSS class “row-hover” – I want to define this class name in one place only in case I change it later.
Is there an easy way to include, or read, some sort of common definitions file into both C++ and JavaScript? Ideally as a compile / preprocessing step but any neat approach good.
2
I had proposed a quite similar approach here: https://stackoverflow.com/questions/4248831/shared-config-file-between-php-and-c/4248881#4248881 for a similar issue.
I would suggest to write a small script that generates code for C++ and JavaScript, with the configuration you want to have common. You can also use two templates in a template library, that you will run as part of your build process that will generate the appropriate files for each language.
Using a freemarker-like syntax (although I do not suggest including another language!), there could be something like this for JavaScript:
commonStuff = { config1: ${value1},
config2: ${value2}
}
and another template for the C++ implementation.
As suggested in the comments below, one good candidate for that, is to use sed, so that you would not have to introduce another language since your project has two already.
3
What you’re describing is what is commonly done with languages like JSP and PHP, injecting names defined on the server. If you cannot use these languages, then I don’t think it is worth the difficulty you’re attempting simply to avoid having to change class names in more than one spot. Granted, that is generally a best practice, but if it makes your code unnecessarily unreadable, you should avoid it like the plague.
C# is similar enough to C++ that the transition should be smooth, and C# has Asp support which allows you to define constants and drop them directly in the page.
However assuming that you don’t want to switch language, my advice is to do the next best thing: Have a C++ header and a css file which declare the same names. If you need to change the names, you need only change in these two places, and you’ve lost little extra time in doing so. Don’t lose sight that simplicity is your objective, not avoiding duplication of code, and in this case, I can’t possibly think that what you’re attempting is the simplest solution.
Generate the javascript based on the definitions in the C++ file. If you already have a template system in place for the HTML, you can probably reuse that to interpolate C++ values into the javascript.
If server load or performance is an issue, consider caching the javascript (both on the server and on the client), or precompiling it altogether (it’s a small step from a C++ program that serves dynamic javascript on-the-fly to one that saves it in a file instead).
You can use macros in C/C++ to change identifiers as needed. For javascript the obvious candidate is var
since it is only needed in javascript … it can be redefined as necessary to any c type.
c-header.h
//constant integers
#define var const int
#include "int-consts.js"
#undef var
//constant strings
#define var const char*
#include "string-consts.js"
#undef var
//arrays of strings
#define var const char**
#define new (const char*[])
#define Array(x,...) {x,__VA_ARGS__}
#include "string-arrays.js"
#undef new
#undef var
//and so forth for other types
int-consts.js
var swapMagicOffset = 4086;
string-consts.js
var swapMagic = "SWAP-SPACE";
var swapMagic2 = "SWAPSPACE2";
string-arrays.js
var cars = new Array("Ford", "Chevy", "Dodge");