forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_utils.hh
62 lines (57 loc) · 2.01 KB
/
rpc_utils.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (C) 2023-present ScyllaDB
*
*/
/*
* SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0)
*/
#pragma once
#include "utils/tuple_utils.hh"
#include <seastar/core/shared_ptr.hh>
#include <seastar/core/sharded.hh>
namespace utils {
namespace internal {
class rpc_default_value {
template <typename T>
struct marker {};
template <typename T>
static auto create(marker<std::unique_ptr<T>>) {
return std::make_unique<T>();
}
template <typename T>
static auto create(marker<std::shared_ptr<T>>) {
return std::make_shared<T>();
}
template <typename T>
static auto create(marker<seastar::lw_shared_ptr<T>>) {
return make_lw_shared<T>();
}
template <typename T>
static auto create(marker<seastar::shared_ptr<T>>) {
return make_shared<T>();
}
template <typename T>
static auto create(marker<seastar::foreign_ptr<T>>) {
return make_foreign(create<T>());
}
template <typename T>
static auto create(marker<T>) {
return T{};
}
public:
template <typename T>
static T create() {
return create(marker<T>{});
}
};
}
// The Seastar RPC infrastructure does not support null values in tuples in handler responses.
// This function generates a tuple by default constructing all of its elements, except for
// smart pointer types. For those, it returns a pointer to the default constructed value.
template <Tuple T>
T make_default_rpc_tuple() {
return std::invoke([&]<std::size_t... Is>(std::index_sequence<Is...>) {
return T { internal::rpc_default_value::create<std::tuple_element_t<Is, T>>()... };
}, std::make_index_sequence<tuple_ex_size_v<T>>());
}
}