Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69594 - in sandbox/local: . boost/local/aux_/function_macros/code_ libs/local/example
From: lorcaminiti_at_[hidden]
Date: 2011-03-05 19:38:52


Author: lcaminiti
Date: 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
New Revision: 69594
URL: http://svn.boost.org/trac/boost/changeset/69594

Log:
Added GCC nested function example (not all compiling).
Added D-style scope guards (exit, success, and failure) examples.
Added:
   sandbox/local/libs/local/example/gcc-access_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/gcc-goto.cpp (contents, props changed)
   sandbox/local/libs/local/example/gcc-square.cpp (contents, props changed)
   sandbox/local/libs/local/example/gcc-square_va.cpp (contents, props changed)
   sandbox/local/libs/local/example/gcc-store.cpp (contents, props changed)
   sandbox/local/libs/local/example/scope_guards_errno.cpp (contents, props changed)
   sandbox/local/libs/local/example/scope_guards_except.cpp (contents, props changed)
   sandbox/local/libs/local/example/scope_guards_se.cpp (contents, props changed)
Text files modified:
   sandbox/local/TODO.txt | 6 ++++++
   sandbox/local/boost/local/aux_/function_macros/code_/functor.hpp | 3 +++
   2 files changed, 9 insertions(+), 0 deletions(-)

Modified: sandbox/local/TODO.txt
==============================================================================
--- sandbox/local/TODO.txt (original)
+++ sandbox/local/TODO.txt 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -10,9 +10,15 @@
 
 * Docs: If `, default ...` syntax seems strange for C99, programmers can always use `#define WITH_DEFAULT(x) , default x` or similar macro instead.
 
+* Docs: Add D-Stype Scope Guards examples to docs.
+
 
 * Code: Try seriously to make nested locals to work...
 
+* Code: Add `BOOST_LOCAL_CONFIG_COMPLIANT` macro (#undef by default) that forces 100% C++ standard compliant if #defined -- no variadic and no empy macro params. Similar to Boost.Typeof COMPLIANT -- but programmers must also #define Boost.Typeof COMPLIANT separately if they wish to do so.
+
+* Code: Make the member variable with the local function name used for recursion const so body cannot change it -- or better, make the body funciton const so it cannot change anything about the object (if possible)...
+
 * Code: Optimize macro expansion code for C++0x, C++03, etc. From John Bytheway:
> Yes, in C++0x local structs can be passed as template parameters.
> Obviously, in C++0x there are lambdas too, so you might think your

Modified: sandbox/local/boost/local/aux_/function_macros/code_/functor.hpp
==============================================================================
--- sandbox/local/boost/local/aux_/function_macros/code_/functor.hpp (original)
+++ sandbox/local/boost/local/aux_/function_macros/code_/functor.hpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -281,6 +281,9 @@
                         BOOST_LOCAL_AUX_FUNCTION_CODE_PARAM_UNBIND_DECL_WITH_DEFAULT, \
                         ~, unbinds) \
             ) /* end body function params */ \
+ /* const member func so it cannot change obj (reassign member */ \
+ /* var with local function name, etc) */ \
+ const \
             /* user local function definition `{ ... }` will follow here */ \
     /* `NAME` macro will close function class decl `};` here */
 

Added: sandbox/local/libs/local/example/gcc-access_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/gcc-access_va.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,14 @@
+
+#include <boost/local/function.hpp>
+
+void bar(int* array, int offset, int size) {
+ int BOOST_LOCAL_FUNCTION_PARAMS(int* array, int index, const bind offset) {
+ return array[index + offset];
+ }
+
+ for (int i = 0; i < size; ++i) {
+ std::cout << access(array, i) << std::endl;
+ }
+}
+
+

Added: sandbox/local/libs/local/example/gcc-goto.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/gcc-goto.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,26 @@
+
+#include <boost/local/function.hpp>
+#include <iostream>
+
+int print(int* array, int offset, int size) {
+ int BOOST_LOCAL_FUNCTION_PARAMS( (int* array) (int index)
+ (const bind offset) (const bind size) ) {
+ if (index > size) goto failure;
+ return array[index + offset];
+ } BOOST_LOCAL_FUNCTION_NAME(access)
+
+ for (int i = 0; i < size; ++i) {
+ std::cout << access(array, i) << std::endl;
+ }
+ return 0;
+
+failure:
+ return -1;
+}
+
+int main() {
+ int a[] = {1, 2, 3, 4, 5};
+ print(a, 1, 2);
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/gcc-square.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/gcc-square.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,13 @@
+
+#include <boost/local/function.hpp>
+
+void add_square(double a, double b) {
+ double BOOST_LOCAL_FUNCTION(double z) {
+ return z * z;
+ } BOOST_LOCAL_FUNCTION_NAME(suare)
+
+ return square(a) + square(b);
+}
+
+
+

Added: sandbox/local/libs/local/example/gcc-square_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/gcc-square_va.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,13 @@
+
+#include <boost/local/function.hpp>
+
+void add_square(double a, double b) {
+ double BOOST_LOCAL_FUNCTION(double z) {
+ return z * z;
+ } BOOST_LOCAL_FUNCTION_NAME(suare)
+
+ return square(a) + square(b);
+}
+
+
+

Added: sandbox/local/libs/local/example/gcc-store.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/gcc-store.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,14 @@
+
+void intermediate(void (*store_func)(int, int), int size) {
+ store_func(size - 1, -1);
+}
+
+void hack(int* array, int size) {
+ void BOOST_LOCAL_FUNCTION_PARAMS(int index, int value, bind array) {
+ array[index] = value;
+ } BOOST_LOCAL_FUNCTION_NAME(store)
+
+ intermediate(store, size);
+}
+
+

Added: sandbox/local/libs/local/example/scope_guards_errno.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/scope_guards_errno.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,30 @@
+
+#include <boost/local/exit.hpp>
+#include <iostream>
+
+int f() {
+ int error = 0; // No error to start with.
+
+ BOOST_LOCAL_EXIT( (void) ) { // Same as D's `scope(exit)`.
+ std::cout << "exit" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_EXIT( (const bind& error) ) { // Sane as D's `scope(success)`.
+ if (!error) std::cout << "success" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_EXIT( (const bind& error) ) { // Same as D's `scope(failure)`.
+ if (error) std::cout << "failure" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ // Cannot use `return <NUMBER>` otherwise scope exits do not know the
+ // error number. Set error and exit using `return error` instead.
+ error = -2; return error;
+ error = -1; return error;
+}
+
+int main() {
+ std::cout << "error number: " << f() << std::endl;
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/scope_guards_except.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/scope_guards_except.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,74 @@
+
+#include <boost/local/exit.hpp>
+#include <iostream>
+#include <stdexcept>
+
+struct exception_error_interface {
+ virtual operator bool() const = 0;
+ virtual void throw_if() const = 0;
+};
+
+template<typename E>
+struct exception_error: exception_error_interface {
+ template<typename A0>
+ explicit exception_error(A0 arg0): ex_(E(arg0)) {}
+ template<typename A0, typename A1>
+ exception_error(A0 arg0, A1 arg1): ex_(E(arg0, arg1)) {}
+ // Add more constructors for more parameters if needed...
+
+ operator bool() const { return true; }
+ void throw_if() const { throw ex_; }
+
+private:
+ E ex_; // I need the exception type `E` here so I can throw it later.
+};
+
+template<>
+struct exception_error<void> { // Use `void` for no error (cannot throw void).
+ exception_error(): err_() {} // No error by default.
+
+ template<typename E> // Construct with some error.
+ /* implicit */ exception_error(exception_error<E> const& err): err_(&err) {}
+ template<typename E> // Set some error.
+ exception_error& operator=(exception_error<E> const& err)
+ { err_ = &err; return *this; }
+
+ operator bool() const { return err_ && bool(*err_); }
+ // Select proper type to throw via polymorphism.
+ void throw_if() const { if (err_) err_->throw_if(); }
+
+private:
+ exception_error_interface const* err_;
+};
+
+void f() {
+ exception_error<void> error; // No error (i.e., `void`) to start with.
+ // This scope exit is special -- it's used to throw on exit if error.
+ BOOST_LOCAL_EXIT( (const bind& error) ) {
+ std::cout << "throwing if error" << std::endl;
+ error.throw_if(); // Throws if error set not to `exception_error<void>`.
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_EXIT( (void) ) { // Smae as D's `scope(exit)`.
+ std::cout << "exit" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_EXIT( (const bind& error) ) { // Same as D's `scope(success)`.
+ if (!error) std::cout << "success" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ BOOST_LOCAL_EXIT( (const bind& error) ) { // Same as D's `scope(failure)`.
+ if (error) std::cout << "failure" << std::endl;
+ } BOOST_LOCAL_EXIT_END
+
+ // Cannot use `throw` otherwise scope exits are not executed. Set error
+ // and exit using `return` instead (the 1st scope exit will throw).
+ error = exception_error<std::runtime_error>("some error"); return;
+ error = exception_error<int>(-1); return;
+}
+
+int main() {
+ f();
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/scope_guards_se.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/scope_guards_se.cpp 2011-03-05 19:38:44 EST (Sat, 05 Mar 2011)
@@ -0,0 +1,20 @@
+
+#include <boost/scope_exit.hpp>
+#include <iostream>
+#include <stdexcept>
+
+void f() {
+ bool error = false;
+
+ BOOST_SCOPE_EXIT( (&error) ) {
+ std::cout << "returning" << std::endl; // Not executed on throw.
+ } BOOST_SCOPE_EXIT_END
+
+ throw std::runtime_error("some error");
+}
+
+int main() {
+ f();
+ 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