|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r56054 - in sandbox/itl: boost/itl/type_traits boost/itl_xt libs/itl/example/boost_party_ libs/itl/example/interval_ libs/itl/example/man_power_ libs/itl/example/month_and_week_grid_ libs/itl/example/overlap_counter_ libs/itl/example/party_ libs/itl/example/partys_height_average_ libs/itl/example/partys_tallest_guests_ libs/itl/example/user_groups_ libs/itl_xt/example
From: afojgo_at_[hidden]
Date: 2009-09-05 17:12:53
Author: jofaber
Date: 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
New Revision: 56054
URL: http://svn.boost.org/trac/boost/changeset/56054
Log:
Refactoring: Simplified string conversion.
Text files modified:
sandbox/itl/boost/itl/type_traits/to_string.hpp | 62 +++++----------------------------------
sandbox/itl/boost/itl_xt/var_tuple.hpp | 20 ++++++++++++
sandbox/itl/libs/itl/example/boost_party_/boost_party.cpp | 1
sandbox/itl/libs/itl/example/interval_/interval.cpp | 11 +++---
sandbox/itl/libs/itl/example/man_power_/man_power.cpp | 1
sandbox/itl/libs/itl/example/month_and_week_grid_/month_and_week_grid.cpp | 1
sandbox/itl/libs/itl/example/overlap_counter_/overlap_counter.cpp | 1
sandbox/itl/libs/itl/example/party_/party.cpp | 1
sandbox/itl/libs/itl/example/partys_height_average_/partys_height_average.cpp | 1
sandbox/itl/libs/itl/example/partys_tallest_guests_/partys_tallest_guests.cpp | 1
sandbox/itl/libs/itl/example/user_groups_/user_groups.cpp | 1
sandbox/itl/libs/itl_xt/example/toytime.h | 37 ++++++++---------------
12 files changed, 46 insertions(+), 92 deletions(-)
Modified: sandbox/itl/boost/itl/type_traits/to_string.hpp
==============================================================================
--- sandbox/itl/boost/itl/type_traits/to_string.hpp (original)
+++ sandbox/itl/boost/itl/type_traits/to_string.hpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -24,65 +24,19 @@
namespace boost{ namespace itl
{
-/// static class template for the string representation of values
-/**
- <b>value</b> serves as a base to for the representation
- of atomic types and classes as strings.
-
- The function <tt>toString</tt> is defined for all atomic (built in) types
- like <tt>int, double etc.</tt>. For types other than atomic ones namely
- classes the function template <tt>toString</tt> calls a member function
- <tt>as_string</tt> on the class-type passed to the template.
-
- Thereby we can implement a general string representation for template classes
- which is independent of the template parameter being an atomic or a class type.
- For every object, including atomic the string converter function toString can
- be called, provides new classes implement a memberfunction <tt>as_string</tt>
-
- @author Joachim Faulhaber
-*/
+/// Static class template for the string representation of values
template <class Type>
struct to_string
{
- /** String converter for all types <tt>Type</tt>
-
- E.g.: <tt>int i=5; string s = to_string<int>::apply(i);</tt>
- */
- static const std::string apply(const Type&);
+ /** Converts all values of types to std::string that implement an operator << */
+ static const std::string apply(const Type& value)
+ {
+ std::stringstream repr;
+ repr << value;
+ return repr.str();
+ }
};
-
-typedef char * CharPT;
-
-#define RETURN_AS_STRING(format, atomicValue) \
- std::stringstream repr; \
- repr << atomicValue; \
- return repr.str();
-
-/* Alternative macro using formated sprintf output
-#define RETURN_AS_STRING(format, atomicValue) \
- char stringRepr[512]; \
- sprintf(stringRepr, format, atomicValue); \
- return stringRepr;
-*/
-
-template<> inline const std::string to_string<bool>::apply(const bool& x){ return x ? "true" : "false"; }
-template<> inline const std::string to_string<char>::apply(const char& x){ RETURN_AS_STRING("%c", x); }
-template<> inline const std::string to_string<short>::apply(const short& x) { RETURN_AS_STRING("%d", x); }
-template<> inline const std::string to_string<int>::apply(const int& x) { RETURN_AS_STRING("%d", x); }
-template<> inline const std::string to_string<long>::apply(const long& x) { RETURN_AS_STRING("%ld", x); }
-template<> inline const std::string to_string<unsigned char>::apply(const unsigned char& x) { RETURN_AS_STRING("%uc", x); }
-template<> inline const std::string to_string<unsigned short>::apply(const unsigned short& x) { RETURN_AS_STRING("%hu", x); }
-template<> inline const std::string to_string<unsigned int>::apply(const unsigned int& x) { RETURN_AS_STRING("%u", x); }
-template<> inline const std::string to_string<unsigned long>::apply(const unsigned long& x) { RETURN_AS_STRING("%lu", x); }
-template<> inline const std::string to_string<float>::apply(const float& x) { RETURN_AS_STRING("%f", x); }
-template<> inline const std::string to_string<double>::apply(const double& x) { RETURN_AS_STRING("%lf", x); }
-template<> inline const std::string to_string<CharPT>::apply(const CharPT & x) { RETURN_AS_STRING("%s", x); }
-template<> inline const std::string to_string<std::string>::apply(const std::string& x) { return x; }
-
-template <class Type>
-inline const std::string to_string<Type>::apply(const Type& x) { return x.as_string(); }
-
}} // namespace boost itl
#endif
Modified: sandbox/itl/boost/itl_xt/var_tuple.hpp
==============================================================================
--- sandbox/itl/boost/itl_xt/var_tuple.hpp (original)
+++ sandbox/itl/boost/itl_xt/var_tuple.hpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -152,6 +152,26 @@
}
+ //==============================================================================
+ //= Representation
+ //==============================================================================
+
+ template<class CharType, class CharTraits, int varCountV>
+ std::basic_ostream<CharType, CharTraits>& operator <<
+ (std::basic_ostream<CharType, CharTraits> &stream, var_tuple<varCountV> const& tuple)
+ {
+ stream << "(";
+ for(int idx = 0; idx < varCountV-1; idx++)
+ stream << tuple[idx] << ",";
+
+ if(varCountV==0)
+ return stream << ")";
+ else
+ return stream << tuple[varCountV-1] << ")";
+ }
+
+
+
}} // namespace itl boost
Modified: sandbox/itl/libs/itl/example/boost_party_/boost_party.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/boost_party_/boost_party.cpp (original)
+++ sandbox/itl/libs/itl/example/boost_party_/boost_party.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -51,7 +51,6 @@
// and a few lines of adapter code.
#include <boost/itl/ptime.hpp>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/interval_map.hpp>
using namespace std;
Modified: sandbox/itl/libs/itl/example/interval_/interval.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/interval_/interval.cpp (original)
+++ sandbox/itl/libs/itl/example/interval_/interval.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -21,7 +21,6 @@
#include <iostream>
#include <string>
#include <math.h>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/interval.hpp>
#include "../toytime.hpp"
@@ -38,17 +37,17 @@
interval<string> city_interval = interval<string>::leftopen("Barcelona", "Boston");
interval<Time> time_interval = interval<Time>::open(Time(monday,8,30), Time(monday,17,20));
- cout << "Interval<int>: " << int_interval.as_string() << endl;
- cout << "Interval<double>: " << sqrt_interval.as_string() << " does "
+ cout << "Interval<int>: " << int_interval << endl;
+ cout << "Interval<double>: " << sqrt_interval << " does "
<< string(sqrt_interval.contains(sqrt(2.0))?"":"NOT")
<< " contain sqrt(2)" << endl;
- cout << "Interval<string>: " << city_interval.as_string() << " does "
+ cout << "Interval<string>: " << city_interval << " does "
<< string(city_interval.contains("Barcelona")?"":"NOT")
<< " contain 'Barcelona'" << endl;
- cout << "Interval<string>: " << city_interval.as_string() << " does "
+ cout << "Interval<string>: " << city_interval << " does "
<< string(city_interval.contains("Berlin")?"":"NOT")
<< " contain 'Berlin'" << endl;
- cout << "Interval<Time>: " << time_interval.as_string() << endl;
+ cout << "Interval<Time>: " << time_interval << endl;
return 0;
}
Modified: sandbox/itl/libs/itl/example/man_power_/man_power.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/man_power_/man_power.cpp (original)
+++ sandbox/itl/libs/itl/example/man_power_/man_power.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -29,7 +29,6 @@
// and a few lines of adapter code.
#include <boost/itl/gregorian.hpp>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/interval_map.hpp>
using namespace std;
Modified: sandbox/itl/libs/itl/example/month_and_week_grid_/month_and_week_grid.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/month_and_week_grid_/month_and_week_grid.cpp (original)
+++ sandbox/itl/libs/itl/example/month_and_week_grid_/month_and_week_grid.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -34,7 +34,6 @@
// and a few lines of adapter code.
#include <boost/itl/gregorian.hpp>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/split_interval_map.hpp>
using namespace std;
Modified: sandbox/itl/libs/itl/example/overlap_counter_/overlap_counter.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/overlap_counter_/overlap_counter.cpp (original)
+++ sandbox/itl/libs/itl/example/overlap_counter_/overlap_counter.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -26,7 +26,6 @@
//[example_overlap_counter
#include <stdio.h>
#include <iostream>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/split_interval_map.hpp>
using namespace std;
Modified: sandbox/itl/libs/itl/example/party_/party.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/party_/party.cpp (original)
+++ sandbox/itl/libs/itl/example/party_/party.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -10,7 +10,6 @@
+-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <iostream>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/interval_map.hpp>
#include "../toytime.hpp"
Modified: sandbox/itl/libs/itl/example/partys_height_average_/partys_height_average.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/partys_height_average_/partys_height_average.cpp (original)
+++ sandbox/itl/libs/itl/example/partys_height_average_/partys_height_average.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -27,7 +27,6 @@
// and a few lines of adapter code.
#include <boost/itl/ptime.hpp>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/interval_map.hpp>
#include <boost/itl/split_interval_map.hpp>
Modified: sandbox/itl/libs/itl/example/partys_tallest_guests_/partys_tallest_guests.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/partys_tallest_guests_/partys_tallest_guests.cpp (original)
+++ sandbox/itl/libs/itl/example/partys_tallest_guests_/partys_tallest_guests.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -31,7 +31,6 @@
// and a few lines of adapter code.
#include <boost/itl/ptime.hpp>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/interval_map.hpp>
#include <boost/itl/split_interval_map.hpp>
Modified: sandbox/itl/libs/itl/example/user_groups_/user_groups.cpp
==============================================================================
--- sandbox/itl/libs/itl/example/user_groups_/user_groups.cpp (original)
+++ sandbox/itl/libs/itl/example/user_groups_/user_groups.cpp 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -38,7 +38,6 @@
// and a few lines of adapter code.
#include <boost/itl/gregorian.hpp>
-#include <boost/itl/type_traits/to_string.hpp>
#include <boost/itl/split_interval_map.hpp>
using namespace std;
Modified: sandbox/itl/libs/itl_xt/example/toytime.h
==============================================================================
--- sandbox/itl/libs/itl_xt/example/toytime.h (original)
+++ sandbox/itl/libs/itl_xt/example/toytime.h 2009-09-05 17:12:52 EDT (Sat, 05 Sep 2009)
@@ -1,29 +1,11 @@
-/*-----------------------------------------------------------------------------+
+/*-----------------------------------------------------------------------------+
+Copyright (c) 2007-2009: Joachim Faulhaber
++------------------------------------------------------------------------------+
Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
+------------------------------------------------------------------------------+
-Boost Software License - Version 1.0 - August 17th, 2003
-
-Permission is hereby granted, free of charge, to any person or organization
-obtaining a copy of the software and accompanying documentation covered by
-this license (the "Software") to use, reproduce, display, distribute,
-execute, and transmit the Software, and to prepare derivative works of the
-Software, and to permit third-parties to whom the Software is furnished to
-do so, all subject to the following:
-
-The copyright notices in the Software and this entire statement, including
-the above license grant, this restriction and the following disclaimer,
-must be included in all copies of the Software, in whole or in part, and
-all derivative works of the Software, unless such copies or derivative
-works are solely in the form of machine-executable object code generated by
-a source language processor.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
-SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
-FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-DEALINGS IN THE SOFTWARE.
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENCE.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+-----------------------------------------------------------------------------*/
namespace boost{namespace itl
@@ -71,5 +53,12 @@
bool operator == (const Time& x1, const Time& x2) { return x1.asInt() == x2.asInt(); }
bool operator <= (const Time& x1, const Time& x2) { return x1.asInt() <= x2.asInt(); }
+template<class CharType, class CharTraits>
+std::basic_ostream<CharType, CharTraits>& operator <<
+ (std::basic_ostream<CharType, CharTraits> &stream, Time const& time)
+{
+ return stream << time.as_string();
+}
+
}} // namespace itl boost
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk