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