Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69229 - sandbox/local/libs/local/example
From: lorcaminiti_at_[hidden]
Date: 2011-02-23 22:51:09


Author: lcaminiti
Date: 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
New Revision: 69229
URL: http://svn.boost.org/trac/boost/changeset/69229

Log:
Added va examples.
Added:
   sandbox/local/libs/local/example/add_callouts.cpp (contents, props changed)
   sandbox/local/libs/local/example/add_num_sum_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/add_num_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/add_optimizers_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/add_this_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/add_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/block_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/doit_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/exit_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/find_if_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/transform_va.cpp (contents, props changed)

Added: sandbox/local/libs/local/example/add_callouts.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_callouts.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,38 @@
+//[add_callouts_cpp
+ 1.
+
+
+
+
+
+
+
+
+
+
+
+ 2.
+ 3.
+ 4.
+
+ 5.
+ 6.
+
+
+
+ 7.
+ 8.
+
+ 9.
+
+
+10.
+
+11.
+12.
+
+13.
+
+
+
+//]

Added: sandbox/local/libs/local/example/add_num_sum_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_num_sum_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,27 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Simple parenthesized syntax example used by the documentation.
+// Simplified syntax for variadic macros only.
+
+//[ add_num_sum_va_cpp
+#include <boost/local/function.hpp>
+#include <iostream>
+
+int main() {
+ double sum = 0.0;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS(double num, bind& sum) {
+ sum += num;
+ std::clog << "Summed: " << sum << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(100.0);
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/add_num_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_num_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,24 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Simple example used by the documentation.
+// Simplified syntax for variadic macros only.
+
+//[ add_num_va_cpp
+#include <boost/local/function.hpp>
+#include <iostream>
+
+int main() {
+ void BOOST_LOCAL_FUNCTION_PARAMS(double num) {
+ std::clog << num << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(100.0);
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/add_optimizers_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_optimizers_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,25 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Show how to use some optimizations (auto, register, etc).
+// Simplified syntax for variadic macros only.
+
+//[add_optimizers_va_cpp
+#include <boost/local/function.hpp>
+#include <iostream>
+
+int main() {
+ int BOOST_LOCAL_FUNCTION_PARAMS(auto int x, register int y) {
+ return x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ std::cout << add(3, 5) << std::endl;
+
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/add_this_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_this_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,48 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Simple parenthesized syntax example used by the documentation.
+// Simplified syntax for variadic macros only.
+
+//[ add_this_va_cpp
+#include <boost/local/function.hpp>
+#include <vector>
+#include <algorithm>
+#include <iostream>
+#include <cassert>
+
+class adder {
+public:
+ adder(): sum_(0.0) {}
+
+ double sum(const std::vector<double>& nums, const int& factor = 10) {
+ void BOOST_LOCAL_FUNCTION_PARAMS(double num, const bind factor,
+ bind this) {
+ this_->sum_ += factor * num; // Use `this_` instead of `this`.
+ std::clog << "Summed: " << this_->sum_ << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ std::for_each(nums.begin(), nums.end(), add);
+ return sum_;
+ }
+
+private:
+ double sum_;
+};
+
+int main() {
+ std::vector<double> v(3);
+ v[0] = 100.0; v[1] = 90.5; v[2] = 7.0;
+
+ adder a;
+ // sum = 10 * 100.0 + 10 * 90.5 + 10 * 7.0 = 1975.0
+ assert(a.sum(v) == 1975.0);
+
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/add_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,60 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Use all local constructs -- main motivating example for the documentation.
+// Adapted from C++0x lambda paper N2529 (added local blocks and exits).
+// Simplified syntax for variadic macros only.
+
+// NOTE: In this example `factor` is bound as const reference so to prevent
+// the local function to modify it. Alternatively, it could have been bound by
+// value as in `(bind)( (&sum) (factor) )` so to prevent modifications of
+// `factor` from within the local function to affect `factor` values outside
+// the local function scope (as done in N2529). However, binding `factor` by
+// value instead that as const reference would have introduced an extra copy
+// operation and, more importantly, it would have not enforced the logical
+// constraint that `factor` should not be modified at all from within the local
+// function body.
+
+//[ add_va_cpp
+#include <boost/local/function.hpp>
+#include <boost/local/block.hpp>
+#include <boost/local/exit.hpp>
+
+#include <algorithm>
+#include <iostream>
+#include <cassert>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS(double num,
+ const bind factor, bind& sum) {
+ sum += factor * num;
+ std::clog << "Summed: " << sum << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ add(100.0);
+
+ size_t size = 2;
+ double* nums = new double[size];
+ BOOST_LOCAL_EXIT(const bind& size, bind nums) {
+ if (size && nums) delete[] nums;
+ std::clog << "Freed array: " << nums << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ nums[0] = 90.5; nums[1] = 7.0;
+ std::for_each(nums, nums + size, add); // `add` as template parameter
+
+ BOOST_LOCAL_BLOCK(const bind& sum) {
+ assert(sum == 1975.0); // so far `sum` is 10*100+10*90.5+10*7=1975
+ std::clog << "Asserted summation: " << sum << std::endl;
+ } BOOST_LOCAL_BLOCK_END
+
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/block_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/block_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,40 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Use of local code blocks (with const and non-const binding).
+// Simplified syntax for variadic macros only.
+
+#include <boost/local/block.hpp>
+#include <cassert>
+
+struct c {
+ c(): x_(0) {}
+
+ void f(int& x) { // Non-const member function so `this` is not const.
+ // Non-const `this` but const `x`.
+ BOOST_LOCAL_BLOCK(const bind& x, bind this) {
+ this_->x_ = x; // Non-const `this` cant be modified.
+ assert(x == this_->x_); // Compiler error if `=` instead of `==`.
+
+ return; // Exit local block (not enclosing function).
+ assert(false); // Never executed.
+ } BOOST_LOCAL_BLOCK_END
+
+ x = x_; // Non-const `x` changed outside block.
+ }
+
+private:
+ int x_;
+};
+
+int main() {
+ c cc;
+ int x = 1;
+ cc.f(x);
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/doit_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/doit_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,64 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Pass local function as a parameter.
+// Adapted from C++0x lambda papaer N2529 (added default parameters).
+// Simplified syntax for variadic macros only.
+
+//[ doit_va_cpp
+#include <boost/local/function.hpp>
+#include <boost/function.hpp>
+#include <iostream>
+
+// Using `function` allows to use the same functor for all calls (regardless of
+// which set of default parameters is specified).
+void doit(boost::local::function< int (int, int), 2 > l) {
+ std::cout << l(1, 2) << std::endl;
+ std::cout << l(1) << std::endl;
+ std::cout << l() << std::endl;
+}
+
+// Using `function` requires different functors for calls with different set of
+// default parameters.
+void doit2(boost::function< int (int, int) > l)
+ { std::cout << l(1, 2) << std::endl; }
+void doit1(boost::function< int (int) > l) { std::cout << l(1) << std::endl; }
+void doit0(boost::function< int () > l) { std::cout << l() << std::endl; }
+
+int main() {
+ boost::function<int (int, int)> l2;
+
+ {
+ int i = 2;
+
+ int BOOST_LOCAL_FUNCTION_PARAMS(int x, default 1, int y, default 2,
+ bind i) {
+ return x + i * y;
+ } BOOST_LOCAL_FUNCTION_NAME(linear)
+
+ // Assign local functions variables.
+ boost::local::function< int (int, int), 2 > l = linear;
+ l(1, 2); l(1); l(); // All calls because of default parameters.
+ l2 = linear;
+ l2(1, 2); // Only one call operation (without default parameters).
+
+ // Similarly, pass local functions as a function parameter.
+ doit(linear);
+ std::cout << std::endl;
+ doit2(linear);
+ doit1(linear);
+ doit0(linear);
+ }
+
+ if (false) {
+ l2(1, 2); // This wouuld raise run-time error because invalid reference.
+ }
+
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/exit_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/exit_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,107 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Use local scope exit (with const, non-const, and `this` binding).
+// Adapted from Boost.ScopeExit documentation.
+
+#include <boost/local/exit.hpp>
+#include <boost/foreach.hpp>
+#include <boost/typeof/std/vector.hpp> // Typeof emulation mode.
+#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() // Typeof emulation mode.
+#include <vector>
+#include <ostream>
+#include <iostream>
+#include <cassert>
+
+class world;
+
+class person {
+ friend class world;
+public:
+ typedef unsigned int id_t;
+ typedef unsigned int evolution_t;
+
+ person(): id_(0), evolution_(0) {}
+
+ friend std::ostream& operator<<(std::ostream& o, person const& p)
+ { return o << "person: " << p.id_ << ", " << p.evolution_; }
+
+private:
+ id_t id_;
+ evolution_t evolution_;
+};
+
+BOOST_TYPEOF_REGISTER_TYPE(person)
+
+class world {
+public:
+ typedef unsigned int id_t;
+
+ world(): next_id_(1) {}
+
+ void add_person(person const& a_person);
+
+ friend std::ostream& operator<<(std::ostream& o, world const& w) {
+ o << "world: " << w.next_id_ << ", {";
+ BOOST_FOREACH(person const& p, w.persons_) {
+ o << ' ' << p << ',';
+ }
+ return o << "}";
+ }
+private:
+ id_t next_id_;
+ std::vector<person> persons_;
+};
+
+BOOST_TYPEOF_REGISTER_TYPE(world)
+
+void world::add_person(person const& a_person) {
+ persons_.push_back(a_person);
+
+ // This block must be no-throw.
+ person& p = persons_.back();
+ person::evolution_t checkpoint = p.evolution_;
+
+ BOOST_LOCAL_EXIT(const bind checkpoint, const bind& p, bind this) {
+ if (checkpoint == p.evolution_) this_->persons_.pop_back();
+ std::cout << "1st local exit" << std::endl;
+ return; // Exit local scope (not enclosing function).
+ assert(false);
+ } BOOST_LOCAL_EXIT_END
+
+ // ...
+
+ checkpoint = ++p.evolution_;
+
+ // Assign new id to the person.
+ world::id_t const prev_id = p.id_;
+ p.id_ = next_id_++;
+ BOOST_LOCAL_EXIT(const bind checkpoint, const bind prev_id,
+ bind& p, bind& next_id_) {
+ if (checkpoint == p.evolution_) {
+ next_id_ = p.id_;
+ p.id_ = prev_id;
+ }
+ std::cout << "2nd local exit" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ // ...
+
+ checkpoint = ++p.evolution_;
+}
+
+int main() {
+ person adamo, eva;
+ std::cout << adamo << std::endl;
+ std::cout << eva << std::endl;
+
+ world w;
+ w.add_person(adamo);
+ w.add_person(eva);
+ std::cout << w << std::endl;
+}
+

Added: sandbox/local/libs/local/example/find_if_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/find_if_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,47 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Pass local function to STL algorithm.
+// Adapted from C++0x lambda paper N2550.
+// Simplified syntax for variadic macros only.
+
+#include <boost/local/function.hpp>
+#include <list>
+#include <algorithm>
+#include <iostream>
+
+class employee {
+public:
+ explicit employee(const double& salary): salary_(salary) {}
+ double salary() const { return salary_; }
+private:
+ double salary_;
+};
+
+int main() {
+ std::list<employee> employees;
+ employees.push_back(employee( 85000.00));
+ employees.push_back(employee(100000.00));
+ employees.push_back(employee(120000.00));
+
+ double min_salary = 100000.00;
+ double u_limit = 1.1 * min_salary;
+
+ bool BOOST_LOCAL_FUNCTION_PARAMS(const employee& e,
+ const bind& min_salary, const bind& u_limit) {
+ return e.salary() >= min_salary && e.salary() < u_limit;
+ } BOOST_LOCAL_FUNCTION_NAME(between)
+
+ // Pass local function to an STL algorithm as a template paramter (this
+ // cannot be done with plain member funcitons of local classes).
+ std::list<employee>::iterator i = std::find_if(
+ employees.begin(), employees.end(), between);
+
+ if (i != employees.end()) { std::cout << i->salary() << std::endl; }
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/transform_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/transform_va.cpp 2011-02-23 22:51:07 EST (Wed, 23 Feb 2011)
@@ -0,0 +1,47 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the
+// Boost Software License, Version 1.0
+// (see accompanying file LICENSE_1_0.txt or a copy at
+// http://www.boost.org/LICENSE_1_0.txt).
+
+// Bind another local function.
+// Simplified syntax for variadic macros only.
+
+#include <boost/local/function.hpp>
+#include <iostream>
+#include <algorithm>
+#include <vector>
+
+int main () {
+ int offset = 5;
+ std::vector<int> v;
+ std::vector<int> w;
+
+ // v = 10 20 30 40 50
+ for (int i = 1; i < 6; i++) v.push_back(i * 10);
+ w.resize(v.size());
+
+ // w = ++v + 5 = 16 26 36 46 56
+ int BOOST_LOCAL_FUNCTION_PARAMS(int i, const bind& offset) {
+ // Compiler error if const `offset` modified here by mistake.
+ return ++i + offset;
+ } BOOST_LOCAL_FUNCTION_NAME(inc)
+ std::transform(v.begin(), v.end(), w.begin(), inc);
+
+ offset = 0;
+
+ // v = ++(v + w) + 0 = 27 47 67 87 107
+ int BOOST_LOCAL_FUNCTION_PARAMS(int i, int j,
+ bind inc) { // Bind another local function.
+ return inc(i + j); // Call the bound local function.
+ } BOOST_LOCAL_FUNCTION_NAME(inc_sum)
+ std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum);
+
+ for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
+ { std::cout << " " << *i; }
+ std::cout << 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