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