flagmap.h

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #ifndef WT_FLAGMAP_H
00022 #define WT_FLAGMAP_H
00023 
00024 #include <map>
00025 
00026 namespace Wt {
00027 
00028 /// maps the flags of the underlying system to ours
00029 /*!
00030 This is one weird class that needs some documenting since
00031 it competely not obvious.
00032  
00033 This is a map between two different but equivalent flags
00034 sets.
00035  
00036 The mapped type (value_type) is always an enum in
00037 power of two.
00038  
00039 The key_type can be consequent or power of two. We use
00040 a heuristic to decide if it is consequent or not.
00041 if consequent = true then the key is expected
00042 in consequent numbers starting from 1 (1, 2, 3) else
00043 it is expected an enum with powers of two (1, 2, 4, 8)
00044  
00045 You can map a key of the original system to a Wt flag
00046 using operator[].
00047  
00048 It is also possible to map an ored combination of
00049 the original flags using state().
00050 */
00051 class FlagMap : public std::map<int, int> {
00052     typedef std::map<int, int> StdMap;
00053 
00054 public:
00055     FlagMap() : StdMap() {}
00056 
00057     template<typename InputIterator>
00058     FlagMap(InputIterator first, InputIterator last) :
00059             StdMap(first, last),
00060 consequent_(size() > 2 && find(3) == end() ? false : true) {}
00061 
00062     // forward look up
00063     int operator[](int key) const {
00064         const_iterator it = find(key);
00065         const bool is_flag_valid = it != end();
00066         //assert(is_flag_valid);
00067         return is_flag_valid ? it->second : 0;
00068     }
00069 
00070     int state(int flags) const {
00071         const FlagMap& self = *this;
00072         int state_ = 0;
00073         int key;
00074 
00075         for (const_iterator it = begin(), e = end(); it != e; ++it) {
00076             if (consequent_) {
00077                 key = (flags & (1 << (it->first - 1))) ? it->first : 0;
00078             } else {
00079                 key = flags & it->first;
00080             }
00081             state_ |= self[key];
00082         }
00083 
00084         return state_;
00085     }
00086 
00087 private:
00088     bool consequent_;
00089 };
00090 
00091 }
00092 
00093 #endif // WT_FLAGMAP_H

Generated Fri Jul 28 19:23:00 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.