Skip to content

Commit

Permalink
adding license, make_function example
Browse files Browse the repository at this point in the history
  • Loading branch information
badair committed May 18, 2016
1 parent 5ff3a54 commit ac90473
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright 2015-2016 Barrett Adair

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
55 changes: 55 additions & 0 deletions test/make_function.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*<-
Copyright Barrett Adair 2016
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http ://boost.org/LICENSE_1_0.txt)
->*/

#ifndef EXAMPLE_MAKE_FUNCTION_HPP
#define EXAMPLE_MAKE_FUNCTION_HPP

#include <functional>
#include <type_traits>
#include <utility>

#include "bind_parser.hpp"

namespace example {

// make_function turns a non-overloaded callable into a type-erased std::function object
template<typename T>
inline decltype(auto) make_function(T&& t) {

using no_ref = typename std::remove_reference<T>::type;
using f = bind_parser::function_type<no_ref>;
using result_type = std::function<f>;
return result_type{ std::forward<T>(t) };
}

// this make_function overload turns a bind expression into a type-erased std::function object
template<typename T, typename First, typename... Others>
inline decltype(auto) make_function(T&& t, First&& first, Others&&... others) {

// bind_parser::bind is a compile-time parser of placeholder expressions,
// for the purpose of retaining more type information than std::bind normally
// allows - specifically, bind_parser::bind is used to determine the de-facto
// signature of the std::bind return type, with special considerations for
// conversions between reused placeholders and nested placeholder expressions.
// For the sake of convenience, bind_parser::bind is also a thin forwarding
// wrapper around std::bind.

using bind_expr = decltype(bind_parser::bind(
std::forward<T>(t),
std::forward<First>(first),
std::forward<Others>(others)...
));

using f = bind_parser::function_type<bind_expr>;
using result_type = std::function<f>;

return result_type{ std::bind(
std::forward<T>(t),
std::forward<First>(first),
std::forward<Others>(others)...
)};
}
}
46 changes: 46 additions & 0 deletions test/make_function_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*<-
Copyright Barrett Adair 2016
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http ://boost.org/LICENSE_1_0.txt)
->*/

#undef NDEBUG

#include <cassert>
#include <functional>

#include "make_function.hpp"

using namespace example;
using namespace std::placeholders;

int add(int i, int j) {
return i + j;
}

struct adder {

int eval(int i, int j) const { return i + j; }
};

int main() {

// function pointer
auto f = make_function(&add);
assert(f(99, 1) == 100);

// function reference
f = make_function(add);
assert(f(99, 1) == 100);

// member function pointer (bound to object)
f = make_function(&adder::eval, adder{}, _1, _2);
assert(f(99, 1) == 100);

// lambda
f = make_function([](int i, int j) {
return i + j;
});

assert(f(99, 1) == 100);
}

0 comments on commit ac90473

Please sign in to comment.