Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
p-groarke committed Oct 14, 2024
1 parent 7d08057 commit f7eff66
Show file tree
Hide file tree
Showing 21 changed files with 894 additions and 398 deletions.
1 change: 0 additions & 1 deletion benchmarks/flat_recurse/flat_recurse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <array>
#include <chrono>
#include <fea/benchmark/benchmark.hpp>
#include <fea/flat_recurse/flat_recurse.hpp>
#include <fea/utils/platform.hpp>
#include <gtest/gtest.h>
#include <random>
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/flat_recurse/global.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <fea/flat_recurse/flat_recurse.hpp>
#include <fea/functional/flat_recurse.hpp>
#include <gtest/gtest.h>
#include <iterator>
#include <memory>
Expand Down
1 change: 0 additions & 1 deletion benchmarks/flat_recurse/iterators.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "global.hpp"

#include <fea/flat_recurse/flat_recurse.hpp>
#include <gtest/gtest.h>
#include <iterator>

Expand Down
1 change: 0 additions & 1 deletion benchmarks/flat_recurse/small_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "global.hpp"
#include "iterators.hpp"

#include <fea/flat_recurse/flat_recurse.hpp>
#include <gtest/gtest.h>

namespace fea {
Expand Down
120 changes: 22 additions & 98 deletions include_cpp17/fea/functional/callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,111 +34,35 @@
#include "fea/meta/function_traits.hpp"
#include "fea/utils/platform.hpp"

namespace fea {
// Pass template lambdas while requiring the necessary signature for
// documentation.
template <class, class = void>
struct callback;

template <class Func, class Ret, class... Args>
struct callback<Func, Ret(Args...)> {
using func_t = Func;

constexpr callback(const Func& func)
: _func(func) {
}
constexpr callback(Func&& func)
: _func(std::move(func)) {
}

constexpr Ret operator()(Args... args) const {
return _func(std::forward<Args>(args)...);
}
/*
fea::callback enforces callback signatures on templated lambda callbacks.
private:
Func _func;
};
It uses the most efficient callback mechanism, can be used / exposed in your API
and is strict (more strict than std::function).
namespace detail {
template <class, class>
struct callback_base;

template <class Func, class Ret, class... Args>
struct callback_base<Func, Ret(Args...)> : callback<Func, Ret(Args...)> {
using base = callback<Func, Ret(Args...)>;
using base::base;
};

template <class Func, class Ret, class... Args>
struct callback_base<Func, Ret (*)(Args...)> : callback<Func, Ret(Args...)> {
using base = callback<Func, Ret(Args...)>;
using base::base;
};

template <class Func, class Ret, class T, class... Args>
struct callback_base<Func, Ret (T::*)(Args...)> : callback<Func, Ret(Args...)> {
using base = callback<Func, Ret(Args...)>;
using base::base;
};

template <class Func, class Ret, class T, class... Args>
struct callback_base<Func, Ret (T::*)(Args...) const>
: callback<Func, Ret(Args...)> {
using base = callback<Func, Ret(Args...)>;
using base::base;
};
First template argument is the function type (should come from template).
Second is the signature type, in the style of std::function.
For ex, fea::callback<FuncT, void(int)>
*/

namespace fea {
// Stores a callback function.
// First template argument is the function type (should come from template).
// Second is the signature type, as you would a std::function.
// For example, fea::callback<FuncT, void(int, float)>
template <class, class = void>
struct callback_selector;

struct callback;

// Create a callback object.
// Provide a lambda that matches the callback signature.
template <class Func>
struct callback_selector<Func,
std::enable_if_t<fea::is_detected_v<fea::has_operator_paren, Func>>>
: callback_base<Func, decltype(&Func::operator())> {
using base = callback_base<Func, decltype(&Func::operator())>;
using base::base;
};
callback<Func> make_callback(const Func& func);

// Create a callback object.
// Provide a lambda that matches the callback signature.
template <class Func>
struct callback_selector<Func,
std::enable_if_t<!fea::is_detected_v<fea::has_operator_paren, Func>>>
: callback_base<Func, Func> {
using base = callback_base<Func, Func>;
using base::base;
};

} // namespace detail

template <class Func, class>
struct callback : detail::callback_selector<Func> {
using func_t = Func;
using base = detail::callback_selector<Func>;

constexpr callback(const Func& func)
: base(func) {
}
constexpr callback(Func&& func)
: base(std::move(func)) {
}
};


template <class Func>
callback<Func> make_callback(const Func& func) {
return callback<Func>(func);
}
template <class Func>
callback<Func> make_callback(Func&& func) {
return callback<Func>(func);
}

//#if FEA_CPP17
// template <class Func>
// callback(const Func&) -> callback<Func>;
// template <class Func>
// callback(Func &&) -> callback<Func>;
//#endif

callback<Func> make_callback(Func&& func);

} // namespace fea

#include "imp/callback.imp.hpp"
Loading

0 comments on commit f7eff66

Please sign in to comment.