2018-04-27 16:32:58 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-04-27 16:32:58 +10:00
|
|
|
*
|
2019-02-05 12:13:53 +11:00
|
|
|
* Copyright 2018-2019 Danny Robson <danny@nerdcruft.net>
|
2018-04-27 16:32:58 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <utility>
|
2019-02-05 12:13:53 +11:00
|
|
|
#include <tuple>
|
2018-04-27 16:32:58 +10:00
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::functor {
|
2019-02-05 11:58:31 +11:00
|
|
|
/// A trivial functor that wraps std::begin without any static typing.
|
|
|
|
struct begin {
|
|
|
|
template <typename valuet>
|
|
|
|
decltype (auto)
|
|
|
|
operator() (valuet &&value) noexcept (noexcept (std::begin (value)))
|
|
|
|
{
|
|
|
|
return std::begin (
|
|
|
|
std::forward<valuet> (value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// A trivial functor that wraps std::end without any static typing.
|
|
|
|
struct end {
|
|
|
|
template <typename valuet>
|
|
|
|
decltype (auto)
|
|
|
|
operator() (valuet &&value) noexcept (noexcept (std::end (value)))
|
|
|
|
{
|
|
|
|
return std::end (
|
|
|
|
std::forward<valuet> (value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-04-27 16:32:58 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// returns the value provided at construction time regardless of the
|
|
|
|
/// arguments supplied in the call operator.
|
|
|
|
template <typename ValueT>
|
|
|
|
class constant {
|
|
|
|
public:
|
|
|
|
constant (const ValueT &_value):
|
|
|
|
m_value (_value)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
ValueT&
|
|
|
|
operator() (Args&&...) noexcept
|
|
|
|
{ return m_value; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
const ValueT&
|
|
|
|
operator() (Args&&...) const noexcept
|
|
|
|
{ return m_value; }
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValueT m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename ValueT>
|
|
|
|
constant (ValueT) -> constant<std::decay_t<ValueT>>;
|
2019-02-05 12:13:53 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Returns a ValueT constructed from the supplied arguments.
|
|
|
|
template <typename ValueT>
|
|
|
|
struct construct {
|
|
|
|
template <typename ...Args>
|
|
|
|
ValueT operator() (Args &&...args)
|
|
|
|
{
|
|
|
|
return ValueT {
|
|
|
|
std::forward<Args> (args)...
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns a ValueT constructed from a tuple of arguments.
|
|
|
|
template <typename ValueT>
|
|
|
|
struct tuple_construct {
|
|
|
|
template <typename Args>
|
|
|
|
ValueT operator() (Args &&args)
|
|
|
|
{
|
|
|
|
return std::apply (
|
|
|
|
construct<ValueT> {},
|
|
|
|
std::forward<Args> (args)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|