bool OrderUtils::shouldCptyAutoexecute(int Id)
{
bool res =
dummyCache::instance().getVal(Id).getWhitelabelType() == "ABCD";
if (!res)
res |= dummyCache::instance().getVal(Id).getIsPlc() == 1;
return res;
}
The above code checks for 2 Id’s and returns true to res if any of the id is present in the database.
Can you suggest a way in which I can compare one more value from the databse table and return true to the value res..Also can you explain what does the second if statement do and the bitwise OR operator?
Sir, just let the short-circuit eval do this for you:
If the first is true, the second will not fire. Moreover, I assure you a remotely-reasonable optimizing compiler will not re-fire
instance().getVal(id)if the returned object has not changed between thegetWhitelabelType()andgetisPlc()calls. In fact, i would all-but-guarantee it ifgetWhiteLabelType()isconst. (and it certainly looks like it should be).Regarding the bit work. The expression was pretty-much whacked. though it will work. Unless I read it wrong (and trust me, the list of people that will tell me I am will let me know quickly) it is performing a boolean eval, promoting the resulting true/false
boolto anint, promoting the current value ofresfrombooltoint(which is zero, so nothing special there), bitwise-OR-ing that with the expressionint, then demoting the final int back to a bool to store inres. I’m surprised this doesn’t flag at least a warning on the compiler.It likely should have been
if (!res) res ||= expr, and even then, it is pointless, as you can just use short circuit eval as in the top of this answer to cut out the localresentirely. Consider ifreswerefalse. Then the equivalent expression would beres = false || expr. But thats justres = expr. In the!resstate it executes in, you may as well just use an assignment.Finally, regarding adding a third field to your eval, it depends entirely on how you want it added. for an additional logical OR it is pretty simple.
For an expression like (A || B || C) you can just
For more complex operations, some judicious use of parenthesis will go a long way. For example, to return
(A || B) && C:Or perhaps
(A && C) || (B && !C)(ok this is getting a little overboard…)