Skip to content

Commit 4743b5d

Browse files
committed
feat utils: add utils::move_only_function
Co-authored-by: Anton3 [[email protected]](mailto:[email protected]) commit_hash:33d4ab07f0e950e0ce445a42afe5c9b46d7ddaba
1 parent e91f40e commit 4743b5d

File tree

11 files changed

+652
-1
lines changed

11 files changed

+652
-1
lines changed

.mapping.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,6 +4357,12 @@
43574357
"third_party/moodycamel/include/moodycamel/blockingconcurrentqueue.h":"taxi/uservices/userver/third_party/moodycamel/include/moodycamel/blockingconcurrentqueue.h",
43584358
"third_party/moodycamel/include/moodycamel/concurrentqueue.h":"taxi/uservices/userver/third_party/moodycamel/include/moodycamel/concurrentqueue.h",
43594359
"third_party/moodycamel/include/moodycamel/lightweightsemaphore.h":"taxi/uservices/userver/third_party/moodycamel/include/moodycamel/lightweightsemaphore.h",
4360+
"third_party/move_only_function_backport/README.md":"taxi/uservices/userver/third_party/move_only_function_backport/README.md",
4361+
"third_party/move_only_function_backport/include/function_backports/move_only_function.h":"taxi/uservices/userver/third_party/move_only_function_backport/include/function_backports/move_only_function.h",
4362+
"third_party/move_only_function_backport/include/function_backports/move_only_function_common.h":"taxi/uservices/userver/third_party/move_only_function_backport/include/function_backports/move_only_function_common.h",
4363+
"third_party/move_only_function_backport/include/function_backports/move_only_function_impl.h":"taxi/uservices/userver/third_party/move_only_function_backport/include/function_backports/move_only_function_impl.h",
4364+
"third_party/move_only_function_backport/include/function_backports/pointer_int_pair.h":"taxi/uservices/userver/third_party/move_only_function_backport/include/function_backports/pointer_int_pair.h",
4365+
"third_party/move_only_function_backport/include/function_backports/small_buffer.h":"taxi/uservices/userver/third_party/move_only_function_backport/include/function_backports/small_buffer.h",
43604366
"third_party/pfr/include/boost/pfr.hpp":"taxi/uservices/userver/third_party/pfr/include/boost/pfr.hpp",
43614367
"third_party/pfr/include/boost/pfr/config.hpp":"taxi/uservices/userver/third_party/pfr/include/boost/pfr/config.hpp",
43624368
"third_party/pfr/include/boost/pfr/core.hpp":"taxi/uservices/userver/third_party/pfr/include/boost/pfr/core.hpp",
@@ -4708,6 +4714,7 @@
47084714
"universal/include/userver/utils/meta.hpp":"taxi/uservices/userver/universal/include/userver/utils/meta.hpp",
47094715
"universal/include/userver/utils/meta_light.hpp":"taxi/uservices/userver/universal/include/userver/utils/meta_light.hpp",
47104716
"universal/include/userver/utils/mock_now.hpp":"taxi/uservices/userver/universal/include/userver/utils/mock_now.hpp",
4717+
"universal/include/userver/utils/move_only_function.hpp":"taxi/uservices/userver/universal/include/userver/utils/move_only_function.hpp",
47114718
"universal/include/userver/utils/not_null.hpp":"taxi/uservices/userver/universal/include/userver/utils/not_null.hpp",
47124719
"universal/include/userver/utils/numeric_cast.hpp":"taxi/uservices/userver/universal/include/userver/utils/numeric_cast.hpp",
47134720
"universal/include/userver/utils/optional_ref.hpp":"taxi/uservices/userver/universal/include/userver/utils/optional_ref.hpp",
@@ -5008,6 +5015,7 @@
50085015
"universal/src/utils/meta_test.cpp":"taxi/uservices/userver/universal/src/utils/meta_test.cpp",
50095016
"universal/src/utils/mock_now.cpp":"taxi/uservices/userver/universal/src/utils/mock_now.cpp",
50105017
"universal/src/utils/mock_now_test.cpp":"taxi/uservices/userver/universal/src/utils/mock_now_test.cpp",
5018+
"universal/src/utils/move_only_function_test.cpp":"taxi/uservices/userver/universal/src/utils/move_only_function_test.cpp",
50115019
"universal/src/utils/not_null_test.cpp":"taxi/uservices/userver/universal/src/utils/not_null_test.cpp",
50125020
"universal/src/utils/numeric_cast_test.cpp":"taxi/uservices/userver/universal/src/utils/numeric_cast_test.cpp",
50135021
"universal/src/utils/optional_ref_test.cpp":"taxi/uservices/userver/universal/src/utils/optional_ref_test.cpp",

third_party/function_backports/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Imported from https://github.com/zhihaoy/nontype_functional/tree/p0792r13
33
The library was patched to support C++17 (C++20 was originally required).
44
Summary of the changes:
55

6-
- used `namespace function_backports` instead of `namespace cpp23`
6+
- used `namespace function_backports` instead of `namespace std23`
77
- usages of `requires` were replaced with `std::enable_if`
88
- `auto` function parameters were replaced with named template parameters
99
- poly-filled `std::type_identity`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Imported from libc++ PR ["Implement std::move_only_function"](https://github.com/llvm/llvm-project/pull/94670).
2+
Specifically, from the [commit](https://github.com/llvm/llvm-project/pull/94670/commits/7a203002b19f5a2827607e73a998dcd1ace9d135) on June 6, 2024.
3+
4+
The library was patched to support C++17 (C++20 was originally required).
5+
6+
Summary of the changes:
7+
8+
- used `namespace function_backports`
9+
- removed `_LIBCPP_STD_VER` guards
10+
- used `#pragma once`
11+
- replaced `_LIBCPP_` macro prefix with `USERVER_`
12+
- removed `_LIBCPP_HIDE_FROM_ABI`
13+
- replaced internal libc++ assert macros with `assert()`
14+
- replaced usages of `requires` with `std::enable_if`
15+
- added `std::` where required
16+
- replaced internal libc++ includes with standard includes
17+
- removed a usage of sized `delete[]`
18+
- replaced `auto` function parameters with named template parameters
19+
- inlined `std::decay_t<_VT>` in `__is_callable_from` to work around a compiler bug
20+
- added an extra `typename` where required by C++17
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#pragma GCC system_header
12+
13+
// move_only_function design:
14+
//
15+
// move_only_function has a small buffer with a size of `3 * sizeof(void*)` bytes. This buffer can only be used when the
16+
// object that should be stored is trivially relocatable (currently only when it is trivially move constructible and
17+
// trivially destructible). There is also a bool in the lower bits of the vptr stored which is set when the contained
18+
// object is not trivially destructible.
19+
//
20+
// trivially relocatable: It would also be possible to store nothrow_move_constructible types, but that would mean
21+
// that move_only_function itself would not be trivially relocatable anymore. The decision to keep move_only_function
22+
// trivially relocatable was made because we expect move_only_function to be mostly used to store a functor. To only
23+
// forward functors there is std::function_ref (not voted in yet, expected in C++26).
24+
//
25+
// buffer size: We did a survey of six implementations from various vendors. Three of them had a buffer size of 24 bytes
26+
// on 64 bit systems. This also allows storing a std::string or std::vector inside the small buffer (once the compiler
27+
// has full support of trivially_relocatable annotations).
28+
//
29+
// trivially-destructible bit: This allows us to keep the overall binary size smaller because we don't have to store
30+
// a pointer to a noop function inside the vtable. It also avoids loading the vtable during destruction, potentially
31+
// resulting in fewer cache misses. The downside is that calling the function now also requires setting the lower bits
32+
// of the pointer to zero, but this is a very fast operation on modern CPUs.
33+
34+
// NOLINTBEGIN(readability-duplicate-include)
35+
# define USERVER_IN_MOVE_ONLY_FUNCTION_H
36+
37+
# include "move_only_function_impl.h"
38+
39+
# define USERVER_MOVE_ONLY_FUNCTION_REF &
40+
# include "move_only_function_impl.h"
41+
42+
# define USERVER_MOVE_ONLY_FUNCTION_REF &&
43+
# include "move_only_function_impl.h"
44+
45+
# define USERVER_MOVE_ONLY_FUNCTION_CV const
46+
# include "move_only_function_impl.h"
47+
48+
# define USERVER_MOVE_ONLY_FUNCTION_CV const
49+
# define USERVER_MOVE_ONLY_FUNCTION_REF &
50+
# include "move_only_function_impl.h"
51+
52+
# define USERVER_MOVE_ONLY_FUNCTION_CV const
53+
# define USERVER_MOVE_ONLY_FUNCTION_REF &&
54+
# include "move_only_function_impl.h"
55+
56+
# define USERVER_MOVE_ONLY_FUNCTION_NOEXCEPT true
57+
# include "move_only_function_impl.h"
58+
59+
# define USERVER_MOVE_ONLY_FUNCTION_NOEXCEPT true
60+
# define USERVER_MOVE_ONLY_FUNCTION_REF &
61+
# include "move_only_function_impl.h"
62+
63+
# define USERVER_MOVE_ONLY_FUNCTION_NOEXCEPT true
64+
# define USERVER_MOVE_ONLY_FUNCTION_REF &&
65+
# include "move_only_function_impl.h"
66+
67+
# define USERVER_MOVE_ONLY_FUNCTION_NOEXCEPT true
68+
# define USERVER_MOVE_ONLY_FUNCTION_CV const
69+
# include "move_only_function_impl.h"
70+
71+
# define USERVER_MOVE_ONLY_FUNCTION_NOEXCEPT true
72+
# define USERVER_MOVE_ONLY_FUNCTION_CV const
73+
# define USERVER_MOVE_ONLY_FUNCTION_REF &
74+
# include "move_only_function_impl.h"
75+
76+
# define USERVER_MOVE_ONLY_FUNCTION_NOEXCEPT true
77+
# define USERVER_MOVE_ONLY_FUNCTION_CV const
78+
# define USERVER_MOVE_ONLY_FUNCTION_REF &&
79+
# include "move_only_function_impl.h"
80+
81+
# undef USERVER_IN_MOVE_ONLY_FUNCTION_H
82+
// NOLINTEND(readability-duplicate-include)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <type_traits>
12+
#include <utility>
13+
14+
#pragma GCC system_header
15+
16+
namespace function_backports {
17+
18+
template <class...>
19+
class move_only_function;
20+
21+
template <class>
22+
struct __is_move_only_function : std::false_type {};
23+
24+
template <class... _Args>
25+
struct __is_move_only_function<move_only_function<_Args...>> : std::true_type {};
26+
27+
template <class _BufferT, class _ReturnT, class... _ArgTypes>
28+
struct _MoveOnlyFunctionTrivialVTable {
29+
using _CallFunc = _ReturnT(_BufferT&, _ArgTypes...);
30+
31+
_CallFunc* __call_;
32+
};
33+
34+
template <class _BufferT, class _ReturnT, class... _ArgTypes>
35+
struct _MoveOnlyFunctionNonTrivialVTable : _MoveOnlyFunctionTrivialVTable<_BufferT, _ReturnT, _ArgTypes...> {
36+
using _DestroyFunc = void(_BufferT&) noexcept;
37+
38+
_DestroyFunc* __destroy_;
39+
};
40+
41+
template <class _Tp> struct __is_inplace_type_imp : std::false_type {};
42+
template <class _Tp> struct __is_inplace_type_imp<std::in_place_type_t<_Tp>> : std::true_type {};
43+
44+
template <class _Tp>
45+
using __is_inplace_type = __is_inplace_type_imp<std::remove_cv_t<std::remove_reference_t<_Tp>>>;
46+
47+
} // namespace function_backports

0 commit comments

Comments
 (0)