2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/ApplyTuple.h>
20 #include <gtest/gtest.h>
24 // this placates visual studio stupidity - see
25 // http://stackoverflow.com/questions/5503901
30 void func(int a, int b, double c) {
37 void func(int a, int b, double c) {
41 double retVal(int a, double b) {
46 Wat(Wat const&) = delete;
52 int func(int) { return 0; }
53 bool func(bool) { return true; }
57 int operator()() const {
64 CopyCount(CopyCount const&) {
65 std::cout << "copy count copy ctor\n";
69 void anotherFunc(CopyCount const&) {}
71 std::function<void (int, int, double)> makeFunc() {
76 GuardObjBase(GuardObjBase&&) {}
78 GuardObjBase(GuardObjBase const&) = delete;
79 GuardObjBase& operator=(GuardObjBase const&) = delete;
81 typedef GuardObjBase const& Guard;
83 template<class F, class Tuple>
84 struct GuardObj : GuardObjBase {
85 explicit GuardObj(F&& f, Tuple&& args)
87 , args_(std::move(args))
89 GuardObj(GuardObj&& g)
90 : GuardObjBase(std::move(g))
92 , args_(std::move(g.args_))
96 folly::applyTuple(f_, args_);
99 GuardObj(const GuardObj&) = delete;
100 GuardObj& operator=(const GuardObj&) = delete;
107 template<class F, class ...Args>
108 GuardObj<typename std::decay<F>::type,std::tuple<Args...>>
109 guard(F&& f, Args&&... args) {
110 return GuardObj<typename std::decay<F>::type,std::tuple<Args...>>(
112 std::tuple<Args...>(std::forward<Args>(args)...)
119 Mover(const Mover&) = delete;
120 Mover& operator=(const Mover&) = delete;
123 void move_only_func(Mover&&) {}
127 TEST(ApplyTuple, Test) {
128 auto argsTuple = std::make_tuple(1, 2, 3.0);
130 folly::applyTuple(func2, argsTuple);
131 folly::applyTuple(func, argsTuple);
132 folly::applyTuple(func, std::make_tuple(1, 2, 3.0));
133 folly::applyTuple(makeFunc(), std::make_tuple(1, 2, 3.0));
134 folly::applyTuple(makeFunc(), argsTuple);
136 std::unique_ptr<Wat> wat(new Wat);
137 folly::applyTuple(&Wat::func, std::make_tuple(wat.get(), 1, 2, 3.0));
138 auto argsTuple2 = std::make_tuple(wat.get(), 1, 2, 3.0);
139 folly::applyTuple(&Wat::func, argsTuple2);
142 folly::applyTuple(&Wat::retVal,
143 std::make_tuple(wat.get(), 1, 9.0)));
145 auto test = guard(func, 1, 2, 3.0);
147 auto test2 = guard(anotherFunc, cpy);
148 auto test3 = guard(anotherFunc, std::cref(cpy));
153 static_cast<int (Overloaded::*)(int)>(&Overloaded::func),
154 std::make_tuple(&ovl, 12)));
157 static_cast<bool (Overloaded::*)(bool)>(&Overloaded::func),
158 std::make_tuple(&ovl, false)));
160 int x = folly::applyTuple(std::plus<int>(), std::make_tuple(12, 12));
164 folly::applyTuple(move_only_func,
165 std::forward_as_tuple(std::forward<Mover>(Mover())));