I have been using a bitwise comparison to check if entities and maptiles have flags in a roguelike game, but I've run into a problem - I need to check in an if() if a tile/ent doesn't have a flag, but I can't figure out how to do it without using an empty if() {} and else { condition; }, an example being:
if(Tile->Flags & TILE_INVIEW) {} else { attron(A_DIM); }
or
if(Tile->Flags & TILE_RENDER) {} else { SetTileFlags(GetTileFlags() + TILE_RENDER); }
Is there a cleaner way to do this?
Just reverse your condition:
if(!(Tile->Flags & TILE_INVIEW)) {
attron(A_DIM);
}
There is a not operator :)
Related
I have several functions that try and evaluate some data. Each function returns a 1 if it can successfully evaluate the data or 0 if it can not. The functions are called one after the other but execution should stop if one returns a value of 1.
Example functions look like so:
int function1(std::string &data)
{
// do something
if (success)
{
return 1;
}
return 0;
}
int function2(std::string &data)
{
// do something
if (success)
{
return 1;
}
return 0;
}
... more functions ...
How would be the clearest way to organise this flow? I know I can use if statements as such:
void doSomething(void)
{
if (function1(data))
{
return;
}
if (function2(data))
{
return;
}
... more if's ...
}
But this seems long winded and has a huge number of if's that need typing. Another choice I thought of is to call the next function from the return 0 of the function like so
int function1(std::string &data)
{
// do something
if (success)
{
return 1;
}
return function2(data);
}
int function2(std::string &data)
{
// do something
if (success)
{
return 1;
}
return function3(data);
}
... more functions ...
Making calling cleaner because you only need to call function1() to evaluate as far as you need to but seems to make the code harder to maintain. If another check need to be inserted into the middle of the flow, or the order of the calls changes, then all of the functions after the new one will need to be changed to account for it.
Am I missing some smart clear c++ way of achieving this kind of program flow or is one of these methods best. I am leaning towards the if method at the moment but I feel like I am missing something.
void doSomething() {
function1(data) || function2(data) /* || ... more function calls ... */;
}
Logical-or || operator happens to have the properties you need - evaluated left to right and stops as soon as one operand is true.
I think you can make a vector of lambdas where each lambdas contains specific process on how you evaluate your data. Something like this.
std::vector<std::function<bool(std::string&)> listCheckers;
listCheckers.push_back([](std::string& p_data) -> bool { return function1(p_data); });
listCheckers.push_back([](std::string& p_data) -> bool { return function2(p_data); });
listCheckers.push_back([](std::string& p_data) -> bool { return function3(p_data); });
//...and so on...
//-----------------------------
std::string theData = "Hello I'm a Data";
//evaluate all data
bool bSuccess = false;
for(fnChecker : listCheckers){
if(fnChecker(theData)) {
bSuccess = true;
break;
}
}
if(bSuccess ) { cout << "A function has evaluated the data successfully." << endl; }
You can modify the list however you like at runtime by: external objects, config settings from file, etc...
This might be a non-sense question, but i'm kind of stuck so I was wondering if someone can help. I have the following code:
bool while_condition=false;
do{
if(/*condition*/){
//code
}
else if(/*condition*/){
//code
}
else if(/*condition*/){
//code
}
...//some more else if
else{
//code
}
check_for_do_while_loop(while_condition, /*other parameters*/);
}while(while_condition);
the various if and else if exclude with each other but each have other if inside; if a certain condition is met (which can't be specified in a single if statement), then the code return a value and the do while loop is ended. But if, after entering a single else if, the conditions inside aren't met the code exit without actually doing nothing, and the while loop restart the whole.
I want the program to remember where he entered and avoid that part of the code, i.e. to avoid that specific else if he entered without any result, so he can try entering another else if. I thought about associating a boolean to the statements but I'm not quite sure on how to do it. Is there a way which allows me not to modify the code structure too much?
To give an idea of one way of approaching this that avoid loads of variables, here is an outline of how you might data-drive a solution.
class TestItem
{
public:
typedef bool (*TestFuncDef)(const state_type& state_to_test, std::shared_ptr<result_type>& result_ptr);
TestItem(TestFuncDef test_fn_parm)
{
test_fn = test_fn_parm;
already_invoked = false;
}
bool Invoke(const state_type& state_to_test, std::shared_ptr<result_type>& result_ptr)
{
already_invoked = true;
return test_fn(state_to_test, result_ptr);
}
bool AlreadyInvoked() const {return already_invoked; }
private:
TestFuncDef test_fn;
bool already_invoked;
};
std::shared_ptr<result_type> RunTest(std::list<TestItem>& test_item_list, state_type& state_to_test)
{
for(;;) {
bool made_a_test = false;
for (TestItem& item : test_item_list) {
std::shared_ptr<result_type> result_ptr;
if (!item.AlreadyInvoked()) {
made_a_test = true;
if (item.Invoke(state_to_test, result_ptr)) {
return result_ptr;
}
else
continue;
}
}
if (!made_a_test)
throw appropriate_exception("No conditions were matched");
}
}
This is not supposed to be a full solution to your problem but suggests another way of approaching it.
The important step not documented here is to build up the std::list of TestItems to be passed to RunTest. Code to do so might look like this
std::list<TestItem> test_item_list;
test_item_list.push_back(TestItem(ConditionFn1));
test_item_list.push_back(TestItem(ConditionFn2));
The definition of ConditionFn1 might look something like
bool ConditionFn1(const state_type& state_to_test, std::shared_ptr<result_type>& result_ptr)
{
// Do some work
if (....)
return false;
else {
result_ptr.reset(new result_type(some_args));
return true;
}
}
The title may not be right because I can't find an appropriate words for it.
I want to add conditions to some object (instance created by class).
In obj.h: (excluding preprocessor commands)
class obj {
public:
void addCondition( bool cond );
void useCondition();
private:
bool conditions;
};
In obj.cpp: (excluding preprocessor commands)
void obj::addCondition( bool cond )
{
//What to write here, is the question in short!!!
}
void obj::useCondition()
{
if(conditions)
{
//Do something...
}
}
Suppose that the conditions was:
conditions = value1 > value2;
I wanted to 'ADD' a condition in conditions so, that it becomes something like that:
conditions = (value1 > value2) || (value3 <= value4);
OR
conditions = (value 1 > value2) && (value3 <= value4);
If I am wrong about something in asking things, I am sorry! If you know something other than the answer but the whole different thing that does the same thing, don't hesitate to discuss it.
Thanks in advance!
I assume you know why conditions field and condition parameter are both simple boolean variables. If that is true, it could be very simple, but you should replace addCondition with andCondition and orCondition :
void obj::andCondition( bool cond )
{
conditions = conditions && condition;
}
void obj::orCondition( bool cond )
{
conditions = conditions || condition;
}
And you should define whether conditions is initialy true or false. You can always set it to a defined value, because with code above :
obj.andCondition(false);
sets conditions to false, and
obj.orCondition(true);
sets conditions to true
Edit per comments:
The above answer is based on the requirement that conditions is a simple boolean variable, and condition is a simple boolean value.
Here is one example of what could be done if you want to re-evaluate the condition.
A class and-ing (resp. or-ing) conditions represented by boolean variables evaluated at the moment where useCondition is used :
class andcond {
std::list<bool *> conditions;
public:
void addCondition(bool &condition) {
conditions.push_back(&condition);
}
bool isCondition();
};
bool andcond::isCondition() {
bool retval = true;
for (std::list<bool *>::iterator it = conditions.begin(); it != conditions.end(); it++) {
retval = retval && **it;
}
return retval;
}
int main() {
bool a=false, b=true;
andcond c;
c.addCondition(a);
c.addCondition(b);
std::cout << c.isCondition() << std::endl; // is false here
a = true;
std::cout << c.isCondition() << std::endl; // is true here
return 0;
}
Note : conditions is a list of pointers to boolean variables that can be re-evaluated
You could even be more genereral by defining a full hierarchy of condition classes implementing a bool eval() method, for example the equality or inequality between 2 variables, and combinable by and and or. But it is way too complex for a disgression on an initial answer on SO. But you can try to implement this idea and ask more precise questions here when stuck ...
I've come across a situation where I have a bunch of "systems" that need to be initialized in sequence, with the next system only being initialized if all of the proceeding systems initialized successfully.
This has led me to a whole slew of nested if - else statements. Here's some pseudo-code for visualization.
bool mainInit () {
if (!system1Init ()) {
reportError (); // some error reporting function
}
else {
if (!system2Init ()) {
reportError ();
}
else {
if (!system3Init ()) {
// ... and so on
I find that this starts to look like a mess when you get even a handful of levels to it.
Now I thought of using a switch statement instead, starting at the first case and falling through to the other cases on success, only breaking if there's an error.
bool mainInit () {
switch (1) {
case 1:
if (!system1Init ()) {
reportError ();
break;
}
case 2:
if (!system2Init ())
reportError ();
break;
}
// ....
}
Now, I like this a lot better. I find it much easier to read, especially with some decent comments, but I'm fairly new to programming.
So, my question is: Seeing how this is not how switch statements are traditionally used(at least from what I've seen), is something like this acceptable, or would this be considered bad form?
Being new to programming, I'm trying not to develop too many bad habits that might frustrate and make things more difficult for other programmers down the road.
I did a search, but most of what I found had to do with replacing chains of if - else if statements, not replacing nested ones.
Reference all of the systems in an array, for example an std::vector<mySystem*>, and loop over them sequentially, breaking off on the first fail. This way your entire code is reduced to less than 5 lines of code, even for 500+ systems.
The suggested switch hack is an evil example of XY problem solving: your real problem is that you don't have the array of systems, and are using named variables, thus eliminating all options to more flexibly use all systems, like in a loop.
Assuming that all your system#Init() calls are known at compile time, you can very easily put them in a table and then iterate over that table.
typedef (*system_init)(void);
system_init initialization_functions[] =
{
system1Init,
system2Init,
system3Init,
...
systemNInit
};
bool mainInit()
{
for(size_t idx(0); idx < sizeof(initialization_functions) / sizeof(initialization_functions[0]); ++idx)
{
if(!initialization_functions[idx]())
{
ReportError();
return false;
}
}
return true;
}
However, your existing code looks incorrect since the first mainInit() only calls system1Init() and then exits. Probably not what you wanted in the first place.
if(!system1Init())
{
ReportError();
return false;
}
// if you add an else, the system2Init() does not get called
// even if system1Init() succeeds
if(!system2Init())
{
ReportError();
return false;
}
[...]
return true;
Would the switch answer your problem? Not as it was written. That is, if you wanted to call the mainInit() function with a counter, it could be useful. Drupal uses that mechanism:
bool mainInit(int idx)
{
bool r(true);
switch(idx)
{
case 1:
r = system1Init();
break;
case 2:
r = system2Init();
break;
[...]
}
if(!r)
{
ReportError();
}
return r
}
Note that the table mechanism works the same way as the switch. As long as all the code is found in the systemNInit() functions (and it should be), the switch does not add anything, so you could do something like this too:
bool mainInit(int idx)
{
if(idx < 0 || idx >= sizeof(initialization_functions) / sizeof(initialization_functions[0]))
{
throw std::range_error("index out of bounds");
}
if(!initialization_functions[idx]())
{
ReportError();
return false;
}
return true;
}
Calling the mainInit() with an index can be helpful in case you want to "de-initialize" properly:
int main()
{
for(size_t idx(0); idx < ...; ++idx)
{
if(!mainInit(idx))
{
while(idx > 0)
{
--idx;
mainDeinit(idx);
}
exit(1);
}
}
...app do something here...
}
Use custom exceptions with clear error messages and add a try-catch-report-die around the code in main(). Exceptions are there to specifically make your case look good by making "bad path" implicit.
void initX() { ...; throw std::invalid_argument_exception("..."); }
int main() {
try {
init1(); init2(); ... run();
return 0;
} catch (std::exception const& e) {
log(e.what()); exit 42;
}
}
I'd do it this way:
bool mainInit () {
if (!system1Init ()) {
return(false);
}
if (!system2Init ()) {
return(false);
}
if (!system3Init ()) {
return(false);
}
//...
return(true);
}
//...
if(!mainInit()) {
reportError();
}
I want a boolean assigned to a method so then depending on the ouput of that method i know whether to execute a following method or not, but it does not seem to like the way i am doing it.
Code is below
bool ok = (boost::regex_match(str1,string_matcher))
{
DCS_LOG_DEBUG("Correct Number of Passengers");
output.push_back("Correct Number of Passengers\n");
DCS_LOG_DEBUG("2nd loop done 1");
}
else
{
a = st[0];
boost::regex const string_matcher1(splitMask[1]);
boost::trim(a);
if(boost::regex_match(a,string_matcher1))
{
DCS_LOG_DEBUG("Correct format for surnamce");
output.push_back("Correct format for surnamce\n");
DCS_LOG_DEBUG("2nd loop done 4");
}
else
{
DCS_LOG_DEBUG("Invalid format for surname");
output.push_back("Invalid format for surname\n");
DCS_LOG_DEBUG("2nd loop done 5");
}
}
What am i doing wrong there or is it not possible? would be happy for any help :)
You are missing an if:
if (boost::regex_match(str1,string_matcher))
{ ...
OR:
bool ok = boost::regex_match(str1,string_matcher);
if (ok)
{ ...