Common header file for C++ and JavaScipt

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");

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật