Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52272 - in sandbox/pinhole: boost/pinhole libs/pinhole/test
From: jmcintyre_at_[hidden]
Date: 2009-04-08 19:45:44


Author: jared
Date: 2009-04-08 19:45:43 EDT (Wed, 08 Apr 2009)
New Revision: 52272
URL: http://svn.boost.org/trac/boost/changeset/52272

Log:
Pinhole now supports conversion between string and wstring.
Text files modified:
   sandbox/pinhole/boost/pinhole/property_info.hpp | 18 ++++++++++++++----
   sandbox/pinhole/libs/pinhole/test/test_string_properties.cpp | 24 ++++++++++++------------
   2 files changed, 26 insertions(+), 16 deletions(-)

Modified: sandbox/pinhole/boost/pinhole/property_info.hpp
==============================================================================
--- sandbox/pinhole/boost/pinhole/property_info.hpp (original)
+++ sandbox/pinhole/boost/pinhole/property_info.hpp 2009-04-08 19:45:43 EDT (Wed, 08 Apr 2009)
@@ -18,6 +18,8 @@
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/format.hpp>
 #include <boost/any.hpp>
+#include <boost/archive/iterators/mb_from_wchar.hpp>
+#include <boost/archive/iterators/wchar_from_mb.hpp>
 #if defined(BOOST_MSVC)
     #pragma warning(pop)
 #endif
@@ -94,7 +96,8 @@
         template<typename Set_Type>
         inline void operator()( Set_Type setter, std::wstring value )
         {
- throw std::invalid_argument( "A wstring cannot be used to set a string." );
+ typedef boost::archive::iterators::mb_from_wchar<std::wstring::const_iterator> translator;
+ setter( std::string( translator(value.begin()), translator(value.end()) ) );
         }
     };
 
@@ -107,7 +110,8 @@
         template<typename Set_Type>
         inline void operator()( Set_Type setter, std::string value )
         {
- throw std::invalid_argument( "A string cannot be used to set a wstring." );
+ typedef boost::archive::iterators::wchar_from_mb<std::string::const_iterator> translator;
+ setter( std::wstring( translator(value.begin()), translator(value.end()) ) );
         }
     };
 
@@ -201,7 +205,10 @@
         template<typename Get_Type>
         inline std::string operator()( Get_Type getter ) const
         {
- throw std::invalid_argument( "A wstring property cannot be returned as a string." );
+ typedef boost::archive::iterators::mb_from_wchar<std::wstring::const_iterator> translator;
+
+ std::wstring& value = getter();
+ return std::string( translator(value.begin()), translator(value.end()) );
         }
     };
 
@@ -214,7 +221,10 @@
         template<typename Get_Type>
         inline std::wstring operator()( Get_Type getter ) const
         {
- throw std::invalid_argument( "A wstring property cannot be returned as a string." );
+ typedef boost::archive::iterators::wchar_from_mb<std::string::const_iterator> translator;
+
+ std::string& value = getter();
+ return std::wstring( translator(value.begin()), translator(value.end()) );
         }
     };
 

Modified: sandbox/pinhole/libs/pinhole/test/test_string_properties.cpp
==============================================================================
--- sandbox/pinhole/libs/pinhole/test/test_string_properties.cpp (original)
+++ sandbox/pinhole/libs/pinhole/test/test_string_properties.cpp 2009-04-08 19:45:43 EDT (Wed, 08 Apr 2009)
@@ -57,9 +57,9 @@
         testGroup.set_as_string( "String_Func", "second" );
     BOOST_CHECK_EQUAL( testGroup.get_as_string("String_Func"), "second" );
 
- BOOST_CHECK_THROW( testGroup.set_as_wstring( "String_Func", L"third" ), std::invalid_argument );
- BOOST_CHECK_THROW( testGroup.get_as_wstring("String_Func"), std::invalid_argument );
- BOOST_CHECK_EQUAL( testGroup.get_as_string("String_Func"), "second" );
+ testGroup.set_as_wstring( "String_Func", L"third" );
+ BOOST_CHECK( testGroup.get_as_wstring("String_Func") == L"third" );
+ BOOST_CHECK_EQUAL( testGroup.get_as_string("String_Func"), "third" );
 }
 
 BOOST_AUTO_TEST_CASE( TestGetSetString_Var )
@@ -72,9 +72,9 @@
         testGroup.set_as_wstring( "WString_Var", L"second" );
     BOOST_CHECK( testGroup.get_as_wstring("WString_Var") == L"second" );
 
- BOOST_CHECK_THROW( testGroup.set_as_string( "WString_Var", "third" ), std::invalid_argument );
- BOOST_CHECK_THROW( testGroup.get_as_string("WString_Var"), std::invalid_argument );
- BOOST_CHECK( testGroup.get_as_wstring("WString_Var") == L"second" );
+ testGroup.set_as_string( "WString_Var", "third" );
+ BOOST_CHECK_EQUAL( testGroup.get_as_string("WString_Var"), "third" );
+ BOOST_CHECK( testGroup.get_as_wstring("WString_Var") == L"third" );
 }
 
 BOOST_AUTO_TEST_CASE( TestGetSetWString_Func )
@@ -87,9 +87,9 @@
     testGroup.set_as_wstring( "WString_Func", L"second" );
     BOOST_CHECK( testGroup.get_as_wstring("WString_Func") == L"second" );
 
- BOOST_CHECK_THROW( testGroup.set_as_string("WString_Func", "third" ), std::invalid_argument );
- BOOST_CHECK_THROW( testGroup.get_as_string("WString_Func"), std::invalid_argument );
- BOOST_CHECK( testGroup.get_as_wstring("WString_Func") == L"second" );
+ testGroup.set_as_string("WString_Func", "third" );
+ BOOST_CHECK_EQUAL( testGroup.get_as_string("WString_Func"), "third" );
+ BOOST_CHECK( testGroup.get_as_wstring("WString_Func") == L"third" );
 }
 
 BOOST_AUTO_TEST_CASE( TestGetSetWString_Var )
@@ -102,9 +102,9 @@
     testGroup.set_as_string( "String_Var", "second" );
     BOOST_CHECK_EQUAL( testGroup.get_as_string("String_Var"), "second" );
 
- BOOST_CHECK_THROW( testGroup.set_as_wstring("String_Var", L"third" ), std::invalid_argument );
- BOOST_CHECK_THROW( testGroup.get_as_wstring("String_Var"), std::invalid_argument );
- BOOST_CHECK_EQUAL( testGroup.get_as_string("String_Var"), "second" );
+ testGroup.set_as_wstring("String_Var", L"third" );
+ BOOST_CHECK( testGroup.get_as_wstring("String_Var") == L"third" );
+ BOOST_CHECK_EQUAL( testGroup.get_as_string("String_Var"), "third" );
 }
 
 BOOST_AUTO_TEST_CASE( TestStringPropertyType )


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