Pasting formed an invalid processing token '.' - c++

I am trying to use macro for calling appropriate object based on the type.
#define DELEGATE_FUNC(FuncName, kind, paramPtr) \
if (kind == 1) { \
return PolicyObject1.##FuncName(paramPtr); \
} \
else { \
return PolicyObject2.##FuncName(paramPtr); \
} \
return 0; \
(PolicyObject1 & PolicyObject2 are two static objects.)
Now when using the macro, e.g.
DELEGATE_FUNC(ProcessPreCreate, 1, null_ptr);
It compiles fine in VS 2015, but gives error with LLVM "Pasting formed an invalid processing token '.ProcessPreCreate'"
I looked for, and found a few posts and understood it up to some level - Need double level of indirection, e.g.
Why do I need double layer of indirection for macros?
However I am unable to define those two layers of macro, can anyone help?
(Please leave aside the discussion on design aspects)
Thanks

When the compiler reads your C++ file, one of the first steps is dividing it into tokens like identifier, string literal, number, punctuation, etc. The C preprocessor works on these tokens, not on text. The ## operator glues tokens together. So, for example, if you have
#define triple(foo) foo##3
Then triple(x) will get you the identifier x3, triple(12) will get you the integer 123, and triple(.) will get you the float .3.
However, what you have is .##FuncName, where FuncName is ProcessPreCreate. This creates the single token .ProcessPreCreate, which is not a valid C++ token. If you had typed PolicyObject1.ProcessPreCreate directly instead of through a macro, it would be tokenized into three tokens: PolicyObject1, ., and ProcessPreCreate. This is what your macro needs to produce in order to give valid C++ output.
To do that, simply get rid of the ##. It is unnecessary to glue the . to the FuncName, because they are separate tokens. To check this, you can put a space between a . and a member name; it will still compile just fine. Since they are separate tokens, they should not and cannot be glued together.

delete "##".
#define DELEGATE_FUNC(FuncName, kind, paramPtr) \
if (kind == 1) { \
return PolicyObject1.FuncName(paramPtr); \
} \
else { \
return PolicyObject2.FuncName(paramPtr); \
} \
return 0; \

Related

Macro to create multiple similar identifiers for testing

I wanted to write a simple macro to expand some identifiers so it saves me the work of typing everything again and again when I have similar code to test for many different classes.
I wanted something like this:
#define TST(x) x## x##_1(2); \
x## x##_2; \
\
x##_1.print(cout); \
x##_2.print(cout); \
x##_2.elem(3); \
x##_2.elem(4); \
x##_2.print(cout)
To be translated into
Pentagonal Pentagonal_1(2);
Pentagonal Pentagonal_2;
Pentagonal_1.print(cout);
Pentagonal_2.print(cout);
Pentagonal_2.elem(3);
Pentagonal_2.elem(4);
Pentagonal_2.print(cout);
whenever I call
TST(Pentagonal);
so far it is being translated glued together like
PentagonalPentagonal_1
I tried searching for this but in this specific case I couldn't find much help elsewhere.
Change:
#define TST(x) x## x##_1(2); \
x## x##_2; \
...
to
#define TST(x) x x##_1(2); \
x x##_2; \
...
## is the token-paste operator: It "absorbs" surrounding whitespace and joins neighboring tokens into one.
The extras you had up there were pasting the Pentagonal and Pentagonal_1 together.

django : using Q objects

I have these 2 queries :
gifts = Products.objects \
.filter(entry_query,in_stock__icontains='A-in') \
.filter(~Q(title__icontains='Not Found'))
and
gifts1 = Products.objects \
.filter(('city__name__iregex', 'Delhi'),in_stock__icontains='A-in') \
.filter(~Q(title__icontains='Not Found'))
If I do gifts = gifts | gifts1 I get the following error:
error user-defined function raised exception
If i use Q objects like this:
gifts = Products.objects \
.filter((Q(entry_query) & Q(in_stock__icontains='A-in')) | Q(('city__name__iregex', 'Delhi'),in_stock__icontains='A-in') ) \
.filter(~Q(title__icontains = 'Not Found'))
I again get the same error
Here entry query is
(OR: ('title__iregex', u'bag'), ('description__iregex', u'bag'),('source_website_url__iregex', u'bag'))
Can someone please tell me where am I going wrong
The error in the first approach is because gifts1 and gifts are already QuerySets (try typeof(gifts)), so you cannot use | between them. It is like if you were using 'hi'|'goodbye'. You only use | for Q objects.
What you did the second time was almost correct, except the syntax: use | for OR and & for AND. The comma does not provide any logical operation on the Q, leading to a different argument on the filter, leading to an error.
Notice that both using & and joining filters are equivalent in Django (or are supposed to be), but your first approach of getting gift and gift1 as different QuerySets and evaluating them is different: that approach leads to two accesses (hits) to the database, while joining filters or Q objects in a single filter only hits it once.
As a rule of thumb, you should minimize the number of hits to the db.
Hope this helps

Concatenating two strings in macro definition (Clang)

I'm working on some code that was originally made in MSVC and I'm trying to get it to compile on Clang (using Xcode). The code I've got is something like this:
#define DO_MAPPING(x,y,z)\
myMaps[map##x] = GetTex( #x##"Map" );\
myRemaps[map##x] = GetHandle( #x##"Remap" );
Currently I'm getting a build error saying that pasting formed the string "Height""Map", where I really want "HeightMap". This code works on MSVC, is there something about Clang's macro syntax that means this kind of thing needs to be written differently?
In C, "X""Y" is equivalent to "XY". However, when you write such a thing in a macro:
str1##str2
you are telling the lexer to concat the two as one token. "X""Y" is actually two tokens that are concatenated by the lexer1, while "X"##"Y" is supposed to be one token (which is not a valid token).
What you need is to simply drop the ##:
#define DO_MAPPING(x,y,z)\
myMaps[map##x] = GetTex( #x "Map" );\
myRemaps[map##x] = GetHandle( #x "Remap" );
1 Or the semantics analyzer, depending on the implementation
Typically, the regex matching a string looks like this (simplified):
"(a|\b)*"
(assume a is a list of all characters that don't need to be escaped and b is the others). Probably, for MSVC, it is defined like this:
"(a|\b)*"s*+
(s is whitespace)
This means that MSVC probably sees "X" "Y" as one token instead of two.
Just use (#x "Map") in place of #x##"Map". It should work in C++. E.g. this is perfectly valid: ("B" "Map") and evaluates to "BMap".
Note that "Height""Map" actually is the same as "HeightMap" when parsed. So you can simply use this:
#define DO_MAPPING(x,y,z)\
myMaps[map##x] = GetTex( #x "Map" );\
myRemaps[map##x] = GetHandle( #x "Remap" );
Try this instead:
#define DO_MAPPING(x,y,z)\
myMaps[map##x] = GetTex( #x "Map" );\
myRemaps[map##x] = GetHandle( #x "Remap" );
Two string litterals are automatically concatenated by the compiler.

Makefile - loop through a list and select a specific value

I'm trying to do something like this (assuming $input is something provided by the user):
LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0
for prefix in $(LIST); do \
if $(input) == $(prefix) then
START = 1
endif \
if $(START) == 1 then \
if [ -f $(prefix)<file_name> ]; then <do_A>; else <do_B>; fi
endif \
done
my problem is with the two if's mentioned above. i don't know how can i choose a specific string value from a list while iterating it (if $(input) == $(prefix) then) and i don't know how to check if a value is 1 or 0 (if $(START) == 1 then).
My intent with this code is to use the same makefile for different directories which have the same file name, but with a different prefix. sometimes, a directory might contain multiple versions of the file with a different prefix and i want to define a hierarchy of those prefixes (defined by LIST in my example). when the user provide a version, the idea is to start searching for the most up-to date version, starting from the version he provides (e.g. if the user provide pre4, then i need to search pre4 first and if it's not exist - i'll go on and search for pre5 and so on. but in this example, i won't search for pre1 even if it do exist in the current directory).
Anyone has an idea on how can i do that?
Thanks in advance.
If that is supposed to be a command in a Makefile, the syntax would have to be something like this:
LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0
input = somename
file_name = whatever
some_target:
for prefix in $(LIST); do \
if test "$(input)" = $$prefix; then \
START=1; \
fi; \
if test "$(START)" = 1; then \
if test -f $$prefix$(file_name); then \
<do_A>; \
else \
<do_B>; \
fi; \
fi; \
done
But you didn't tell us what <input> and <file_name> are supposed to be, so I assumed they are other make variables. Basically the make rules look like one long shell line, with commands separated by semicolons, and lines continued with backslashes. $$ is replaced by make with a single $, which is why references to shell variables ($$prefix) need two dollars.
Your make manual (type man make has the whole story and is fun to read and understand.) Become a make guru today! Be sure to understand the difference between a make variable and a shell variable.

Creating simple calculator with bison & flex in C++ (not C)

I would like to create simple C++ calculator using bison and flex. Please note I'm new to the creating parsers. I already found few examples in bison/flex but they were all written in C.
My goal is to create C++ code, where classes would contain nodes of values, operations, funcs - to create AST (evaluation would be done just after creating whole AST - starting from the root and going forward).
For example:
my_var = sqrt(9 ** 2 - 32) + 4 - 20 / 5
my_var * 3
Would be parsed as:
=
/ \
my_var +
/ \
sqrt -
| / \
- 4 /
/ \ / \
** 32 20 5
/ \
9 2
and the second AST would look like:
*
/ \
my_var 3
Then following pseudocode reflects AST:
ast_root = create_node('=', new_variable("my_var"), exp)
where exp is:
exp = create_node(OPERATOR, val1, val2)
but NOT like this:
$$ = $1 OPERATOR $3
because this way I directly get value of operation instead of creation the Node.
I believe the Node should contain type (of operation), val1 (Node), val2 (Node). In some cases val2 would be NULL, like above mentioned sqrt which takes in the end one argument. Right?
It will be nice if you can propose me C++ skeleton (without evaluation) for above described problem (including *.y file creating AST) to help me understand the way of creating/holding Nodes in AST. Code can be snipped, just to let me get the idea.
I'll also be grateful if you point me to an existing (possibly simple) example if you know any.
Thank you all for your time and assistance!
http://www.progtools.org/compilers/tutorials/cxx_and_bison/cxx_and_bison.html is a mini-tutorial which should create something like what you want.