Instances of this class represents objects containing a set of booleans values (flags) that can be accessed by their symbolic name.
More...
template<unsigned int N>
class CLAM::Flags< N >
Instances of this class represents objects containing a set of booleans values (flags) that can be accessed by their symbolic name.
Because Flags derives from std::bitset<N> it provides all the standard operations like bitwise operators and so. This interface has been fattened to provide symbolic representation (name string) for each flag. If symbolic names for flags are not useful to you, consider use std::bitset instead which has a pure positional approach.
- Bitwise operators
- Several ways to access to individual flags
- Formated input and output to an std::stream as symbols with the insertion (<<) and extraction (>>) operators
- Implements the Component interface: StoreOn, DeepCopy, ShallowCopy...
- Runtime symbolic representation
- Runtime checking of the values
Accessing individual flags
You can acces an individual flag of the Flags object in different ways:
Access by name: | flags.bFlagName |
Access by indexation plus enums: | flags[MyFlagsClass::eFlagName] |
Access by symbol: | flags["FlagName"] |
You can use references this individual flags like any reference to a boolean. Moreover, you can use some extra operations like flip
that inverses the actual value of the flag:
bool isTrue= flags.bFlagName;
bool isFalse= ~(flags.bFlagName);
flags.bFlagName=true;
flags.bFlagName.flip();
Bitwise operations
Non Standard operations
Because older versions of this class didn't derive from bitfield, a different interface for a few functions was used. This old interface has the advantage that uses the same function name convention as the rest of the CLAM classes.
CLAMFlags | mtg::Flags | Comments |
Size | size | Returns the number of flags |
NSet | count | Counts the set flags |
Reset | reset | Sets to 0 all the flags |
NFlags | not present | Revise This!!!! |
operator int | operator int | The interface is unchanged but throws a |
Creating your own flags
The way to define a new Flags type is by subclassing Flags<N>
. The subclass MUST redefine its constructors by providing the Flags constructor an array of tFlagsValue's, which defines the mapping between numeric and symbolic values, and an initialization value, than can be both a symbol (char* or std::string) or an integer.
class MyFlags :
public Flags<5> {
public:
static tFlagValue sFlagValues[];
virtual Component *
Species()
const {
return new MyFlags();
}
typedef enum {
eFlag0=0,
eFlag1=1,
eFlag2=2,
eFlag3=3,
eFlag4=4
} tFlag;
MyFlags () :
flag0(operator[](eFlag0)),
flag1(operator[](eFlag1)),
flag2(operator[](eFlag2)),
flag3(operator[](eFlag3)),
flag4(operator[](eFlag4))
{
flag4=true;
};
MyFlags (const MyFlags& someFlags) :
Flags<5>(sFlagValues,someFlags),
flag0(operator[](eFlag0)),
flag1(operator[](eFlag1)),
flag2(operator[](eFlag2)),
flag3(operator[](eFlag3)),
flag4(operator[](eFlag4))
{};
template <class T>
MyFlags(const T &t) :
flag0(operator[](eFlag0)),
flag1(operator[](eFlag1)),
flag2(operator[](eFlag2)),
flag3(operator[](eFlag3)),
flag4(operator[](eFlag4))
{};
template <class T1, class T2>
MyFlags(const T1 &t1, const T2 &t2) :
Flags<5>(sFlagValues,t1,t2),
flag0(operator[](eFlag0)),
flag1(operator[](eFlag1)),
flag2(operator[](eFlag2)),
flag3(operator[](eFlag3)),
flag4(operator[](eFlag4))
{}
reference flag0;
reference flag1;
reference flag2;
reference flag3;
reference flag4;
};
Flags<5>::tFlagValue MyFlags::sFlagValues[] = {
{MyFlags::eFlag0, "flag0"},
{MyFlags::eFlag1, "flag1"},
{MyFlags::eFlag2, "flag2"},
{MyFlags::eFlag3, "flag3"},
{MyFlags::eFlag4, "flag4"},
};
Definition at line 251 of file Flags.hxx.