enumset.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. ******************************************************************************
  3. *
  4. * Copyright (C) 2012,2014 International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. ******************************************************************************
  8. */
  9. /**
  10. * \file
  11. * \brief C++: internal template EnumSet<>
  12. */
  13. #ifndef ENUMSET_H
  14. #define ENUMSET_H
  15. #include "unicode/utypes.h"
  16. #if U_SHOW_CPLUSPLUS_API
  17. U_NAMESPACE_BEGIN
  18. /* Can't use #ifndef U_HIDE_INTERNAL_API for the entire EnumSet class, needed in .h file declarations */
  19. /**
  20. * enum bitset for boolean fields. Similar to Java EnumSet<>.
  21. * Needs to range check. Used for private instance variables.
  22. * @internal
  23. */
  24. template<typename T, uint32_t minValue, uint32_t limitValue>
  25. class EnumSet {
  26. public:
  27. inline EnumSet() : fBools(0) {}
  28. inline EnumSet(const EnumSet<T,minValue,limitValue>& other) : fBools(other.fBools) {}
  29. inline ~EnumSet() {}
  30. #ifndef U_HIDE_INTERNAL_API
  31. inline void clear() { fBools=0; }
  32. inline void add(T toAdd) { set(toAdd, 1); }
  33. inline void remove(T toRemove) { set(toRemove, 0); }
  34. inline int32_t contains(T toCheck) const { return get(toCheck); }
  35. inline void set(T toSet, int32_t v) { fBools=(fBools&(~flag(toSet)))|(v?(flag(toSet)):0); }
  36. inline int32_t get(T toCheck) const { return (fBools & flag(toCheck))?1:0; }
  37. inline UBool isValidEnum(T toCheck) const { return (toCheck>=minValue&&toCheck<limitValue); }
  38. inline UBool isValidValue(int32_t v) const { return (v==0||v==1); }
  39. inline const EnumSet<T,minValue,limitValue>& operator=(const EnumSet<T,minValue,limitValue>& other) {
  40. fBools = other.fBools;
  41. return *this;
  42. }
  43. inline uint32_t getAll() const {
  44. return fBools;
  45. }
  46. #endif /* U_HIDE_INTERNAL_API */
  47. private:
  48. inline uint32_t flag(T toCheck) const { return (1<<(toCheck-minValue)); }
  49. private:
  50. uint32_t fBools;
  51. };
  52. U_NAMESPACE_END
  53. #endif /* U_SHOW_CPLUSPLUS_API */
  54. #endif /* ENUMSET_H */