IMHO, your better create a member function or an operator for the struct, like shown by NathanOliver, that operates on the booleans in there. In fact, your could create at least two member functions, one that tells you whether any of the booleans is true, and other that tells you if all are true.
My approach, as this will probably need to be extendable in some sense in a normal project in the future, is to use a vector
of booleans, or better a map
, so that each boolean can be given a name, and then two functions (methods if they belong to a bigger class, like a configuration entity) that provide the all/any computation on those flags.
A quick and dirty example of this approach is shown below (compile with C++11 activated, for the auto
loop, or modify otherwise):
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<string, bool> Flags;
bool allOK(Flags & fl) {
bool result = true;
for (auto & kv : fl) { result &= kv.second; }
return result;
}
bool anyOK(Flags & fl) {
bool result = false;
for (auto & kv : fl) { result |= kv.second; }
return result;
}
int main(int argc, char * arg[])
{
Flags flags;
flags["a"] = true;
flags["b"] = true;
flags["the_third_flag"] = false;
cout << "1. ALL OK: " << boolalpha << allOK(flags)
<< " - ANY OK: " << anyOK(flags) << '\n';
flags["the_third_flag"] = true;
cout << "2. ALL OK: " << boolalpha << allOK(flags)
<< " - ANY OK: " << anyOK(flags) << '\n';
flags["a"] = false;
flags["b"] = false;
flags["the_third_flag"] = false;
flags["a_last_flag"] = false;
cout << "3. ALL OK: " << boolalpha << allOK(flags)
<< " - ANY OK: " << anyOK(flags) << '\n';
return 0;
}