From cd2060232099230084d03d345ca3f687084a7f73 Mon Sep 17 00:00:00 2001 From: fbraem Date: Thu, 12 Nov 2015 19:06:35 +0100 Subject: [PATCH] Rename ElementTraits to RedisTypeTraits / Move read to traits --- Redis/include/Poco/Redis/Array.h | 35 ++++++------ Redis/include/Poco/Redis/Client.h | 4 +- Redis/include/Poco/Redis/Error.h | 14 +++-- Redis/include/Poco/Redis/Type.h | 90 +++++++++++++++---------------- Redis/src/Array.cpp | 4 +- Redis/src/Type.cpp | 10 ++-- 6 files changed, 77 insertions(+), 80 deletions(-) diff --git a/Redis/include/Poco/Redis/Array.h b/Redis/include/Poco/Redis/Array.h index 850550af9..34bc1b6af 100644 --- a/Redis/include/Poco/Redis/Array.h +++ b/Redis/include/Poco/Redis/Array.h @@ -94,7 +94,7 @@ public: if ( pos >= _elements.value().size() ) throw InvalidArgumentException(); RedisType::Ptr element = _elements.value().at(pos); - if ( ElementTraits::TypeId == element->type() ) + if ( RedisTypeTraits::TypeId == element->type() ) { Type* concrete = dynamic_cast* >(element.get()); if ( concrete != NULL ) return concrete->value(); @@ -198,7 +198,7 @@ inline size_t Array::size() const template<> -struct ElementTraits +struct RedisTypeTraits { enum { TypeId = RedisType::REDIS_ARRAY }; @@ -223,28 +223,29 @@ struct ElementTraits } return result.str(); } -}; -template<> inline -void Type::read(RedisInputStream& input) -{ - Int64 length = NumberParser::parse64(input.getline()); - - if ( length != -1 ) + static void read(RedisInputStream& input, Array& value) { - for(int i = 0; i < length; ++i) + value.clear(); + + Int64 length = NumberParser::parse64(input.getline()); + + if ( length != -1 ) { - char marker = input.get(); - RedisType::Ptr element = Type::createRedisType(marker); + for(int i = 0; i < length; ++i) + { + char marker = input.get(); + RedisType::Ptr element = RedisType::createRedisType(marker); - if ( element.isNull() ) - throw RedisException("Wrong answer received from Redis server"); + if ( element.isNull() ) + throw RedisException("Wrong answer received from Redis server"); - element->read(input); - _value.add(element); + element->read(input); + value.add(element); + } } } -} +}; }} diff --git a/Redis/include/Poco/Redis/Client.h b/Redis/include/Poco/Redis/Client.h index 40f2ca45a..8650ef466 100644 --- a/Redis/include/Poco/Redis/Client.h +++ b/Redis/include/Poco/Redis/Client.h @@ -143,13 +143,13 @@ public: /// the reply is not of the given type. { RedisType::Ptr redisResult = readReply(); - if (redisResult->type() == ElementTraits::TypeId) + if (redisResult->type() == RedisTypeTraits::TypeId) { Type* error = dynamic_cast*>(redisResult.get()); throw RedisException(error->value().getMessage()); } - if (redisResult->type() == ElementTraits::TypeId) + if (redisResult->type() == RedisTypeTraits::TypeId) { Type* type = dynamic_cast*>(redisResult.get()); if (type != NULL) result = type->value(); diff --git a/Redis/include/Poco/Redis/Error.h b/Redis/include/Poco/Redis/Error.h index 1fccad94e..f9464c40c 100644 --- a/Redis/include/Poco/Redis/Error.h +++ b/Redis/include/Poco/Redis/Error.h @@ -51,7 +51,7 @@ inline void Error::setMessage(const std::string& message) } template<> -struct ElementTraits +struct RedisTypeTraits { enum { TypeId = RedisType::REDIS_ERROR }; @@ -61,15 +61,13 @@ struct ElementTraits { return marker + value.getMessage() + LineEnding::NEWLINE_CRLF; } + + static void read(RedisInputStream& input, Error& value) + { + value.setMessage(input.getline()); + } }; - -template<> inline -void Type::read(RedisInputStream& input) -{ - _value = input.getline(); -} - }} // Namespace Poco::Redis #endif // Redis_Error_INCLUDED \ No newline at end of file diff --git a/Redis/include/Poco/Redis/Type.h b/Redis/include/Poco/Redis/Type.h index dd82960bb..9a930964f 100644 --- a/Redis/include/Poco/Redis/Type.h +++ b/Redis/include/Poco/Redis/Type.h @@ -102,27 +102,34 @@ inline bool RedisType::isSimpleString() const } template -struct ElementTraits +struct RedisTypeTraits { }; template<> -struct ElementTraits +struct RedisTypeTraits { enum { TypeId = RedisType::REDIS_INTEGER }; static const char marker = ':'; - static std::string toString(const Poco::Int64& value) + static std::string toString(const Int64& value) { return marker + NumberFormatter::format(value) + "\r\n"; } + + static void read(RedisInputStream& input, Int64& value) + { + std::string number = input.getline(); + value = NumberParser::parse64(number); + } + }; template<> -struct ElementTraits +struct RedisTypeTraits { enum { TypeId = RedisType::REDIS_SIMPLE_STRING }; @@ -132,6 +139,11 @@ struct ElementTraits { return marker + value + LineEnding::NEWLINE_CRLF; } + + static void read(RedisInputStream& input, std::string& value) + { + value = input.getline(); + } }; @@ -141,7 +153,7 @@ typedef Nullable BulkString; template<> -struct ElementTraits +struct RedisTypeTraits { enum { TypeId = RedisType::REDIS_BULK_STRING }; @@ -163,6 +175,24 @@ struct ElementTraits + LineEnding::NEWLINE_CRLF; } } + + static void read(RedisInputStream& input, BulkString& value) + { + value.clear(); + + std::string line = input.getline(); + int length = NumberParser::parse64(line); + + if ( length >= 0 ) + { + std::string s; + s.resize(length, ' '); + input.read(&*s.begin(), length); + value.assign(s); + + input.getline(); // Read and ignore /r/n + } + } }; @@ -189,14 +219,17 @@ public: int type() const { - return ElementTraits::TypeId; + return RedisTypeTraits::TypeId; } - virtual void read(RedisInputStream& socket); - - virtual std::string toString() const + void read(RedisInputStream& socket) { - return ElementTraits::toString(_value); + RedisTypeTraits::read(socket, _value); + } + + std::string toString() const + { + return RedisTypeTraits::toString(_value); } T& value() @@ -214,41 +247,6 @@ private: T _value; }; -template<> inline -void Type::read(RedisInputStream& input) -{ - std::string number = input.getline(); - _value = NumberParser::parse64(number); -} - -template<> inline -void Type::read(RedisInputStream& input) -{ - _value.clear(); - _value = input.getline(); -} - -template<> inline -void Type::read(RedisInputStream& input) -{ - _value.clear(); - - std::string line = input.getline(); - int length = NumberParser::parse64(line); - - if ( length >= 0 ) - { - std::string s; - s.resize(length, ' '); - input.read(&*s.begin(), length); - _value.assign(s); - - line = input.getline(); - } - -} - - -}} +}} // Namespace Poco/Redis #endif // Redis_Type_INCLUDED diff --git a/Redis/src/Array.cpp b/Redis/src/Array.cpp index bcf8ef507..dcceab844 100644 --- a/Redis/src/Array.cpp +++ b/Redis/src/Array.cpp @@ -46,9 +46,9 @@ Array& Array::add(RedisType::Ptr value) } -std::string Array::toString() const +std::string Array::toString() const { - return ElementTraits::toString(*this); + return RedisTypeTraits::toString(*this); } } } diff --git a/Redis/src/Type.cpp b/Redis/src/Type.cpp index e0962f62b..68ce05a90 100644 --- a/Redis/src/Type.cpp +++ b/Redis/src/Type.cpp @@ -38,19 +38,19 @@ RedisType::Ptr RedisType::createRedisType(char marker) switch(marker) { - case ElementTraits::marker : + case RedisTypeTraits::marker : result = new Type(); break; - case ElementTraits::marker : + case RedisTypeTraits::marker : result = new Type(); break; - case ElementTraits::marker : + case RedisTypeTraits::marker : result = new Type(); break; - case ElementTraits::marker : + case RedisTypeTraits::marker : result = new Type(); break; - case ElementTraits::marker : + case RedisTypeTraits::marker : result = new Type(); break; }