Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76682 - in sandbox/local_function/libs: local_function/example local_function/test utility utility/identity_type utility/identity_type/test
From: lorcaminiti_at_[hidden]
Date: 2012-01-25 09:22:11


Author: lcaminiti
Date: 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
New Revision: 76682
URL: http://svn.boost.org/trac/boost/changeset/76682

Log:
Upd.
Added:
   sandbox/local_function/libs/local_function/example/boost_phoenix_factorial.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/example/boost_phoenix_local_factorial.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/test/goto_err.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/test/goto_ok.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/test/macro_commas.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/test/operator_err.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/test/operator_error.cpp (contents, props changed)
   sandbox/local_function/libs/local_function/test/operator_ok.cpp (contents, props changed)
   sandbox/local_function/libs/utility/
   sandbox/local_function/libs/utility/identity_type/
   sandbox/local_function/libs/utility/identity_type/test/

Added: sandbox/local_function/libs/local_function/example/boost_phoenix_factorial.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/example/boost_phoenix_factorial.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,34 @@
+
+#include <boost/phoenix/core.hpp>
+#include <boost/phoenix/function.hpp>
+#define BOOST_TEST_MODULE ExampleBoostPhoenixFactorial
+#include <boost/test/unit_test.hpp>
+
+//[example_boost_phoenix_factorial
+struct factorial_impl { // Phoenix function from global functor.
+ template<typename Sig>
+ struct result;
+
+ template<typename This, typename Arg>
+ struct result<This (Arg)> : result<This (Arg const&)> {};
+
+ template<typename This, typename Arg>
+ struct result<This (Arg&)> { typedef Arg type; };
+
+ template<typename Arg> // Polymorphic.
+ Arg operator()(Arg n) const {
+ return (n <= 0) ? 1 : n * (*this)(n - 1);
+ }
+};
+
+BOOST_AUTO_TEST_CASE( example_boost_phoenix_factorial ) {
+ using boost::phoenix::arg_names::arg1;
+
+ boost::phoenix::function<factorial_impl> factorial;
+
+ int i = 4;
+ BOOST_CHECK( factorial(i)() == 24 ); // Call.
+ BOOST_CHECK( factorial(arg1)(i) == 24 ); // Lazy call.
+}
+//]
+

Added: sandbox/local_function/libs/local_function/example/boost_phoenix_local_factorial.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/example/boost_phoenix_local_factorial.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,25 @@
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#include <boost/phoenix/core.hpp>
+#include <boost/phoenix/function.hpp>
+#define BOOST_TEST_MODULE ExampleBoostPhoenixFactorialLocal
+#include <boost/test/unit_test.hpp>
+
+//[example_boost_phoenix_local_factorial
+BOOST_AUTO_TEST_CASE( example_boost_phoenix_factorial_local ) {
+ using boost::phoenix::arg_names::arg1;
+
+ int BOOST_LOCAL_FUNCTION(int n) { // Monomorphic.
+ return (n <= 0) ? 1 : n * factorial_impl(n - 1);
+ } BOOST_LOCAL_FUNCTION_NAME(recursive factorial_impl)
+
+ boost::phoenix::function< boost::function<int (int)> >
+ factorial(factorial_impl); // Phoenix function from local function.
+
+ int i = 4;
+ BOOST_CHECK( factorial(i)() == 24 ); // Call.
+ BOOST_CHECK( factorial(arg1)(i) == 24 ); // Lazy call.
+}
+//]
+

Added: sandbox/local_function/libs/local_function/test/goto_err.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/test/goto_err.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,25 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestGotoErr
+#include <boost/test/unit_test.hpp>
+
+//[test_goto_err
+int error(int x, int y) {
+ bool BOOST_LOCAL_FUNCTION(int x) {
+ if(x <= 0) goto failure; // Error: Cannot jump to enclosing scope.
+ else goto success; // OK: Can jump within local function.
+
+ success:
+ return 0;
+ } BOOST_LOCAL_FUNCTION_NAME(validate)
+
+ return validate(x + y);
+faliure:
+ return -1;
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_goto_err ) {
+ error(1, 2);
+}
+

Added: sandbox/local_function/libs/local_function/test/goto_ok.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/test/goto_ok.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,22 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestGotoOk
+#include <boost/test/unit_test.hpp>
+
+//[test_goto_ok
+int error(int x, int y) {
+ bool BOOST_LOCAL_FUNCTION(int x) {
+ if(x > 0) goto success; // OK: Can jump within local function.
+ return -1;
+ success:
+ return 0;
+ } BOOST_LOCAL_FUNCTION_NAME(validate)
+
+ return validate(x + y);
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_goto_ok ) {
+ error(1, 2);
+}
+

Added: sandbox/local_function/libs/local_function/test/macro_commas.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/test/macro_commas.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,38 @@
+
+#include <boost/local_function.hpp>
+#include <boost/utility/identity_type.hpp>
+#include <boost/config.hpp>
+//#define BOOST_TEST_MODULE TestMacroCommas
+//#include <boost/test/unit_test.hpp>
+#include <map>
+#include <string>
+
+std::string cat(const std::string& x, const std::string& y) { return x + y; }
+
+template<typename V, typename K>
+struct key_sizeof {
+ BOOST_STATIC_CONSTANT(size_t, value = sizeof(K));
+};
+
+typedef int sign_t;
+
+//BOOST_AUTO_TEST_CASE( test_macro_commas ) {
+int main(void) {
+ //[test_macro_commas
+ void BOOST_LOCAL_FUNCTION(
+ ((const std::map<std::string, size_t>&)) m,
+ BOOST_IDENTITY_TYPE((::sign_t)) sign,
+ const size_t& factor,
+ default (key_sizeof<std::string, size_t>::value),
+ const std::string& separator, default cat(":", " ")
+ ) {
+ // Do something...
+ } BOOST_LOCAL_FUNCTION_NAME(f)
+ //]
+
+ std::map<std::string, size_t> m;
+ ::sign_t sign = -1;
+ f(m, sign);
+ return 0;
+}
+

Added: sandbox/local_function/libs/local_function/test/operator_err.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/test/operator_err.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,22 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestOperatorErr
+#include <boost/test/unit_test.hpp>
+
+//[test_operator_err
+struct point {
+ int x;
+ int y;
+};
+
+BOOST_AUTO_TEST_CASE( test_operator_err ) {
+ bool BOOST_LOCAL_FUNCTION(const point& p, const point& q) {
+ return p.x == q.x && p.y == q.y;
+ } BOOST_LOCAL_FUNCTION_NAME(operator==) // Error: Cannot use `operator...`.
+
+ point a; a.x = 1; a.y = 2;
+ point b = a;
+ BOOST_CHECK( a == b );
+}
+//]
+

Added: sandbox/local_function/libs/local_function/test/operator_error.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/test/operator_error.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,16 @@
+
+struct point {
+ int x;
+ int y;
+};
+
+BOOST_AUTO_TEST_CASE( operator_error ) {
+ bool BOOST_LOCAL_FUNCTION(const point& p, const point& q) {
+ return p.x == q.x && p.y == q.y;
+ } BOOST_LOCAL_FUNCTION(operator==) // Error: Cannot use `operator==`.
+
+ point a; a.x = 1; a.y = 2;
+ point b = a;
+ BOOST_CHECK( a == b );
+}
+

Added: sandbox/local_function/libs/local_function/test/operator_ok.cpp
==============================================================================
--- (empty file)
+++ sandbox/local_function/libs/local_function/test/operator_ok.cpp 2012-01-25 09:22:10 EST (Wed, 25 Jan 2012)
@@ -0,0 +1,22 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestOperatorOk
+#include <boost/test/unit_test.hpp>
+
+//[test_operator_ok
+struct point {
+ int x;
+ int y;
+};
+
+BOOST_AUTO_TEST_CASE( test_operator_ok ) {
+ bool BOOST_LOCAL_FUNCTION(const point& p, const point& q) {
+ return p.x == q.x && p.y == q.y;
+ } BOOST_LOCAL_FUNCTION_NAME(equal) // OK: not using `operator...`.
+
+ point a; a.x = 1; a.y = 2;
+ point b = a;
+ BOOST_CHECK( equal(a, b) );
+}
+//]
+


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