/************************************************************************** Copyright (c) 2017 sewenew Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *************************************************************************/ #ifndef SEWENEW_REDISPLUSPLUS_COMMAND_ARGS_H #define SEWENEW_REDISPLUSPLUS_COMMAND_ARGS_H #include #include #include #include #include "utils.h" namespace sw { namespace redis { class CmdArgs { public: template CmdArgs& append(Arg &&arg); template CmdArgs& append(Arg &&arg, Args &&...args); // All overloads of operator<< are for internal use only. CmdArgs& operator<<(const StringView &arg); template ::type>::value, int>::type = 0> CmdArgs& operator<<(T &&arg); template CmdArgs& operator<<(const std::pair &range); template auto operator<<(const std::tuple &) -> typename std::enable_if::type { return *this; } template auto operator<<(const std::tuple &arg) -> typename std::enable_if::type; const char** argv() { return _argv.data(); } const std::size_t* argv_len() { return _argv_len.data(); } std::size_t size() const { return _argv.size(); } private: // Deep copy. CmdArgs& _append(std::string arg); // Shallow copy. CmdArgs& _append(const StringView &arg); // Shallow copy. CmdArgs& _append(const char *arg); template ::type>::value, int>::type = 0> CmdArgs& _append(T &&arg) { return operator<<(std::forward(arg)); } template CmdArgs& _append(std::true_type, const std::pair &range); template CmdArgs& _append(std::false_type, const std::pair &range); std::vector _argv; std::vector _argv_len; std::list _args; }; template inline CmdArgs& CmdArgs::append(Arg &&arg) { return _append(std::forward(arg)); } template inline CmdArgs& CmdArgs::append(Arg &&arg, Args &&...args) { _append(std::forward(arg)); return append(std::forward(args)...); } inline CmdArgs& CmdArgs::operator<<(const StringView &arg) { _argv.push_back(arg.data()); _argv_len.push_back(arg.size()); return *this; } template inline CmdArgs& CmdArgs::operator<<(const std::pair &range) { return _append(IsKvPair())>::type>(), range); } template ::type>::value, int>::type> inline CmdArgs& CmdArgs::operator<<(T &&arg) { return _append(std::to_string(std::forward(arg))); } template auto CmdArgs::operator<<(const std::tuple &arg) -> typename std::enable_if::type { operator<<(std::get(arg)); return operator<<(arg); } inline CmdArgs& CmdArgs::_append(std::string arg) { _args.push_back(std::move(arg)); return operator<<(_args.back()); } inline CmdArgs& CmdArgs::_append(const StringView &arg) { return operator<<(arg); } inline CmdArgs& CmdArgs::_append(const char *arg) { return operator<<(arg); } template CmdArgs& CmdArgs::_append(std::false_type, const std::pair &range) { auto first = range.first; auto last = range.second; while (first != last) { *this << *first; ++first; } return *this; } template CmdArgs& CmdArgs::_append(std::true_type, const std::pair &range) { auto first = range.first; auto last = range.second; while (first != last) { *this << first->first << first->second; ++first; } return *this; } } } #endif // end SEWENEW_REDISPLUSPLUS_COMMAND_ARGS_H