LexicalToolKit.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 
00034 #ifndef KEYVALUE_LEXICAL_TOOL_KIT_H_
00035 #define KEYVALUE_LEXICAL_TOOL_KIT_H_
00036 
00037 #include <boost/utility.hpp>
00038 
00039 #include "keyvalue/extern/String.h"
00040 #include "keyvalue/extern/Ptime.h"
00041 #include "keyvalue/sys/exception/Exception.h"
00042 #include "keyvalue/util/Lexical.h"
00043 
00044 namespace keyvalue {
00045 namespace frontend {
00046 
00077 class LexicalToolKit : private boost::noncopyable {
00078 
00079 public:
00080 
00086   LexicalToolKit();
00087 
00093   enum IO { none = 0x0, input = 0x1, output = 0x2, both = 0x3};
00094 
00108   template <typename From, typename To>
00109   struct Helper {
00110 
00111     // The converter's signature
00112     typedef To (*Type_)(const From&);
00113 
00114     // A pointer to LexicalToolKit's member which holds the corresponding
00115     // conversion function pointer.
00116     static Type_ LexicalToolKit::* converter_;
00117 
00118     // A pointer to the LexicalToolKit member which holds the corresponding
00119     // direction flag.
00120     static IO    LexicalToolKit::* io_;
00121   };
00122 
00143   template <typename From, typename To>
00144   void
00145   set(IO io, typename Helper<From, To>::Type_ converter = util::Lexical);
00146 
00162   template <typename From, typename To>
00163   bool
00164   isEnabled(IO io) const;
00165 
00172   class Failure : public RuntimeError {
00173   };
00174 
00198   template <typename To, typename From>
00199   To
00200   convert(From input) const;
00201 
00202 private:
00203 
00212 #define KV_LEXICAL_TOOL_KIT_PAIR(From, To) \
00213   Helper<From, To>::Type_ From##2##To##_; \
00214   IO                      From##2##To##_io_;
00215 
00216 #include "keyvalue/frontend/LexicalToolKitPairs.h"
00217 
00225   char endInitList;
00226 
00227 }; // class LexicalToolKit
00228 
00229 } // namespace frontend
00230 } // namespace keyvalue
00231 
00232 #endif // KEYVALUE_LEXICAL_TOOL_KIT_H_