forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathclient.ipp
More file actions
148 lines (127 loc) · 5.37 KB
/
client.ipp
File metadata and controls
148 lines (127 loc) · 5.37 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// The MIT License (MIT)
//
// Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <functional>
#include <iostream>
namespace cpp_redis {
template <typename T>
typename std::enable_if<std::is_same<T, client::client_type>::value>::type
client::client_kill_unpack_arg(std::vector<string_t> &redis_cmd,
reply_callback_t &, client_type type) {
redis_cmd.emplace_back("TYPE");
string_t type_string;
switch (type) {
case client_type::normal:
type_string = "normal";
break;
case client_type::master:
type_string = "master";
break;
case client_type::pubsub:
type_string = "pubsub";
break;
case client_type::slave:
type_string = "slave";
break;
}
redis_cmd.emplace_back(type_string);
}
template <typename T>
typename std::enable_if<std::is_same<T, bool>::value>::type
client::client_kill_unpack_arg(std::vector<string_t> &redis_cmd,
reply_callback_t &, bool skip) {
redis_cmd.emplace_back("SKIPME");
redis_cmd.emplace_back(skip ? "yes" : "no");
}
template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type
client::client_kill_unpack_arg(std::vector<string_t> &redis_cmd,
reply_callback_t &, uint64_t id) {
redis_cmd.emplace_back("ID");
redis_cmd.emplace_back(std::to_string(id));
}
template <typename T>
typename std::enable_if<std::is_class<T>::value>::type
client::client_kill_unpack_arg(std::vector<string_t> &,
reply_callback_t &reply_callback, const T &cb) {
reply_callback = cb;
}
template <typename T, typename... Ts>
void client::client_kill_impl(std::vector<string_t> &redis_cmd,
reply_callback_t &reply, const T &arg,
const Ts &... args) {
static_assert(!std::is_class<T>::value,
"Reply callback should be in the end of the argument list");
client_kill_unpack_arg<T>(redis_cmd, reply, arg);
client_kill_impl(redis_cmd, reply, args...);
}
template <typename T>
void client::client_kill_impl(std::vector<string_t> &redis_cmd,
reply_callback_t &reply, const T &arg) {
client_kill_unpack_arg<T>(redis_cmd, reply, arg);
}
template <typename T, typename... Ts>
inline client &client::client_kill(const T &arg, const Ts &... args) {
static_assert(helpers::is_different_types<T, Ts...>::value,
"Should only have one distinct value per filter type");
static_assert(
!(std::is_class<T>::value &&
std::is_same<T, typename helpers::back<T, Ts...>::type>::value),
"Should have at least one filter");
std::vector<string_t> redis_cmd({"CLIENT", "KILL"});
reply_callback_t reply_cb = nullptr;
client_kill_impl<T, Ts...>(redis_cmd, reply_cb, arg, args...);
return send(redis_cmd, reply_cb);
}
template <typename T, typename... Ts>
inline client &client::client_kill(const string_t &host, int port, const T &arg,
const Ts &... args) {
static_assert(helpers::is_different_types<T, Ts...>::value,
"Should only have one distinct value per filter type");
std::vector<string_t> redis_cmd({"CLIENT", "KILL"});
//! If we have other type than lambda, then it's a filter
if (!std::is_class<T>::value) {
redis_cmd.emplace_back("ADDR");
}
redis_cmd.emplace_back(host + ":" + std::to_string(port));
reply_callback_t reply_cb = nullptr;
client_kill_impl<T, Ts...>(redis_cmd, reply_cb, arg, args...);
return send(redis_cmd, reply_cb);
}
inline client &client::client_kill(const string_t &host, int port) {
return client_kill(host, port, reply_callback_t(nullptr));
}
template <typename... Ts>
inline client &client::client_kill(const char *host, int port,
const Ts &... args) {
return client_kill(string_t(host), port, args...);
}
template <typename T, typename... Ts>
std::future<reply> client::client_kill_future(const T arg, const Ts... args) {
//! gcc 4.8 doesn't handle variadic template capture arguments (appears
//! in 4.9) so std::bind should capture all arguments because of the compiler.
return exec_cmd(std::bind(
[this](T arg, Ts... args, const reply_callback_t &cb) -> client & {
return client_kill(arg, args..., cb);
},
arg, args..., std::placeholders::_1));
}
} // namespace cpp_redis