Boost logo

Boost-Commit :

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


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

Log:
Conversion: Added some examples
Added:
   sandbox/conversion/libs/conversion_ext/example/even.cpp (contents, props changed)
   sandbox/conversion/libs/conversion_ext/example/no_throw.cpp (contents, props changed)
   sandbox/conversion/libs/conversion_ext/example/swap.cpp (contents, props changed)

Added: sandbox/conversion/libs/conversion_ext/example/even.cpp
==============================================================================
--- (empty file)
+++ sandbox/conversion/libs/conversion_ext/example/even.cpp 2011-06-19 05:00:57 EDT (Sun, 19 Jun 2011)
@@ -0,0 +1,32 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+//[EVEN_CPP
+
+ #include <boost/conversion/convert_to.hpp>
+ #include <iostream>
+ using namespace boost::conversion;
+
+ template <typename T>
+ bool is_even(T val)
+ {
+ return (convert_to<int>(val) % 2)==0;
+ }
+ int main()
+ {
+ if (is_even(2))
+ std::cout << "2 is even" << std::endl;
+ else
+ std::cout << "2 is odd" << std::endl;
+
+ return 0;
+ }
+
+//]

Added: sandbox/conversion/libs/conversion_ext/example/no_throw.cpp
==============================================================================
--- (empty file)
+++ sandbox/conversion/libs/conversion_ext/example/no_throw.cpp 2011-06-19 05:00:57 EDT (Sun, 19 Jun 2011)
@@ -0,0 +1,77 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+//[NO_THROW_CPP
+
+#include <boost/conversion/include.hpp>
+#include <boost/conversion/std/string.hpp>
+#include <iostream>
+
+using boost::optional;
+using namespace boost::conversion;
+
+void try_catch_way()
+{
+ //[NO_THROW_CPP_TRY_CATCH_WAY
+ int t;
+ try
+ {
+ std::string str="not an int";
+ t = convert_to<int>(str);
+ } catch(...)
+ {
+ std::cout << " Not an integer" << std::endl;
+ }
+ //]
+}
+void try_convert_to_way()
+{
+ int t;
+ std::string str="not an int";
+ 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";
+ 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";
+
+ if (!try_assign_to(t,str))
+ {
+ std::cout << " Not an integer" << std::endl;
+ }
+}
+
+int main()
+{
+ try_catch_way();
+ try_convert_to_way();
+ convert_to_optional_way();
+ try_assign_to_way();
+
+ return 0;
+}
+
+//]

Added: sandbox/conversion/libs/conversion_ext/example/swap.cpp
==============================================================================
--- (empty file)
+++ sandbox/conversion/libs/conversion_ext/example/swap.cpp 2011-06-19 05:00:57 EDT (Sun, 19 Jun 2011)
@@ -0,0 +1,42 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+//[SWAP_CPP
+
+ #include <boost/conversion/assign_to.hpp>
+ #include <iostream>
+ using namespace boost::conversion;
+
+ template <typename T, typename U>
+ void swap_convertibles(T& t, U& u)
+ {
+ T tmp(t);
+ assign_to(t,u);
+ assign_to(u,tmp);
+ }
+ int main()
+ {
+ int i = 3;
+ float x = 2.5;
+
+ std::cout << "i= " << i << std::endl;
+ std::cout << "x= " << x << std::endl;
+
+#if defined(BOOST_CONVERSION_ENABLE_CND)
+ swap_convertibles(i, x);
+#endif
+
+ std::cout << "i= " << i << std::endl;
+ std::cout << "x= " << x << std::endl;
+ return 0;
+ }
+
+
+//]


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