diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-29 02:24:25 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-29 02:24:25 -0400 |
commit | 8b75b5cbfbb53e0bfa51c63dc96ade69617cd1db (patch) | |
tree | c10c7ea058d0491988078093ad27bac2f9ba240c /src/flags.h | |
parent | 5b388af6264b1bff5ff143db4120e4776d06c90c (diff) | |
download | fork-ledger-8b75b5cbfbb53e0bfa51c63dc96ade69617cd1db.tar.gz fork-ledger-8b75b5cbfbb53e0bfa51c63dc96ade69617cd1db.tar.bz2 fork-ledger-8b75b5cbfbb53e0bfa51c63dc96ade69617cd1db.zip |
Revised the way that parsing flags get passed around.
Diffstat (limited to 'src/flags.h')
-rw-r--r-- | src/flags.h | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/src/flags.h b/src/flags.h index 297d5b9f..eb42311c 100644 --- a/src/flags.h +++ b/src/flags.h @@ -32,7 +32,7 @@ #ifndef _FLAGS_H #define _FLAGS_H -template <typename T = boost::uint_least8_t> +template <typename T = boost::uint_least8_t, typename U = T> class supports_flags { public: @@ -42,7 +42,7 @@ protected: flags_t _flags; public: - supports_flags() : _flags(0) { + supports_flags() : _flags(static_cast<T>(0)) { TRACE_CTOR(supports_flags, ""); } supports_flags(const supports_flags& arg) : _flags(arg._flags) { @@ -66,13 +66,60 @@ public: _flags = arg; } void clear_flags() { - _flags = 0; + _flags = static_cast<T>(0); } void add_flags(const flags_t arg) { - _flags |= arg; + _flags = static_cast<T>(static_cast<U>(_flags) | static_cast<U>(arg)); } void drop_flags(const flags_t arg) { - _flags &= ~arg; + _flags = static_cast<T>(static_cast<U>(_flags) & static_cast<U>(~arg)); + } +}; + +template <typename T = boost::uint_least8_t, typename U = T> +class basic_flags_t : public supports_flags<T, U> +{ +public: + basic_flags_t() { + TRACE_CTOR(basic_flags_t, ""); + } + basic_flags_t(const T& bits) { + TRACE_CTOR(basic_flags_t, "const T&"); + set_flags(bits); + } + basic_flags_t(const U& bits) { + TRACE_CTOR(basic_flags_t, "const U&"); + set_flags(static_cast<T>(bits)); + } + ~basic_flags_t() throw() { + TRACE_DTOR(basic_flags_t); + } + + basic_flags_t& operator=(const basic_flags_t& other) { + set_flags(other.flags()); + return *this; + } + basic_flags_t& operator=(const T& bits) { + set_flags(bits); + return *this; + } + + operator T() const { + return supports_flags<T, U>::flags(); + } + operator U() const { + return supports_flags<T, U>::flags(); + } + + basic_flags_t plus_flags(const T& arg) const { + basic_flags_t temp(*this); + temp.add_flags(arg); + return temp; + } + basic_flags_t minus_flags(const T& arg) const { + basic_flags_t temp(*this); + temp.drop_flags(arg); + return temp; } }; |