From b8ea4e1d3bde4181812d72722735f62eda9f8356 Mon Sep 17 00:00:00 2001 From: fbraem Date: Thu, 12 Nov 2015 23:37:14 +0100 Subject: [PATCH] Add operator<< and make use of template function for add --- Redis/include/Poco/Redis/Array.h | 69 ++++++++++++++++++-------------- Redis/src/Array.cpp | 2 +- Redis/src/Client.cpp | 2 +- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Redis/include/Poco/Redis/Array.h b/Redis/include/Poco/Redis/Array.h index 34bc1b6af..b1c1548af 100644 --- a/Redis/include/Poco/Redis/Array.h +++ b/Redis/include/Poco/Redis/Array.h @@ -47,26 +47,34 @@ public: virtual ~Array(); /// Destructor. - Array& add(Poco::Int64 value); - /// Adds an integer element. + template + Array& operator<<(const T& arg) + /// Adds the argument to the array + { + addRedisType(new Type(arg)); + return *this; + } - Array& add(const std::string& value); - /// Adds a bulk string element. A bulk string is a string that - /// is binary safe (it can contain newlines). If you want a simple - /// string use addSimpleString (can't contain a newline!). - - Array& add(const BulkString& value); - /// Adds a bulk string element. + Array& operator<<(const char* s); Array& add(); - /// Adds a Null bulk string element. + /// Adds an Null BulkString - Array& add(RedisType::Ptr value); + template + Array& add(const T& arg) + /// Adds an element to the array. + /// Note: the specialization for std::string will add a BulkString! + /// If you really need a simple string, call addSimpleString. + { + addRedisType(new Type(arg)); + return *this; + } + + Array& add(const char* s); + + Array& addRedisType(RedisType::Ptr value); /// Adds a Redis element. - Array& add(const Array& array); - /// Adds an array. - Array& addSimpleString(const std::string& value); /// Adds a simple string (can't contain newline characters!) @@ -124,35 +132,34 @@ private: void checkNull(); }; +inline Array& Array::operator<<(const char* s) +{ + BulkString value(s); + return add(value); +} + inline Array& Array::add() { BulkString value; - return add(new Type(value)); + return add(value); } -inline Array& Array::add(Int64 value) +template<> +inline Array& Array::add(const std::string& arg) { - return add(new Type(value)); + BulkString value(arg); + return add(value); } -inline Array& Array::add(const std::string& value) +inline Array& Array::add(const char* s) { - return add(new Type(value)); -} - -inline Array& Array::add(const BulkString& value) -{ - return add(new Type(value)); -} - -inline Array& Array::add(const Array& value) -{ - return add(new Type(value)); + BulkString value(s); + return add(value); } inline Array& Array::addSimpleString(const std::string& value) { - return add(new Type(value)); + return addRedisType(new Type(value)); } inline Array::const_iterator Array::begin() const @@ -241,7 +248,7 @@ struct RedisTypeTraits throw RedisException("Wrong answer received from Redis server"); element->read(input); - value.add(element); + value.addRedisType(element); } } } diff --git a/Redis/src/Array.cpp b/Redis/src/Array.cpp index dcceab844..f4f56754c 100644 --- a/Redis/src/Array.cpp +++ b/Redis/src/Array.cpp @@ -36,7 +36,7 @@ Array::~Array() } -Array& Array::add(RedisType::Ptr value) +Array& Array::addRedisType(RedisType::Ptr value) { checkNull(); diff --git a/Redis/src/Client.cpp b/Redis/src/Client.cpp index d63906561..80a6c6222 100644 --- a/Redis/src/Client.cpp +++ b/Redis/src/Client.cpp @@ -167,7 +167,7 @@ Array Client::sendCommands(const std::vector& commands) for(int i = 0; i < commands.size(); ++i) { - results.add(readReply()); + results.addRedisType(readReply()); } return results;