Skip to content

Instantly share code, notes, and snippets.

@croepha
Last active November 28, 2017 07:15
Show Gist options
  • Save croepha/bfe66502817b4e6ae0bc9f5a8a2eaffb to your computer and use it in GitHub Desktop.
Save croepha/bfe66502817b4e6ae0bc9f5a8a2eaffb to your computer and use it in GitHub Desktop.
/*
Example output:
proc:
Got opt1
proc:
Got opt1
Got opt3
proc:
Got opt1
Got opt2
Got opt4
proc:
Got opt1
Got opt4
proc:
Got opt1
Got opt3
Got opt4
I think this requires c++11
To help know the meaning of true vs false, use the option name for that.
like .overwrite=1 means that we overwrite or .overwrite=0 means that we don't
If you want the meaning of true or false to be the opposite then you
would do .no_overwrite=0 for yes we want to overwrite
*/
#include <stdio.h>
struct proc_flags_t { // this is a bit field construct sizeof(proc_flags_t) == sizeof(unsigned char)
unsigned char opt1:1,opt2:1,opt3:1,opt4:1;
};
// use proc_flags_t&&flags if you want to pass a pointer (actually a reference, but same thing)
void proc(proc_flags_t flags) {
printf("proc:\n");
if (flags.opt1) printf(" Got opt1\n");
if (flags.opt2) printf(" Got opt2\n");
if (flags.opt3) printf(" Got opt3\n");
if (flags.opt4) printf(" Got opt4\n");
}
int main() {
proc({ .opt1=1 });
proc({ .opt1=1, .opt3=1 });
proc({ .opt1=1, .opt2=1, .opt4=1 });
proc({ .opt1=1, .opt2=0, .opt4=1 });
proc({ 1, 0, 1, 1 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment