forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Variadic.h
95 lines (82 loc) · 2.38 KB
/
Variadic.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <cstdint>
#include <tuple>
#include <type_traits>
#include <utility>
#include <c10/util/ArrayRef.h>
#include <ATen/core/List.h>
namespace at {
// This class allows you to write variadic functions which
// call a (possibly overloaded) function on each argument,
// in order. This is most commonly used in autogenerated code,
// where it is convenient to have a function that can uniformly
// take arguments of different types. If your arguments
// are homogenous consider using a std::initializer_list instead.
//
// For examples of this in use, see torch/csrc/utils/variadic.h
template <typename F>
struct IterArgs {
template <typename... Args>
inline F& apply() {
return self();
}
// NB: Use perfect forwarding here, otherwise we'll make value
// copies of all arguments!
template <typename T, typename... Args>
inline F& apply(T&& arg, Args&&... args) {
self()(std::forward<T>(arg));
if (self().short_circuit()) {
return self();
} else {
return apply(std::forward<Args>(args)...);
}
}
// Here are some handy overloads which provide sensible
// defaults for container-like structures that one might
// be interested in recursing into. You can enable them
// by adding:
//
// using IterArgs<YourStructName>::operator()
//
// to your struct. These are not enabled by default because
// you may be able to process these structures more efficiently
// than handling them one-by-one.
template <typename T>
void operator()(c10::IListRef<T> args) {
for (const auto& arg : args) {
self()(arg);
if (self().short_circuit())
return;
}
}
template <typename T>
void operator()(at::ArrayRef<T> args) {
for (const auto& arg : args) {
self()(arg);
if (self().short_circuit())
return;
}
}
template <typename T>
void operator()(const torch::List<T>& args) {
for (const auto& arg : args) {
self()(arg);
if (self().short_circuit())
return;
}
}
// NB: we need to specify std::vector manually as C++ won't
// do an implicit conversion to make a template deduction go through.
template <typename T>
void operator()(const std::vector<T>& args) {
self()(at::ArrayRef<T>{args});
}
constexpr bool short_circuit() const {
return false;
}
private:
inline F& self() {
return *static_cast<F*>(this);
}
};
} // namespace torch