I don't really want to use <bitset> because I only want to use a unsigned char for the flags, not a ulong (I could cast but that's only for desperation!), sorry for not asking this on a C forum, but I want to believe that I am just doing this wrong, and could be done better. (I looked at bitset for a bit and I find that it doesn't really do anything special other than putting the bit operators into functions). I have a hunch that maybe the bit shifting operator is the key, but my brain just doesn't click...
The all caps words are in a enum with values that are a multiple of 2. IE: 1, 2, 4, 8. log_flags is a char, and it has a few default flags.
Just to make this clear, this code works 100%, but I am not happy with how it looks, and I want to believe it could be shorter (as in more easier to be read).
I'm assuming that find<bool> sets l_general, etc., to whatever it finds in the options file but if it doesn't appear in the options file then he wants it to retain it's default value as given in log_flags on input. So the final expression is only using the bitmasks of the values that changed, the others being set to 0.
Depending on what you are doing, a bitfield has merits (not here) and you can also use an enum to hold some common bit masks, eg if FlagAndDebug&mybitsint) instead of (if mybitsint &1 && mybitsint&2) or the like. Dunno if that would make it any more 'elegant' or not.
If I understand this right, you have a default set of bits in log_flag. The defaults can be overridden by the appropriate settings in option_file. If that's right then the solutions so far won't work right because they don't distinguish between "the options file says false" and "the options file has no entry for this keyword."
To handle this, I'd have option_file<>.find() work like this:
1 2 3
// Find an option in the option file. If found then return true and set value to the found value.
// Otherwise return false and leave value unchanged.
bool option_file.find<T>(constchar *keyword, T&value);
Then to read the log flags, you can use a short table and a loop:
You are right, I forgot to test what happens when I remove the option flags, it seems to give the opposite results that my defaults have. I guess I completely ignored why did I use those ternary operators in the first place!