Author: mordante
Date: Fri May 1 23:51:51 2009
New Revision: 35400
URL: http://svn.gna.org/viewcvs/wesnoth?rev=35400&view=rev
Log:
Initial commit of the lexical_cast test.
Copied from the svn version.
Added:
trunk/src/lexical_cast/
trunk/src/lexical_cast/util.cpp
trunk/src/lexical_cast/util.hpp
Modified:
trunk/src/CMakeLists.txt
Modified: trunk/src/CMakeLists.txt
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/CMakeLists.txt?rev=35400&r1=35399&r2=35400&view=diff
==============================================================================
--- trunk/src/CMakeLists.txt (original)
+++ trunk/src/CMakeLists.txt Fri May 1 23:51:51 2009
@@ -105,6 +105,10 @@
########### libwesnoth-core ###############
+
+add_executable(lexical_cast
+ lexical_cast/util.cpp
+)
SET(libwesnoth-core_STAT_SRC
color_range.cpp
Added: trunk/src/lexical_cast/util.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/lexical_cast/util.cpp?rev=35400&view=auto
==============================================================================
--- trunk/src/lexical_cast/util.cpp (added)
+++ trunk/src/lexical_cast/util.cpp Fri May 1 23:51:51 2009
@@ -1,0 +1,120 @@
+/* $Id$ */
+/*
+ Copyright (C) 2005 - 2009 by Philippe Plantier <[email protected]>
+ Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2
+ or at your option any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY.
+
+ See the COPYING file for more details.
+*/
+
+/**
+ * @file util.cpp
+ * String-routines - Templates for lexical_cast & lexical_cast_default.
+ */
+
+// g++ -O2 -o util util.cpp && ./util
+#define LOG
+//#define THROW
+
+#include "./util.hpp"
+
+#include <cstdlib>
+
+template<>
+int lexical_cast<int, const std::string&>(const std::string& a)
+{
+#ifdef LOG
+ std::cerr << "Lexical cast specialized int - string\n";
+#endif
+ char* endptr;
+ int res = strtol(a.c_str(), &endptr, 10);
+
+ if (a.empty() || *endptr != '\0') {
+ throw bad_lexical_cast();
+ } else {
+ return res;
+ }
+}
+
+template<>
+int lexical_cast<int, const char*>(const char* a)
+{
+#ifdef LOG
+ std::cerr << "Lexical cast specialized int - char*\n";
+#endif
+ char* endptr;
+ int res = strtol(a, &endptr, 10);
+
+ if (*a == '\0' || *endptr != '\0') {
+ throw bad_lexical_cast();
+ } else {
+ return res;
+ }
+}
+
+int main()
+{
+#ifdef THROW
+ try {
+ std::cerr << NEW_lexical_cast<std::string>(1) << '\n';
+ } catch(const std::string& type) {
+ std::cerr << type << '\n';
+ }
+
+ return 0;
+#endif
+
+#ifndef LOG
+ for(size_t i = 0; i != 1000000; ++i) {
+#if 0
+ lexical_cast<std::string>(1);
+#else
+ NEW_lexical_cast<std::string>(1);
+#endif
+ }
+
+#else
+
+ std::string foo = "1";
+ const std::string& bar = "1";
+ char foobar[] = "a";
+ foobar[0] = '1';
+
+ std::cerr << "Original\n";
+ std::cerr << lexical_cast<int>(foo) << '\n';
+ std::cerr << lexical_cast<int>(bar) << '\n';
+ std::cerr << lexical_cast<int>("1") << '\n';
+ std::cerr << lexical_cast<int>(foobar) << '\n';
+ std::cerr << lexical_cast<short>(foo) << '\n';
+ std::cerr << lexical_cast<short>(bar) << '\n';
+ std::cerr << lexical_cast<short>("1") << '\n';
+ std::cerr << lexical_cast<short>(foobar) << '\n';
+ std::cerr << lexical_cast<unsigned>(foo) << '\n';
+ std::cerr << lexical_cast<unsigned>(bar) << '\n';
+ std::cerr << lexical_cast<unsigned>("1") << '\n';
+ std::cerr << lexical_cast<unsigned>(foobar) << '\n';
+ std::cerr << lexical_cast<std::string>(1) << '\n';
+ std::cerr << lexical_cast<std::string>(1u) << '\n';
+
+ std::cerr << "\nNew\n";
+ std::cerr << NEW_lexical_cast<int>(foo) << '\n';
+ std::cerr << NEW_lexical_cast<int>(bar) << '\n';
+ std::cerr << NEW_lexical_cast<int>("1") << '\n';
+ std::cerr << NEW_lexical_cast<int>(foobar) << '\n';
+ std::cerr << NEW_lexical_cast<short>(foo) << '\n';
+ std::cerr << NEW_lexical_cast<short>(bar) << '\n';
+ std::cerr << NEW_lexical_cast<short>("1") << '\n';
+ std::cerr << NEW_lexical_cast<short>(foobar) << '\n';
+ std::cerr << NEW_lexical_cast<unsigned>(foo) << '\n';
+ std::cerr << NEW_lexical_cast<unsigned>(bar) << '\n';
+ std::cerr << NEW_lexical_cast<unsigned>("1") << '\n';
+ std::cerr << NEW_lexical_cast<unsigned>(foobar) << '\n';
+ std::cerr << NEW_lexical_cast<std::string>(1) << '\n';
+ std::cerr << NEW_lexical_cast<std::string>(1u) << '\n';
+#endif
+}
Added: trunk/src/lexical_cast/util.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/lexical_cast/util.hpp?rev=35400&view=auto
==============================================================================
--- trunk/src/lexical_cast/util.hpp (added)
+++ trunk/src/lexical_cast/util.hpp Fri May 1 23:51:51 2009
@@ -1,0 +1,166 @@
+/* $Id$ */
+/*
+ Copyright (C) 2003 - 2009 by David White <[email protected]>
+ Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2
+ or at your option any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY.
+
+ See the COPYING file for more details.
+*/
+
+/**
+ * @file util.hpp
+ * Templates and utility-routines for strings and numbers.
+ */
+
+#ifndef UTIL_H_INCLUDED
+#define UTIL_H_INCLUDED
+
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <boost/type_traits.hpp>
+
+struct bad_lexical_cast {};
+
+template<typename To, typename From>
+To lexical_cast(From a)
+{
+#ifdef LOG
+ std::cerr << "Lexical cast generic\n";
+#endif
+ To res;
+ std::stringstream str;
+
+ if(!(str << a && str >> res)) {
+ throw bad_lexical_cast();
+ } else {
+ return res;
+ }
+}
+
+template<>
+int lexical_cast<int, const std::string&>(const std::string& a);
+
+template<>
+int lexical_cast<int, const char*>(const char* a);
+
+namespace implementation {
+
+template<typename To, typename From>
+To default_lexical_cast(From a)
+{
+#ifdef THROW
+ throw std::string("default");
+#endif
+#ifdef LOG
+ std::cerr << "default\n";
+#endif
+ To res;
+ std::stringstream str;
+
+ if(!(str << a && str >> res)) {
+ throw bad_lexical_cast();
+ } else {
+ return res;
+ }
+}
+
+
+
+template<typename To, typename From>
+struct tlexical_cast
+{
+ To operator()(From a)
+ {
+#ifdef LOG
+ std::cerr << "NEW Generic\n";
+#endif
+ return default_lexical_cast<To,
+ typename boost::add_reference<
+ typename
boost::add_const<From>::type>::type>(a);
+ }
+};
+
+template<typename To, typename From>
+struct tlexical_cast<To, From*>
+{
+ To operator()(From* a)
+ {
+#ifdef LOG
+ std::cerr << "NEW pointer\n";
+#endif
+ return default_lexical_cast<To, const From*>(a);
+ }
+};
+
+template<typename From>
+struct tlexical_cast<std::string, From>
+{
+ template <class T>
+ std::string cast(T a, const boost::true_type&)
+ {
+#ifdef THROW
+ throw std::string("cast To = std::string - true_type");
+#endif
+#ifdef LOG
+ std::cerr << "cast To = std::string - true_type";
+#endif
+ std::stringstream sstr;
+ sstr << a;
+ return sstr.str();
+ }
+
+ template <class T>
+ std::string cast(T a, const boost::false_type&)
+ {
+#ifdef THROW
+ throw std::string("cast To = std::string - false_type");
+#endif
+#ifdef LOG
+ std::cerr << "cast To = std::string - false_type";
+#endif
+ return default_lexical_cast<std::string, T>(a);
+ }
+
+ std::string operator()(From a)
+ {
+#ifdef LOG
+ std::cerr << "cast To = std::string\b";
+#endif
+ return this->cast<From>(a, boost::is_integral<From>());
+ }
+};
+
+
+/*
+template<typename To>
+struct tlexical_cast<To, const std::string&>
+{
+ To operator()(const std::string& str)
+ {
+#ifdef LOG
+ std::cerr << "NEW Specialized\n";
+#endif
+ return lexical_cast_from_string<From>(a,
boost::is_integral<From>());
+ }
+};
+*/
+
+} // namespace implementation
+
+
+template<typename To, typename From>
+To NEW_lexical_cast(From a)
+{
+#ifdef LOG
+ std::cerr << "NEW Lexical cast\n";
+#endif
+ return implementation::tlexical_cast<To, From>().operator()(a);
+
+}
+#endif
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits