Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71899 - sandbox/local/libs/local/example
From: lorcaminiti_at_[hidden]
Date: 2011-05-12 12:01:27


Author: lcaminiti
Date: 2011-05-12 12:01:26 EDT (Thu, 12 May 2011)
New Revision: 71899
URL: http://svn.boost.org/trac/boost/changeset/71899

Log:
Added typed bind examples.
Added:
   sandbox/local/libs/local/example/add_this_typed.cpp (contents, props changed)
   sandbox/local/libs/local/example/add_this_typed_va.cpp (contents, props changed)

Added: sandbox/local/libs/local/example/add_this_typed.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_this_typed.cpp 2011-05-12 12:01:26 EDT (Thu, 12 May 2011)
@@ -0,0 +1,61 @@
+
+// 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).
+
+//[ add_this_typed_cpp
+#include <boost/local/function.hpp>
+#include <boost/local/block.hpp>
+#include <boost/local/exit.hpp>
+#include <vector>
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <cassert>
+
+class adder {
+public:
+ adder(): sum_(0.0) {}
+
+ double sum(const std::vector<double>& nums, const int& factor = 10) {
+ std::ostringstream out;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (double num)
+ (const bind(const int&) factor) // Specify bind types
+ (bind(std::ostringstream)& out) // explicitly (so no
+ (bind(adder*) this) ) { // Boost.Typeof for binds).
+ this_->sum_ += factor * num;
+ out << "Summed: " << this_->sum_ << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ std::for_each(nums.begin(), nums.end(), add);
+
+ BOOST_LOCAL_EXIT( (const bind(std::ostringstream)& out) ) {
+ std::cout << out.str();
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_BLOCK( (bind(std::ostringstream)& out)
+ (const bind(adder*) this) ) {
+ out << "Asserted: " << this_->sum_ << std::endl;
+ assert(this_->sum_ > 0.0);
+ } BOOST_LOCAL_BLOCK_END
+
+ 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_this_typed_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_this_typed_va.cpp 2011-05-12 12:01:26 EDT (Thu, 12 May 2011)
@@ -0,0 +1,72 @@
+
+// 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).
+
+#include <boost/config.hpp>
+#if defined(BOOST_NO_VARIADIC_MACROS) || defined(BOOST_LOCAL_CONFIG_COMPLIANT)
+#include <iostream>
+int main() {
+ std::cerr << "Error: This program requires variadic macros" << std::endl;
+ return 0;
+}
+#else
+
+//[ add_this_typed_va_cpp
+#include <boost/local/function.hpp>
+#include <boost/local/block.hpp>
+#include <boost/local/exit.hpp>
+#include <vector>
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <cassert>
+
+class adder {
+public:
+ adder(): sum_(0.0) {}
+
+ double sum(const std::vector<double>& nums, const int& factor = 10) {
+ std::ostringstream out;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS(double num,
+ const bind(const int&) factor, // Specify bind types
+ bind(std::ostringstream)& out, // explicitly (so no
+ bind(adder*) this) { // Boost.Typeof for binds).
+ this_->sum_ += factor * num;
+ out << "Summed: " << this_->sum_ << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ std::for_each(nums.begin(), nums.end(), add);
+
+ BOOST_LOCAL_EXIT(const bind(std::ostringstream)& out) {
+ std::cout << out.str();
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_BLOCK(bind(std::ostringstream)& out,
+ const bind(adder*) this) {
+ out << "Asserted: " << this_->sum_ << std::endl;
+ assert(this_->sum_ > 0.0);
+ } BOOST_LOCAL_BLOCK_END
+
+ 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;
+}
+//]
+
+#endif
+


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