flags.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "flags.h"
  2. /** Simple bit twiddling functions for setting and getting
  3. flags (bits) on an integer field. */
  4. /** Sets an individual flag on self. */
  5. int flags_set(int * self, int flag) {
  6. return (*self) = (*self) | flag;
  7. }
  8. /** Returns the new flag that self should be set to for
  9. flag to be set in self. */
  10. int flags_setvalue(int self, int flag) {
  11. return (self) | flag;
  12. }
  13. /** Unsets an individual flag on the widget. */
  14. int flags_unset(int * self, int flag) {
  15. return (*self) = (*self) & (~flag);
  16. }
  17. /** Returns the new flag that self should be set to for
  18. flag to be unset in self. */
  19. int flags_unsetvalue(int self, int flag) {
  20. return (self) & (~flag);
  21. }
  22. /** Sets or unsets an individual flag on the self.
  23. If set is true the flag is set, if false it's unset. */
  24. int flags_put(int * self, int flag, int set) {
  25. if (set) { return flags_set(self, flag); }
  26. else { return flags_unset(self, flag); }
  27. }
  28. /** Sets or unsets an individual flag on the self.
  29. If set is true the flag is set, if false it's unset. */
  30. int flags_putvalue(int self, int flag, int set) {
  31. if (set) { return flags_setvalue(self, flag); }
  32. else { return flags_unsetvalue(self, flag); }
  33. }
  34. /** Checks if an individual flag is set. */
  35. int flags_get(int self, int flag) {
  36. return (self & flag) == flag;
  37. }
  38. /** Checks if an individual flag is set. Pass in a pointer. */
  39. int flags_getptr(int * self, int flag) {
  40. return ((*self) & flag) == flag;
  41. }