源码提交

This commit is contained in:
yangzuhao
2022-12-15 23:45:23 +08:00
parent cb7ca21a2e
commit b199dd2a76
63375 changed files with 9928567 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
# Copyright (C) 2009-2012 Lorenzo Caminiti
# Distributed under 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)
# Home at http://www.boost.org/libs/local_function
import testing ;
# Sun does not automatically detect type-of emulation mode (force it).
project : requirements <toolset>sun:<define>BOOST_TYPEOF_EMULATION ;
rule vaseq ( command target )
{
$(command) $(target).cpp ;
$(command) $(target)_seq.cpp ;
$(command) $(target)_seq_nova.cpp ;
}
vaseq run add ;
vaseq run add_classifiers ;
vaseq run add_default ;
vaseq run add_except ;
vaseq run add_inline ;
vaseq run add_params_only ;
vaseq run add_template ;
vaseq run add_this ;
vaseq run add_typed ;
vaseq run add_with_default ;
vaseq run all_decl ;
vaseq run factorial ;
vaseq run goto ;
vaseq compile-fail goto_error ;
vaseq run macro_commas ;
vaseq run nesting ;
vaseq run operator ;
vaseq compile-fail operator_error ;
vaseq run overload ;
vaseq run return_assign ;
vaseq run return_derivative ;
vaseq run return_inc ;
vaseq run return_setget ;
vaseq run return_this ;
vaseq run same_line ;
vaseq run transform ;
vaseq run typeof ;
vaseq run typeof_template ;
run ten_void.cpp ;
run ten_void_nova.cpp ;
+35
View File
@@ -0,0 +1,35 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
//[add
int main(void) { // Some local scope.
int sum = 0, factor = 10; // Variables in scope to bind.
void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
add(1); // Call the local function.
int nums[] = {2, 3};
std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
BOOST_TEST(sum == 60); // Assert final summation value.
return boost::report_errors();
}
//]
#endif // VARIADIC_MACROS
@@ -0,0 +1,30 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
# error "auto-declarations not allowed (using `auto` as storage classifier)"
#elif defined(BOOST_NO_CXX11_VARIADIC_MACROS)
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
//[add_classifiers
int BOOST_LOCAL_FUNCTION(auto int x, register int y) { // Classifiers.
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
//]
BOOST_TEST(add(1, 2) == 3);
return boost::report_errors();
}
#endif // AUTO_DECLARATIONS && VARIADIC_MACROS
@@ -0,0 +1,26 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifndef BOOST_NO_CXX11_AUTO_DECLARATIONS
# error "auto-declarations not allowed (using `auto` as storage classifier)"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
int BOOST_LOCAL_FUNCTION( (auto int x) (register int y) ) {
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1, 2) == 3);
return boost::report_errors();
}
#endif // AUTO_DECLARATIONS
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_classifiers_seq.cpp"
+28
View File
@@ -0,0 +1,28 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
//[add_default
int BOOST_LOCAL_FUNCTION(int x, int y, default 2) { // Default parameter.
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1) == 3);
//]
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,19 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
int BOOST_LOCAL_FUNCTION( (int x) (int y)(default 2) ) {
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1) == 3);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_default_seq.cpp"
+34
View File
@@ -0,0 +1,34 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
//[add_except
double sum = 0.0;
int factor = 10;
void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum,
double num) throw() { // Throw nothing.
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
add(100);
//]
BOOST_TEST(sum == 1000);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,25 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
double sum = 0.0;
int factor = 10;
void BOOST_LOCAL_FUNCTION( (const bind factor) (bind& sum)
(double num) ) throw() {
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
add(100);
BOOST_TEST(sum == 1000);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_except_seq.cpp"
+37
View File
@@ -0,0 +1,37 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <algorithm>
int main(void) {
//[add_inline
int sum = 0, factor = 10;
void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(inline add) // Inlining.
std::vector<int> v(100);
std::fill(v.begin(), v.end(), 1);
for(size_t i = 0; i < v.size(); ++i) add(v[i]); // Cannot use for_each.
//]
BOOST_TEST(sum == 1000);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,28 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <algorithm>
int main(void) {
int sum = 0, factor = 10;
void BOOST_LOCAL_FUNCTION( (const bind factor) (bind& sum) (int num) ) {
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(inline add)
std::vector<int> v(100);
std::fill(v.begin(), v.end(), 1);
for(size_t i = 0; i < v.size(); ++i) add(v[i]);
BOOST_TEST(sum == 1000);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_inline_seq.cpp"
@@ -0,0 +1,28 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
//[add_params_only
int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function.
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1, 2) == 3); // Local function call.
//]
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,19 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
int BOOST_LOCAL_FUNCTION( (int x) (int y) ) {
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1, 2) == 3);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_params_only_seq.cpp"
+28
View File
@@ -0,0 +1,28 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
//[add_seq
int main(void) {
int sum = 0, factor = 10;
void BOOST_LOCAL_FUNCTION( (const bind factor) (bind& sum) (int num) ) {
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
add(1);
int nums[] = {2, 3};
std::for_each(nums, nums + 2, add);
BOOST_TEST(sum == 60);
return boost::report_errors();
}
//]
+10
View File
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_seq.cpp"
+41
View File
@@ -0,0 +1,41 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
//[add_template
template<typename T>
T total(const T& x, const T& y, const T& z) {
T sum = T(), factor = 10;
// Must use the `..._TPL` macros within templates.
T BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) {
return sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME_TPL(add)
add(x);
T nums[2]; nums[0] = y; nums[1] = z;
std::for_each(nums, nums + 2, add);
return sum;
}
//]
int main(void) {
BOOST_TEST(total(1, 2, 3) == 60);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,31 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
template<typename T>
T total(const T& x, const T& y, const T& z) {
T sum = T(), factor = 10;
T BOOST_LOCAL_FUNCTION_TPL( (const bind factor) (bind& sum) (T num) ) {
return sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME_TPL(add)
add(x);
T nums[2]; nums[0] = y; nums[1] = z;
std::for_each(nums, nums + 2, add);
return sum;
}
int main(void) {
BOOST_TEST(total(1, 2, 3) == 60);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_template_seq.cpp"
+51
View File
@@ -0,0 +1,51 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <algorithm>
struct adder;
BOOST_TYPEOF_REGISTER_TYPE(adder) // Register before `bind this_` below.
//[add_this
struct adder {
adder() : sum_(0) {}
int sum(const std::vector<int>& nums, const int factor = 10) {
void BOOST_LOCAL_FUNCTION(const bind factor, bind this_, int num) {
this_->sum_ += factor * num; // Use `this_` instead of `this`.
} BOOST_LOCAL_FUNCTION_NAME(add)
std::for_each(nums.begin(), nums.end(), add);
return sum_;
}
private:
int sum_;
};
//]
int main(void) {
std::vector<int> v(3);
v[0] = 1; v[1] = 2; v[2] = 3;
BOOST_TEST(adder().sum(v) == 60);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
+43
View File
@@ -0,0 +1,43 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <algorithm>
struct adder;
BOOST_TYPEOF_REGISTER_TYPE(adder) // Register before `bind this_` below.
struct adder {
adder() : sum_(0) {}
int sum(const std::vector<int>& nums, const int factor = 10) {
void BOOST_LOCAL_FUNCTION( (const bind factor) (bind this_)
(int num) ) {
this_->sum_ += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
std::for_each(nums.begin(), nums.end(), add);
return sum_;
}
private:
int sum_;
};
int main(void) {
std::vector<int> v(3);
v[0] = 1; v[1] = 2; v[2] = 3;
BOOST_TEST(adder().sum(v) == 60);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_this_seq.cpp"
+47
View File
@@ -0,0 +1,47 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <algorithm>
//[add_typed
struct adder {
adder(void) : sum_(0) {}
int sum(const std::vector<int>& nums, const int& factor = 10) {
// Explicitly specify bound variable and return types (no type-of).
BOOST_LOCAL_FUNCTION(const bind(const int&) factor,
bind(adder*) this_, int num, return int) {
return this_->sum_ += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
std::for_each(nums.begin(), nums.end(), add);
return sum_;
}
private:
int sum_;
};
//]
int main(void) {
std::vector<int> v(3);
v[0] = 1; v[1] = 2; v[2] = 3;
BOOST_TEST(adder().sum(v) == 60);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,37 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <algorithm>
struct adder {
adder(void) : sum_(0) {}
int sum(const std::vector<int>& nums, const int& factor = 10) {
BOOST_LOCAL_FUNCTION( (const bind(const int&) factor)
(bind(adder*) this_) (int num) (return int) ) {
return this_->sum_ += factor * num;
} BOOST_LOCAL_FUNCTION_NAME(add)
std::for_each(nums.begin(), nums.end(), add);
return sum_;
}
private:
int sum_;
};
int main(void) {
std::vector<int> v(3);
v[0] = 1; v[1] = 2; v[2] = 3;
BOOST_TEST(adder().sum(v) == 60);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_typed_seq.cpp"
@@ -0,0 +1,32 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
//[add_with_default_macro
#define WITH_DEFAULT , default
//]
int main(void) {
//[add_with_default
int BOOST_LOCAL_FUNCTION(int x, int y WITH_DEFAULT 2) { // Default.
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1) == 3);
//]
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,21 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#define WITH_DEFAULT )(default
int main(void) {
int BOOST_LOCAL_FUNCTION( (int x) (int y WITH_DEFAULT 2) ) {
return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)
BOOST_TEST(add(1) == 3);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "add_with_default_seq.cpp"
+26
View File
@@ -0,0 +1,26 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#ifndef ADDABLE_HPP_
#define ADDABLE_HPP_
#include <boost/concept_check.hpp>
template<typename T>
struct Addable { // User-defined concept.
BOOST_CONCEPT_USAGE(Addable) {
return_type(x + y); // Check addition `T operator+(T x, T y)`.
}
private:
void return_type(T) {} // Implementation (required for some linkers).
static T const& x;
static T const& y;
};
#endif // #include guard
+177
View File
@@ -0,0 +1,177 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
struct s;
BOOST_TYPEOF_REGISTER_TYPE(s) // Register before binding `this_` below.
// Compile all local function declaration combinations.
struct s {
void f(double p = 1.23, double q = -1.23) {
{ // Only params.
void BOOST_LOCAL_FUNCTION(int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
}
{ // Only const binds.
int a, b;
const int& BOOST_LOCAL_FUNCTION(const bind a,
const bind& b, const bind& p, const bind q) {
return b;
} BOOST_LOCAL_FUNCTION_NAME(l)
l();
const s& BOOST_LOCAL_FUNCTION(const bind this_) {
return *this_;
} BOOST_LOCAL_FUNCTION_NAME(t)
t();
const int BOOST_LOCAL_FUNCTION(const bind a,
const bind& b, const bind& p, const bind q,
const bind this_) {
return a;
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt();
}
{ // Only plain binds.
int c, d;
int& BOOST_LOCAL_FUNCTION(bind c, bind& d,
bind& p, bind& q) {
return d;
} BOOST_LOCAL_FUNCTION_NAME(l)
l();
s& BOOST_LOCAL_FUNCTION(bind this_) {
return *this_;
} BOOST_LOCAL_FUNCTION_NAME(t)
t();
int BOOST_LOCAL_FUNCTION(bind c, bind& d,
bind& p, bind& q, bind this_) {
return c;
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt();
}
{ // Both params and const binds.
int a, b;
void BOOST_LOCAL_FUNCTION(const bind a, const bind& b,
const bind& p, const bind q,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
void BOOST_LOCAL_FUNCTION(const bind this_,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(t)
t(1);
void BOOST_LOCAL_FUNCTION(const bind a, const bind this_,
const bind& b, const bind& p, const bind q,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt(1);
}
{ // Both params and plain binds.
int c, d;
void BOOST_LOCAL_FUNCTION(bind c, bind& d, bind& p, bind q,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
void BOOST_LOCAL_FUNCTION(bind this_,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(t)
t(1);
void BOOST_LOCAL_FUNCTION(bind c, bind& d,
bind& p, bind this_, bind q,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt(1);
}
{ // Both const and plain binds.
int a, b, c, d;
void BOOST_LOCAL_FUNCTION(const bind a, const bind& b,
const bind p, bind c, bind& d, bind q) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l();
void BOOST_LOCAL_FUNCTION(const bind this_,
bind c, bind& d, bind q) {
} BOOST_LOCAL_FUNCTION_NAME(ct)
ct();
void BOOST_LOCAL_FUNCTION(const bind this_,
const bind a, const bind& b, const bind p,
bind c, bind& d, bind q) {
} BOOST_LOCAL_FUNCTION_NAME(lct)
lct();
void BOOST_LOCAL_FUNCTION(const bind a, const bind& b,
const bind p, bind this_) {
} BOOST_LOCAL_FUNCTION_NAME(pt)
pt();
void BOOST_LOCAL_FUNCTION(const bind a, const bind& b,
const bind p, bind c, bind this_, bind& d, bind q) {
} BOOST_LOCAL_FUNCTION_NAME(lpt)
lpt();
}
{ // All params, const binds, and plain binds.
int a, b, c, d;
void BOOST_LOCAL_FUNCTION(
const bind a, const bind& b, const bind& p,
bind c, bind& d, bind& q, int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
void BOOST_LOCAL_FUNCTION(const bind this_,
bind c, bind& d, bind& q,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(ct)
ct(1);
void BOOST_LOCAL_FUNCTION(
const bind a, const bind& b, const bind& p,
bind this_, int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(pt)
pt(1);
void BOOST_LOCAL_FUNCTION(const bind a, const bind this_,
const bind& b, const bind& p, bind c, bind& d,
bind& q, int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(lct)
lct(1);
void BOOST_LOCAL_FUNCTION(const bind a, const bind& b,
const bind& p, bind c, bind& d, bind this_, bind& q,
int x, int y, default 0) {
} BOOST_LOCAL_FUNCTION_NAME(lpt)
lpt(1);
}
}
};
int main(void) {
s().f();
return 0;
}
#endif // VARIADIC_MACROS
+170
View File
@@ -0,0 +1,170 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
struct s;
BOOST_TYPEOF_REGISTER_TYPE(s); // Register before bind `this_` below.
// Compile all local function declaration combinations.
struct s {
void f(double p = 1.23, double q = -1.23) {
{ // Only params.
void BOOST_LOCAL_FUNCTION( (int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
}
{ // Only const binds.
int a, b;
const int& BOOST_LOCAL_FUNCTION( (const bind a)
(const bind& b) (const bind& p) (const bind q) ) {
return b;
} BOOST_LOCAL_FUNCTION_NAME(l)
l();
const s& BOOST_LOCAL_FUNCTION( (const bind this_) ) {
return *this_;
} BOOST_LOCAL_FUNCTION_NAME(t)
t();
const int BOOST_LOCAL_FUNCTION( (const bind a)
(const bind& b) (const bind& p) (const bind q)
(const bind this_) ) {
return a;
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt();
}
{ // Only plain binds.
int c, d;
int& BOOST_LOCAL_FUNCTION( (bind c) (bind& d)
(bind& p) (bind& q) ) {
return d;
} BOOST_LOCAL_FUNCTION_NAME(l)
l();
s& BOOST_LOCAL_FUNCTION( (bind this_) ) {
return *this_;
} BOOST_LOCAL_FUNCTION_NAME(t)
t();
int BOOST_LOCAL_FUNCTION( (bind c) (bind& d)
(bind& p) (bind& q) (bind this_) ) {
return c;
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt();
}
{ // Both params and const binds.
int a, b;
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind& b)
(const bind& p) (const bind q)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
void BOOST_LOCAL_FUNCTION( (const bind this_)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(t)
t(1);
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind this_)
(const bind& b) (const bind& p) (const bind q)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt(1);
}
{ // Both params and plain binds.
int c, d;
void BOOST_LOCAL_FUNCTION( (bind c) (bind& d) (bind& p) (bind q)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
void BOOST_LOCAL_FUNCTION( (bind this_)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(t)
t(1);
void BOOST_LOCAL_FUNCTION( (bind c) (bind& d)
(bind& p) (bind this_) (bind q)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(lt)
lt(1);
}
{ // Both const and plain binds.
int a, b, c, d;
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind& b)
(const bind p) (bind c) (bind& d) (bind q) ) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l();
void BOOST_LOCAL_FUNCTION( (const bind this_)
(bind c) (bind& d) (bind q) ) {
} BOOST_LOCAL_FUNCTION_NAME(ct)
ct();
void BOOST_LOCAL_FUNCTION( (const bind this_)
(const bind a) (const bind& b) (const bind p)
(bind c) (bind& d) (bind q) ) {
} BOOST_LOCAL_FUNCTION_NAME(lct)
lct();
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind& b)
(const bind p) (bind this_) ) {
} BOOST_LOCAL_FUNCTION_NAME(pt)
pt();
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind& b)
(const bind p) (bind c) (bind this_) (bind& d) (bind q) ) {
} BOOST_LOCAL_FUNCTION_NAME(lpt)
lpt();
}
{ // All params, const binds, and plain binds.
int a, b, c, d;
void BOOST_LOCAL_FUNCTION(
(const bind a) (const bind& b) (const bind& p)
(bind c) (bind& d) (bind& q) (int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(l)
l(1);
void BOOST_LOCAL_FUNCTION( (const bind this_)
(bind c) (bind& d) (bind& q)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(ct)
ct(1);
void BOOST_LOCAL_FUNCTION(
(const bind a) (const bind& b) (const bind& p)
(bind this_) (int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(pt)
pt(1);
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind this_)
(const bind& b) (const bind& p) (bind c) (bind& d)
(bind& q) (int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(lct)
lct(1);
void BOOST_LOCAL_FUNCTION( (const bind a) (const bind& b)
(const bind& p) (bind c) (bind& d) (bind this_) (bind& q)
(int x) (int y)(default 0) ) {
} BOOST_LOCAL_FUNCTION_NAME(lpt)
lpt(1);
}
}
};
int main(void) {
s().f();
return 0;
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "all_decl_seq.cpp"
+57
View File
@@ -0,0 +1,57 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
#include <vector>
struct calculator;
BOOST_TYPEOF_REGISTER_TYPE(calculator) // Register before `bind this_` below.
//[factorial
struct calculator {
std::vector<int> results;
void factorials(const std::vector<int>& nums) {
int BOOST_LOCAL_FUNCTION(bind this_, int num,
bool recursion, default false) {
int result = 0;
if(num <= 0) result = 1;
else result = num * factorial(num - 1, true); // Recursive call.
if(!recursion) this_->results.push_back(result);
return result;
} BOOST_LOCAL_FUNCTION_NAME(recursive factorial) // Recursive.
std::for_each(nums.begin(), nums.end(), factorial);
}
};
//]
int main(void) {
std::vector<int> v(3);
v[0] = 1; v[1] = 3; v[2] = 4;
calculator calc;
calc.factorials(v);
BOOST_TEST(calc.results[0] == 1);
BOOST_TEST(calc.results[1] == 6);
BOOST_TEST(calc.results[2] == 24);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,48 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
#include <vector>
struct calculator;
BOOST_TYPEOF_REGISTER_TYPE(calculator) // Register before `bind this_` below.
struct calculator {
std::vector<int> results;
void factorials(const std::vector<int>& nums) {
int BOOST_LOCAL_FUNCTION( (bind this_) (int num)
(bool recursion)(default false) ) {
int result = 0;
if(num <= 0) result = 1;
else result = num * factorial(num - 1, true);
if(!recursion) this_->results.push_back(result);
return result;
} BOOST_LOCAL_FUNCTION_NAME(recursive factorial)
std::for_each(nums.begin(), nums.end(), factorial);
}
};
int main(void) {
std::vector<int> v(3);
v[0] = 1; v[1] = 3; v[2] = 4;
calculator calc;
calc.factorials(v);
BOOST_TEST(calc.results[0] == 1);
BOOST_TEST(calc.results[1] == 6);
BOOST_TEST(calc.results[2] == 24);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "factorial_seq.cpp"
+34
View File
@@ -0,0 +1,34 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
//[goto
int error(int x, int y) {
int BOOST_LOCAL_FUNCTION(int z) {
if(z > 0) goto success; // OK: Can jump within local function.
return -1;
success:
return 0;
} BOOST_LOCAL_FUNCTION_NAME(validate)
return validate(x + y);
}
//]
int main(void) {
error(1, 2);
return 0;
}
#endif // VARIADIC_MACROS
+36
View File
@@ -0,0 +1,36 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
//[goto_error
int error(int x, int y) {
int BOOST_LOCAL_FUNCTION(int z) {
if(z <= 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);
failure:
return -1;
}
//]
int main(void) {
error(1, 2);
return 0;
}
#endif
@@ -0,0 +1,27 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
int error(int x, int y) {
int BOOST_LOCAL_FUNCTION( (int z) ) {
if(z <= 0) goto failure;
else goto success;
success:
return 0;
} BOOST_LOCAL_FUNCTION_NAME(validate)
return validate(x + y);
failure:
return -1;
}
int main(void) {
error(1, 2);
return 0;
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "goto_error_seq.cpp"
+25
View File
@@ -0,0 +1,25 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
int error(int x, int y) {
int BOOST_LOCAL_FUNCTION( (int z) ) {
if(z > 0) goto success;
return -1;
success:
return 0;
} BOOST_LOCAL_FUNCTION_NAME(validate)
return validate(x + y);
}
int main(void) {
error(1, 2);
return 0;
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "goto_seq.cpp"
+52
View File
@@ -0,0 +1,52 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/utility/identity_type.hpp>
#include <boost/typeof/std/string.hpp> // Type-of registrations
#include <boost/typeof/std/map.hpp> // needed for `NAME` macro.
#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 {
static int const value;
};
template<typename V, typename K>
int const key_sizeof<V, K>::value = sizeof(K);
typedef int sign_t;
int main(void) {
//[macro_commas
void BOOST_LOCAL_FUNCTION(
BOOST_IDENTITY_TYPE((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;
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,44 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/utility/identity_type.hpp>
#include <boost/typeof/std/string.hpp> // Type-of registrations
#include <boost/typeof/std/map.hpp> // needed for `NAME` macro.
#include <boost/config.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 {
static int const value;
};
template<typename V, typename K>
int const key_sizeof<V, K>::value = sizeof(K);
typedef int sign_t;
int main(void) {
void BOOST_LOCAL_FUNCTION(
(BOOST_IDENTITY_TYPE((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;
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "macro_commas_seq.cpp"
+37
View File
@@ -0,0 +1,37 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
//[nesting
int x = 0;
void BOOST_LOCAL_FUNCTION(bind& x) {
void BOOST_LOCAL_FUNCTION(bind& x) { // Nested.
x++;
} BOOST_LOCAL_FUNCTION_NAME(g)
x--;
g(); // Nested local function call.
} BOOST_LOCAL_FUNCTION_NAME(f)
f();
//]
BOOST_TEST(x == 0);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
+28
View File
@@ -0,0 +1,28 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
int x = 0;
void BOOST_LOCAL_FUNCTION( (bind& x) ) {
void BOOST_LOCAL_FUNCTION( (bind& x) ) {
x++;
} BOOST_LOCAL_FUNCTION_NAME(g)
x--;
g();
} BOOST_LOCAL_FUNCTION_NAME(f)
f();
BOOST_TEST(x == 0);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "nesting_seq.cpp"
+21
View File
@@ -0,0 +1,21 @@
// Copyright (C) 2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/scope_exit
#ifndef NOVA_HPP_
#define NOVA_HPP_
#include <boost/config.hpp>
// WARNING: This file must be included first in each compilation unit.
// Force no variadic macros but avoiding macro redefinition warning/error.
#ifndef BOOST_NO_CXX11_VARIADIC_MACROS
# define BOOST_NO_CXX11_VARIADIC_MACROS
#endif
#endif // #include guard
+38
View File
@@ -0,0 +1,38 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
//[operator
struct point {
int x;
int y;
};
BOOST_TYPEOF_REGISTER_TYPE(point) // Register for `NAME` below.
int main(void) {
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_TEST(equal(a, b));
return boost::report_errors();
}
//]
#endif // VARIADIC_MACROS
@@ -0,0 +1,38 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
struct point {
int x;
int y;
};
BOOST_TYPEOF_REGISTER_TYPE(point) // Register for `NAME` below.
int main(void) {
//[operator_error
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_TEST(a == b);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,29 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
struct point {
int x;
int y;
};
BOOST_TYPEOF_REGISTER_TYPE(point) // Register for `NAME` below.
int main(void) {
bool BOOST_LOCAL_FUNCTION( (const point& p) (const point& q) ) {
return p.x == q.x && p.y == q.y;
} BOOST_LOCAL_FUNCTION_NAME(operator==)
point a; a.x = 1; a.y = 2;
point b = a;
BOOST_TEST(a == b);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "operator_error_seq.cpp"
+29
View File
@@ -0,0 +1,29 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
struct point {
int x;
int y;
};
BOOST_TYPEOF_REGISTER_TYPE(point) // Register for `NAME` below.
int main(void) {
bool BOOST_LOCAL_FUNCTION( (const point& p) (const point& q) ) {
return p.x == q.x && p.y == q.y;
} BOOST_LOCAL_FUNCTION_NAME(equal)
point a; a.x = 1; a.y = 2;
point b = a;
BOOST_TEST(equal(a, b));
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "operator_seq.cpp"
+52
View File
@@ -0,0 +1,52 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/functional/overloaded_function.hpp> // For overloading.
#include <boost/typeof/std/string.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <string>
//[overload_decl
int add_i(int x, int y) { return x + y; }
//]
int main(void) {
//[overload
std::string s = "abc";
std::string BOOST_LOCAL_FUNCTION(
const bind& s, const std::string& x) {
return s + x;
} BOOST_LOCAL_FUNCTION_NAME(add_s)
double d = 1.23;
double BOOST_LOCAL_FUNCTION(const bind d, double x, double y, default 0) {
return d + x + y;
} BOOST_LOCAL_FUNCTION_NAME(add_d)
boost::overloaded_function<
std::string (const std::string&)
, double (double)
, double (double, double) // Overload giving default param.
, int (int, int)
> add(add_s, add_d, add_d, add_i); // Overloaded function object.
BOOST_TEST(add("xyz") == "abcxyz"); // Call `add_s`.
BOOST_TEST((4.44 - add(3.21)) <= 0.001); // Call `add_d` (no default).
BOOST_TEST((44.44 - add(3.21, 40.0)) <= 0.001); // Call `add_d`.
BOOST_TEST(add(1, 2) == 3); // Call `add_i`.
//]
return boost::report_errors();
}
#endif // VARIADIC_MACROS
+42
View File
@@ -0,0 +1,42 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/functional/overloaded_function.hpp>
#include <boost/typeof/std/string.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <string>
int add_i(int x, int y) { return x + y; }
int main(void) {
std::string s = "abc";
std::string BOOST_LOCAL_FUNCTION(
(const bind& s) (const std::string& x) ) {
return s + x;
} BOOST_LOCAL_FUNCTION_NAME(add_s)
double d = 1.23;
double BOOST_LOCAL_FUNCTION( (const bind d) (double x)
(double y)(default 0) ) {
return d + x + y;
} BOOST_LOCAL_FUNCTION_NAME(add_d)
boost::overloaded_function<
std::string (const std::string&)
, double (double)
, double (double, double)
, int (int, int)
> add(add_s, add_d, add_d, add_i);
BOOST_TEST(add("xyz") == "abcxyz");
BOOST_TEST((4.44 - add(3.21)) <= 0.001); // Equal within precision.
BOOST_TEST((44.44 - add(3.21, 40.0)) <= 0.001); // Equal within precision.
BOOST_TEST(add(1, 2) == 3);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "overload_seq.cpp"
@@ -0,0 +1,49 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
//[return_assign
void call1(boost::function<int (int) > f) { BOOST_TEST(f(1) == 5); }
void call0(boost::function<int (void)> f) { BOOST_TEST(f() == 5); }
boost::function<int (int, int)> linear(const int& slope) {
int BOOST_LOCAL_FUNCTION(const bind& slope,
int x, default 1, int y, default 2) {
return x + slope * y;
} BOOST_LOCAL_FUNCTION_NAME(lin)
boost::function<int (int, int)> f = lin; // Assign to local variable.
BOOST_TEST(f(1, 2) == 5);
call1(lin); // Pass to other functions.
call0(lin);
return lin; // Return.
}
void call(void) {
boost::function<int (int, int)> f = linear(2);
BOOST_TEST(f(1, 2) == 5);
}
//]
int main(void) {
call();
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,40 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
void call1(boost::function<int (int) > f) { BOOST_TEST(f(1) == 5); }
void call0(boost::function<int (void)> f) { BOOST_TEST(f() == 5); }
boost::function<int (int, int)> linear(const int& slope) {
int BOOST_LOCAL_FUNCTION( (const bind& slope)
(int x)(default 1) (int y)(default 2) ) {
return x + slope * y;
} BOOST_LOCAL_FUNCTION_NAME(lin)
boost::function<int (int, int)> f = lin;
BOOST_TEST(f(1, 2) == 5);
call1(lin);
call0(lin);
return lin;
}
void call(void) {
boost::function<int (int, int)> f = linear(2);
BOOST_TEST(f(1, 2) == 5);
}
int main(void) {
call();
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "return_assign_seq.cpp"
@@ -0,0 +1,41 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function, 1)
boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
int BOOST_LOCAL_FUNCTION(bind& f, const bind dx, int x) {
return (f(x + dx) - f(x)) / dx;
} BOOST_LOCAL_FUNCTION_NAME(deriv)
return deriv;
}
int main(void) {
int BOOST_LOCAL_FUNCTION(int x) {
return x + 4;
} BOOST_LOCAL_FUNCTION_NAME(add2)
boost::function<int (int)> a2 = add2; // Reference valid where closure used.
boost::function<int (int)> d2 = derivative(a2, 2);
BOOST_TEST(d2(6) == 1);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,34 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function, 1)
boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
int BOOST_LOCAL_FUNCTION( (bind& f) (const bind dx) (int x) ) {
return (f(x + dx) - f(x)) / dx;
} BOOST_LOCAL_FUNCTION_NAME(deriv)
return deriv;
}
int main(void) {
int BOOST_LOCAL_FUNCTION( (int x) ) {
return x + 4;
} BOOST_LOCAL_FUNCTION_NAME(add2)
boost::function<int (int)> a2 = add2;
boost::function<int (int)> d2 = derivative(a2, 2);
BOOST_TEST(d2(6) == 1);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "return_derivative_seq.cpp"
+38
View File
@@ -0,0 +1,38 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
boost::function<int (void)> inc(int& value) {
int BOOST_LOCAL_FUNCTION(bind& value) {
return ++value;
} BOOST_LOCAL_FUNCTION_NAME(i)
return i;
}
int main(void) {
int value1 = 0; // Reference valid in scope where closure is used.
boost::function<int (void)> inc1 = inc(value1);
int value2 = 0;
boost::function<int (void)> inc2 = inc(value2);
BOOST_TEST(inc1() == 1);
BOOST_TEST(inc1() == 2);
BOOST_TEST(inc2() == 1);
BOOST_TEST(inc1() == 3);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,31 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
boost::function<int (void)> inc(int& value) {
int BOOST_LOCAL_FUNCTION( (bind& value) ) {
return ++value;
} BOOST_LOCAL_FUNCTION_NAME(i)
return i;
}
int main(void) {
int value1 = 0;
boost::function<int (void)> inc1 = inc(value1);
int value2 = 0;
boost::function<int (void)> inc2 = inc(value2);
BOOST_TEST(inc1() == 1);
BOOST_TEST(inc1() == 2);
BOOST_TEST(inc2() == 1);
BOOST_TEST(inc1() == 3);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "return_inc_seq.cpp"
@@ -0,0 +1,47 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/typeof/std/string.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <string>
boost::function<void (const std::string&)> set;
boost::function<const std::string& (void)> get;
void action(void) {
// State `message` hidden behind access functions from here.
BOOST_TEST(get() == "abc");
set("xyz");
BOOST_TEST(get() == "xyz");
}
int main(void) {
std::string message = "abc"; // Reference valid where closure used.
void BOOST_LOCAL_FUNCTION(bind& message, const std::string& text) {
message = text;
} BOOST_LOCAL_FUNCTION_NAME(s)
set = s;
const std::string& BOOST_LOCAL_FUNCTION(const bind& message) {
return message;
} BOOST_LOCAL_FUNCTION_NAME(g)
get = g;
action();
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,39 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/typeof/std/string.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <string>
boost::function<void (const std::string&)> set;
boost::function<const std::string& (void)> get;
void action(void) {
BOOST_TEST(get() == "abc");
set("xyz");
BOOST_TEST(get() == "xyz");
}
int main(void) {
std::string message = "abc";
void BOOST_LOCAL_FUNCTION( (bind& message) (const std::string& text) ) {
message = text;
} BOOST_LOCAL_FUNCTION_NAME(s)
set = s;
const std::string& BOOST_LOCAL_FUNCTION( (const bind& message) ) {
return message;
} BOOST_LOCAL_FUNCTION_NAME(g)
get = g;
action();
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "return_setget_seq.cpp"
+50
View File
@@ -0,0 +1,50 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
struct number;
BOOST_TYPEOF_REGISTER_TYPE(number) // Register before `bind this_` below.
struct number {
number(int value) : value_(value) {}
boost::function<int (void)> inc(void) {
int BOOST_LOCAL_FUNCTION(bind this_) {
return ++this_->value_;
} BOOST_LOCAL_FUNCTION_NAME(i)
return i;
}
private:
int value_;
};
int main(void) {
number n1 = 0; // Object valid in scope where closure is used.
boost::function<int (void)> inc1 = n1.inc();
number n2 = 0;
boost::function<int (void)> inc2 = n2.inc();
BOOST_TEST(inc1() == 1);
BOOST_TEST(inc1() == 2);
BOOST_TEST(inc2() == 1);
BOOST_TEST(inc1() == 3);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,43 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
#include <boost/detail/lightweight_test.hpp>
struct number;
BOOST_TYPEOF_REGISTER_TYPE(number) // Register before `bind this_` below.
struct number {
number(int value) : value_(value) {}
boost::function<int (void)> inc(void) {
int BOOST_LOCAL_FUNCTION( (bind this_) ) {
return ++this_->value_;
} BOOST_LOCAL_FUNCTION_NAME(i)
return i;
}
private:
int value_;
};
int main(void) {
number n1 = 0; // Object valid in scope where closure is used.
boost::function<int (void)> inc1 = n1.inc();
number n2 = 0;
boost::function<int (void)> inc2 = n2.inc();
BOOST_TEST(inc1() == 1);
BOOST_TEST(inc1() == 2);
BOOST_TEST(inc2() == 1);
BOOST_TEST(inc1() == 3);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "return_this_seq.cpp"
+57
View File
@@ -0,0 +1,57 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
//[same_line
#define LOCAL_INC_DEC(offset) \
int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
const bind offset, const int x) { \
return x + offset; \
} BOOST_LOCAL_FUNCTION_NAME(inc) \
\
int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(dec, __LINE__), \
const bind offset, const int x) { \
return x - offset; \
} BOOST_LOCAL_FUNCTION_NAME(dec)
#define LOCAL_INC_DEC_TPL(offset) \
T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
const bind offset, const T x) { \
return x + offset; \
} BOOST_LOCAL_FUNCTION_NAME_TPL(inc) \
\
T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
const bind offset, const T x) { \
return x - offset; \
} BOOST_LOCAL_FUNCTION_NAME_TPL(dec)
template<typename T>
void f(T& delta) {
LOCAL_INC_DEC_TPL(delta) // Multiple local functions on same line.
BOOST_TEST(dec(inc(123)) == 123);
}
int main(void) {
int delta = 10;
LOCAL_INC_DEC(delta) // Multiple local functions on same line.
BOOST_TEST(dec(inc(123)) == 123);
f(delta);
return boost::report_errors();
}
//]
#endif // VARIADIC_MACROS
@@ -0,0 +1,48 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
#define LOCAL_INC_DEC(offset) \
int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
(const bind offset) (const int x) ) { \
return x + offset; \
} BOOST_LOCAL_FUNCTION_NAME(inc) \
\
int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(dec, __LINE__), \
(const bind offset) (const int x) ) { \
return x - offset; \
} BOOST_LOCAL_FUNCTION_NAME(dec)
#define LOCAL_INC_DEC_TPL(offset) \
T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
(const bind offset) (const T x) ) { \
return x + offset; \
} BOOST_LOCAL_FUNCTION_NAME_TPL(inc) \
\
T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
(const bind offset) (const T x) ) { \
return x - offset; \
} BOOST_LOCAL_FUNCTION_NAME_TPL(dec)
template<typename T>
void f(T& delta) {
LOCAL_INC_DEC_TPL(delta) // Multiple local functions on same line.
BOOST_TEST(dec(inc(123)) == 123);
}
int main(void) {
int delta = 10;
LOCAL_INC_DEC(delta) // Declare local functions on same line using `_ID`.
BOOST_TEST(dec(inc(123)) == 123);
f(delta);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "same_line_seq.cpp"
+21
View File
@@ -0,0 +1,21 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
int main(void) {
//[ten_void
int BOOST_LOCAL_FUNCTION(void) { // No parameter.
return 10;
} BOOST_LOCAL_FUNCTION_NAME(ten)
BOOST_TEST(ten() == 10);
//]
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "ten_void.cpp"
+47
View File
@@ -0,0 +1,47 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
#include <vector>
int main(void) {
//[transform
int offset = 5;
std::vector<int> v;
std::vector<int> w;
for(int i = 1; i <= 2; ++i) v.push_back(i * 10);
BOOST_TEST(v[0] == 10); BOOST_TEST(v[1] == 20);
w.resize(v.size());
int BOOST_LOCAL_FUNCTION(const bind& offset, int i) {
return ++i + offset;
} BOOST_LOCAL_FUNCTION_NAME(inc)
std::transform(v.begin(), v.end(), w.begin(), inc);
BOOST_TEST(w[0] == 16); BOOST_TEST(w[1] == 26);
int BOOST_LOCAL_FUNCTION(bind& inc, int i, int j) {
return inc(i + j); // Call the other bound local function.
} BOOST_LOCAL_FUNCTION_NAME(inc_sum)
offset = 0;
std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum);
BOOST_TEST(v[0] == 27); BOOST_TEST(v[1] == 47);
//]
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,39 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/local_function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
#include <vector>
int main(void) {
int offset = 5;
std::vector<int> v;
std::vector<int> w;
for(int i = 1; i <= 2; ++i) v.push_back(i * 10);
BOOST_TEST(v[0] == 10); BOOST_TEST(v[1] == 20);
w.resize(v.size());
int BOOST_LOCAL_FUNCTION( (const bind& offset) (int i) ) {
return ++i + offset;
} BOOST_LOCAL_FUNCTION_NAME(inc)
std::transform(v.begin(), v.end(), w.begin(), inc);
BOOST_TEST(w[0] == 16); BOOST_TEST(w[1] == 26);
int BOOST_LOCAL_FUNCTION( (bind& inc) (int i) (int j) ) {
return inc(i + j);
} BOOST_LOCAL_FUNCTION_NAME(inc_sum)
offset = 0;
std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum);
BOOST_TEST(v[0] == 27); BOOST_TEST(v[1] == 47);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "transform_seq.cpp"
+41
View File
@@ -0,0 +1,41 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include "addable.hpp"
#include <boost/local_function.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/concept_check.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
int main(void) {
//[typeof
int sum = 0, factor = 10;
void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
// Type-of for concept checking.
BOOST_CONCEPT_ASSERT((Addable<boost::remove_reference<
BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
// Type-of for declarations.
boost::remove_reference<BOOST_LOCAL_FUNCTION_TYPEOF(
factor)>::type mult = factor * num;
sum += mult;
} BOOST_LOCAL_FUNCTION_NAME(add)
add(6);
//]
BOOST_TEST(sum == 60);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
+30
View File
@@ -0,0 +1,30 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "addable.hpp"
#include <boost/local_function.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/concept_check.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
int main(void) {
int sum = 0, factor = 10;
void BOOST_LOCAL_FUNCTION( (const bind factor) (bind& sum) (int num) ) {
BOOST_CONCEPT_ASSERT((Addable<boost::remove_reference<
BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
boost::remove_reference<BOOST_LOCAL_FUNCTION_TYPEOF(
factor)>::type mult = factor * num;
sum += mult;
} BOOST_LOCAL_FUNCTION_NAME(add)
add(6);
BOOST_TEST(sum == 60);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "typeof_seq.cpp"
@@ -0,0 +1,43 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX11_VARIADIC_MACROS
# error "variadic macros required"
#else
#include "addable.hpp"
#include <boost/local_function.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/concept_check.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
//[typeof_template
template<typename T>
T calculate(const T& factor) {
T sum = 0;
void BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) {
// Local function `TYPEOF` does not need `typename`.
BOOST_CONCEPT_ASSERT((Addable<typename boost::remove_reference<
BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME_TPL(add)
add(6);
return sum;
}
//]
int main(void) {
BOOST_TEST(calculate(10) == 60);
return boost::report_errors();
}
#endif // VARIADIC_MACROS
@@ -0,0 +1,33 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "addable.hpp"
#include <boost/local_function.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/concept_check.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>
template<typename T>
T calculate(const T& factor) {
T sum = 0;
void BOOST_LOCAL_FUNCTION_TPL( (const bind factor) (bind& sum) (T num) ) {
BOOST_CONCEPT_ASSERT((Addable<typename boost::remove_reference<
BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
sum += factor * num;
} BOOST_LOCAL_FUNCTION_NAME_TPL(add)
add(6);
return sum;
}
int main(void) {
BOOST_TEST(calculate(10) == 60);
return boost::report_errors();
}
@@ -0,0 +1,10 @@
// Copyright (C) 2009-2012 Lorenzo Caminiti
// Distributed under 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)
// Home at http://www.boost.org/libs/local_function
#include "nova.hpp"
#include "typeof_template_seq.cpp"