FlagMap.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00035 #ifndef KEYVALUE_KEY_MAP_FLAGMAP_H_
00036 #define KEYVALUE_KEY_MAP_FLAGMAP_H_
00037 
00038 #include "keyvalue/extern/String.h"
00039 #include "keyvalue/key/Key.h"
00040 #include "keyvalue/sys/exception/Exception.h"
00041 #include "keyvalue/value/Variant.h"
00042 
00043 namespace keyvalue {
00044 namespace key {
00045 
00069 template <typename OutputType>
00070 class FlagMap {
00071 
00072 public:
00073 
00074   typedef OutputType OutputType_;
00075 
00087   OutputType
00088   map(const value::Variant& variant) const;
00089 
00099   virtual OutputType
00100   get(const string& name) const = 0;
00101 
00102 private:
00103 
00109   virtual string
00110   getName() const = 0;
00111 
00112 }; // class FlagMap
00113 
00114 /*--------------------------------------------------------------------------
00115  * map()
00116  *------------------------------------------------------------------------*/
00117 
00118 template<typename OutputType>
00119 OutputType
00120 FlagMap<OutputType>::map(const value::Variant& variant) const {
00121 
00122   if (variant.is<string>(frontend::LexicalToolKit::input)) {
00123 
00124     try {
00125       return get(variant.get<string>(frontend::LexicalToolKit::input));
00126     }
00127 
00128     catch (frontend::LexicalToolKit::Failure&) {
00129     }
00130   }
00131 
00132   throw RuntimeError() & "Invalid value '" & variant & "' for key '" &
00133     getName() & "'!";
00134 }
00135 
00136 } // namespace key
00137 } // namespace keyvalue
00138 
00139 #endif // KEYVALUE_KEY_MAP_FLAGMAP_H_