iceberg-cpp
Loading...
Searching...
No Matches
functional.h
Go to the documentation of this file.
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
27
28#include <concepts>
29#include <functional>
30#include <memory>
31#include <type_traits>
32#include <utility>
33
35
36namespace iceberg {
37
38namespace internal {
39
40template <typename R, typename Fn, typename... Args>
41concept RvalueInvocable = std::constructible_from<std::remove_cvref_t<Fn>, Fn> &&
42 std::move_constructible<std::remove_cvref_t<Fn>> &&
43 std::is_invocable_r_v<R, std::remove_cvref_t<Fn>&&, Args...>;
44
45} // namespace internal
46
47template <typename Signature>
48class FnOnce;
49
50template <typename R, typename... Args>
51class ICEBERG_TEMPLATE_CLASS_EXPORT FnOnce<R(Args...)> {
52 public:
53 template <typename Fn>
54 requires(!std::same_as<std::remove_cvref_t<Fn>, FnOnce> &&
55 internal::RvalueInvocable<R, Fn, Args...>)
56 explicit FnOnce(Fn&& fn) : impl_(std::make_unique<ImplFor<Fn>>(std::forward<Fn>(fn))) {}
57
58 FnOnce(FnOnce&&) noexcept = default;
59 FnOnce& operator=(FnOnce&&) noexcept = default;
60 FnOnce(const FnOnce&) = delete;
61 FnOnce& operator=(const FnOnce&) = delete;
62
63 R operator()(Args... args) && {
64 return std::move(*impl_).Invoke(std::forward<Args>(args)...);
65 }
66
67 private:
68 struct Impl {
69 virtual ~Impl() = default;
70 virtual R Invoke(Args&&... args) && = 0;
71 };
72
73 template <typename Fn>
74 struct ImplFor final : Impl {
75 explicit ImplFor(Fn&& fn) : fn_(std::forward<Fn>(fn)) {}
76 R Invoke(Args&&... args) && override {
77 return std::invoke(std::move(fn_), std::forward<Args>(args)...);
78 }
79 std::remove_cvref_t<Fn> fn_;
80 };
81
82 std::unique_ptr<Impl> impl_;
83};
84
85} // namespace iceberg
Definition functional.h:48
Definition functional.h:41
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33