Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72683 - sandbox/conversion/libs/conversion_ext/example
From: vicente.botet_at_[hidden]
Date: 2011-06-19 12:05:46


Author: viboes
Date: 2011-06-19 12:05:45 EDT (Sun, 19 Jun 2011)
New Revision: 72683
URL: http://svn.boost.org/trac/boost/changeset/72683

Log:
Conversion: Added more examples
Added:
   sandbox/conversion/libs/conversion_ext/example/fallback.cpp (contents, props changed)
   sandbox/conversion/libs/conversion_ext/example/overload.cpp (contents, props changed)
Text files modified:
   sandbox/conversion/libs/conversion_ext/example/no_throw.cpp | 21 ++++++++++++---------
   1 files changed, 12 insertions(+), 9 deletions(-)

Added: sandbox/conversion/libs/conversion_ext/example/fallback.cpp
==============================================================================
--- (empty file)
+++ sandbox/conversion/libs/conversion_ext/example/fallback.cpp 2011-06-19 12:05:45 EDT (Sun, 19 Jun 2011)
@@ -0,0 +1,44 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008-2009. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+//[FALLBACK_CPP
+
+#include <boost/conversion/include.hpp>
+#include <boost/conversion/std/string.hpp>
+#include <iostream>
+using namespace boost::conversion;
+
+void try_assign_to_way()
+{
+ std::string str="not an int";
+ //[FALLBACK_CPP_TRY_ASSIGN_WAY
+ int t=-1;
+ try_assign_to(t,str);
+ //]
+}
+
+void convert_to_or_fallback_way()
+{
+ std::string str="not an int";
+ //[FALLBACK_CPP_CONVERT_TO_OR_FALLBACK
+ int t = convert_to_or_fallback<int>(str,-1);
+ //]
+}
+
+int main()
+{
+
+ try_assign_to_way();
+ convert_to_or_fallback_way();
+
+ return 0;
+}
+
+//]

Modified: sandbox/conversion/libs/conversion_ext/example/no_throw.cpp
==============================================================================
--- sandbox/conversion/libs/conversion_ext/example/no_throw.cpp (original)
+++ sandbox/conversion/libs/conversion_ext/example/no_throw.cpp 2011-06-19 12:05:45 EDT (Sun, 19 Jun 2011)
@@ -20,48 +20,51 @@
 void try_catch_way()
 {
   //[NO_THROW_CPP_TRY_CATCH_WAY
+ std::string str="not an int";
   int t;
- try
- {
- std::string str="not an int";
+ try {
     t = convert_to<int>(str);
- } catch(...)
- {
+ } catch(...) {
     std::cout << " Not an integer" << std::endl;
   }
   //]
 }
 void try_convert_to_way()
 {
- int t;
   std::string str="not an int";
+ int t;
+ //[NO_THROW_CPP_TRY_CONVERT_WAY
   optional<int> optt = try_convert_to<int>(str);
   if (!optt)
   {
     std::cout << " Not an integer" << std::endl;
   }
+ //]
 }
 
 void convert_to_optional_way()
 {
- int t;
   std::string str="not an int";
+ int t;
+ //[NO_THROW_CPP_TRY_CONVERT_TO_OPT_WAY
   optional<int> optt = convert_to<optional<int> >(str);
   if (!optt)
   {
     std::cout << " Not an integer" << std::endl;
   }
+ //]
 }
 
 void try_assign_to_way()
 {
- int t;
   std::string str="not an int";
-
+ int t;
+ //[NO_THROW_CPP_TRY_ASSIGN_WAY
   if (!try_assign_to(t,str))
   {
     std::cout << " Not an integer" << std::endl;
   }
+ //]
 }
 
 int main()

Added: sandbox/conversion/libs/conversion_ext/example/overload.cpp
==============================================================================
--- (empty file)
+++ sandbox/conversion/libs/conversion_ext/example/overload.cpp 2011-06-19 12:05:45 EDT (Sun, 19 Jun 2011)
@@ -0,0 +1,144 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008-2009. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+//[OVERLOAD_CPP
+
+#include <boost/conversion/include.hpp>
+//#include <boost/conversion/std/string.hpp>
+#include <string>
+#include <iostream>
+using namespace boost::conversion;
+
+
+//[OVERLOAD_CPP_FCTS
+int f(int) { return 1; }
+int f(std::string const&) { return 2; }
+//]
+
+//[OVERLOAD_CPP_IMPL
+template <typename T>
+struct ImplTest {
+ static void whichOverload()
+ {
+ T v;
+ std::cout << f(v) << " called" << std::endl;
+ }
+};
+//]
+
+struct IntrCvtToInt {
+ operator int() const {return 0;}
+};
+
+struct IntrCvtToString {
+ operator std::string() const {return "";}
+};
+
+struct IntrCvtINtAndString {
+ operator int() const {return 0;}
+ operator std::string() const {return "";}
+};
+
+struct ExtrCvtToInt {};
+struct ExtrCvtToString {};
+struct ExtrCvtINtAndString {};
+struct ExtrExplicitCvtToInt {};
+
+namespace boost {
+ namespace conversion {
+ template <>
+ struct converter_cp< int, ExtrCvtToInt> : true_type {
+ int operator()(ExtrCvtToInt const&)
+ {
+ return 0;
+ }
+ };
+ template <>
+ struct explicit_converter_cp< int, ExtrExplicitCvtToInt> : true_type {
+ int operator()(ExtrExplicitCvtToInt const&)
+ {
+ return 0;
+ }
+ };
+ template <>
+ struct converter_cp< int, ExtrCvtINtAndString> : true_type {
+ int operator()(ExtrCvtINtAndString const&)
+ {
+ return 0;
+ }
+ };
+ template <>
+ struct converter_cp< std::string , ExtrCvtToString > : true_type {
+ std::string operator()(ExtrCvtToString const&)
+ {
+ return "";
+ }
+ };
+ template <>
+ struct converter_cp< std::string , ExtrCvtINtAndString > : true_type {
+ std::string operator()(ExtrCvtINtAndString const&)
+ {
+ return "";
+ }
+ };
+ }
+}
+//[OVERLOAD_CPP_MCF_LIKE
+template <typename T>
+struct McfTest {
+ static void whichOverload()
+ {
+ T v;
+ std::cout << f(mcf(v)) << " called" << std::endl;
+ }
+};
+//]
+
+void impl_intrinsic_test()
+{
+ //[OVERLOAD_CPP_IMPL_INTRINSIC
+ ImplTest<IntrCvtToInt>::whichOverload();
+ ImplTest<IntrCvtToString>::whichOverload();
+ //ImplTest<IntrCvtINtAndString>::whichOverload(); // compile fail
+ //]
+}
+void impl_extrinsic_test()
+{
+ //[OVERLOAD_CPP_IMPL_EXTRINSIC
+ //ImplTest<ExtrCvtToInt>::whichOverload(); // compile fail
+ //ImplTest<ExtrCvtToString>::whichOverload(); // compile fail
+ //ImplTest<ExtrCvtINtAndString>::whichOverload(); // compile fail
+ //]
+}
+
+void mcf_extrinsic_test()
+{
+ //[OVERLOAD_CPP_MCF
+ McfTest<IntrCvtToInt>::whichOverload();
+ McfTest<IntrCvtToString>::whichOverload();
+ McfTest<ExtrCvtToInt>::whichOverload();
+ McfTest<ExtrCvtToString>::whichOverload();
+ //McfTest<ExtrCvtINtAndString>::whichOverload(); // compile fail
+ //]
+ //[OVERLOAD_CPP_MCF_EXPLICIT
+ McfTest<ExtrExplicitCvtToInt>::whichOverload();
+ //]
+}
+
+int main()
+{
+
+ impl_intrinsic_test();
+ mcf_extrinsic_test();
+
+ return 1;
+}
+
+//]


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