/* * 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/. * * Copyright 2018-2019 Danny Robson */ #pragma once #include #include namespace cruft::functor { /// Returns the supplied argument unchanged /// /// CXX#20: Revisit when C++20 support is available. This is just a /// trivial implementation of std::identity. struct identity { template ValueT&& operator() (ValueT &&val) { return std::forward (val); } }; /// A trivial functor that wraps std::begin without any static typing. struct begin { template decltype (auto) operator() (ValueT &&value) noexcept (noexcept (std::begin (value))) { return std::begin ( std::forward (value) ); } }; /// A trivial functor that wraps std::end without any static typing. struct end { template decltype (auto) operator() (ValueT &&value) noexcept (noexcept (std::end (value))) { return std::end ( std::forward (value) ); } }; /////////////////////////////////////////////////////////////////////////// /// returns the value provided at construction time regardless of the /// arguments supplied in the call operator. template class constant { public: constant (const ValueT &_value): m_value (_value) { ; } template ValueT& operator() (Args&&...) noexcept { return m_value; } template const ValueT& operator() (Args&&...) const noexcept { return m_value; } private: ValueT m_value; }; //------------------------------------------------------------------------- template constant (ValueT) -> constant>; /////////////////////////////////////////////////////////////////////////// /// Returns a ValueT constructed from the supplied arguments. template struct construct { template ValueT operator() (Args &&...args) { return ValueT { std::forward (args)... }; } }; ///------------------------------------------------------------------------ /// Returns a ValueT constructed from a tuple of arguments. template struct tuple_construct { template ValueT operator() (Args &&args) { return std::apply ( construct {}, std::forward (args) ); } }; }