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