iceberg-cpp
Loading...
Searching...
No Matches
functional.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20// Borrowed the file from Apache Arrow:
21// https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/functional.h
22
23#pragma once
24
25#include <concepts>
26#include <functional>
27#include <memory>
28#include <type_traits>
29#include <utility>
30
31#include "iceberg/iceberg_export.h"
32
33namespace iceberg {
34
35namespace internal {
36
37template <typename R, typename Fn, typename... Args>
38concept RvalueInvocable = std::constructible_from<std::remove_cvref_t<Fn>, Fn> &&
39 std::move_constructible<std::remove_cvref_t<Fn>> &&
40 std::is_invocable_r_v<R, std::remove_cvref_t<Fn>&&, Args...>;
41
42} // namespace internal
43
44template <typename Signature>
45class FnOnce;
46
47template <typename R, typename... Args>
48class ICEBERG_TEMPLATE_CLASS_EXPORT FnOnce<R(Args...)> {
49 public:
50 template <typename Fn>
51 requires(!std::same_as<std::remove_cvref_t<Fn>, FnOnce> &&
52 internal::RvalueInvocable<R, Fn, Args...>)
53 explicit FnOnce(Fn&& fn) : impl_(std::make_unique<ImplFor<Fn>>(std::forward<Fn>(fn))) {}
54
55 FnOnce(FnOnce&&) noexcept = default;
56 FnOnce& operator=(FnOnce&&) noexcept = default;
57 FnOnce(const FnOnce&) = delete;
58 FnOnce& operator=(const FnOnce&) = delete;
59
60 R operator()(Args... args) && {
61 return std::move(*impl_).Invoke(std::forward<Args>(args)...);
62 }
63
64 private:
65 struct Impl {
66 virtual ~Impl() = default;
67 virtual R Invoke(Args&&... args) && = 0;
68 };
69
70 template <typename Fn>
71 struct ImplFor final : Impl {
72 explicit ImplFor(Fn&& fn) : fn_(std::forward<Fn>(fn)) {}
73 R Invoke(Args&&... args) && override {
74 return std::invoke(std::move(fn_), std::forward<Args>(args)...);
75 }
76 std::remove_cvref_t<Fn> fn_;
77 };
78
79 std::unique_ptr<Impl> impl_;
80};
81
82} // namespace iceberg
Definition functional.h:45
Definition functional.h:38