Reputation: 69893
I was just fooling around with some Domain Specific Language designs for a new project in C/C++ when I thought up this "odd" solution:
define DSL(...) MakeCommand(#__VA_ARGS__\
)->Exec()->GetResults()
MyResults results = DSL( for p in people do something );
The nice part is this is correct by the standards (but so is a Duff Switch), and cross-platform, portable, etc... However this method is really not any better than writing strings into code, but since the DSL engine parses strings anyways, it seems to look prettier this way, and reduces the clutter. But was wondering what do other folk think about it.
Thanks
Upvotes: 5
Views: 2020
Reputation: 507273
Hmm, while variadic macros are C99, they are not possible in C++. I wouldn't do it like that :) A simple dsl function taking a std::string
or whatever string class your framework uses, and returning MakeCommand(str)->Exec()->GetResults()
would be my preferred option, since it's more debug friendly, and you can put it into a namespace.
You will also be able to build up command strings dynamically and pass it to the function. Using your macro approach, passing a str
variable would actually pass "str"
to the MakeCommand function. You would need another macro for dynamic commands to make it work, which i wouldn't be comfortable with.
Upvotes: 6