jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ****************************************************************************** jpayne@69: * jpayne@69: * Copyright (C) 2012,2014 International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: * jpayne@69: ****************************************************************************** jpayne@69: */ jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++: internal template EnumSet<> jpayne@69: */ jpayne@69: jpayne@69: #ifndef ENUMSET_H jpayne@69: #define ENUMSET_H jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: /* Can't use #ifndef U_HIDE_INTERNAL_API for the entire EnumSet class, needed in .h file declarations */ jpayne@69: /** jpayne@69: * enum bitset for boolean fields. Similar to Java EnumSet<>. jpayne@69: * Needs to range check. Used for private instance variables. jpayne@69: * @internal jpayne@69: * \cond jpayne@69: */ jpayne@69: template jpayne@69: class EnumSet { jpayne@69: public: jpayne@69: inline EnumSet() : fBools(0) {} jpayne@69: inline EnumSet(const EnumSet& other) : fBools(other.fBools) {} jpayne@69: inline ~EnumSet() {} jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: inline void clear() { fBools=0; } jpayne@69: inline void add(T toAdd) { set(toAdd, 1); } jpayne@69: inline void remove(T toRemove) { set(toRemove, 0); } jpayne@69: inline int32_t contains(T toCheck) const { return get(toCheck); } jpayne@69: inline void set(T toSet, int32_t v) { fBools=(fBools&(~flag(toSet)))|(v?(flag(toSet)):0); } jpayne@69: inline int32_t get(T toCheck) const { return (fBools & flag(toCheck))?1:0; } jpayne@69: inline UBool isValidEnum(T toCheck) const { return (toCheck>=minValue&&toCheck& operator=(const EnumSet& other) { jpayne@69: fBools = other.fBools; jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: inline uint32_t getAll() const { jpayne@69: return fBools; jpayne@69: } jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: private: jpayne@69: inline uint32_t flag(T toCheck) const { return (1<<(toCheck-minValue)); } jpayne@69: private: jpayne@69: uint32_t fBools; jpayne@69: }; jpayne@69: jpayne@69: /** \endcond */ jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: #endif /* ENUMSET_H */