Rename ElementTraits to RedisTypeTraits / Move read to traits

This commit is contained in:
fbraem 2015-11-12 19:06:35 +01:00
parent b7bdbf89f0
commit cd20602320
6 changed files with 77 additions and 80 deletions

View File

@ -94,7 +94,7 @@ public:
if ( pos >= _elements.value().size() ) throw InvalidArgumentException();
RedisType::Ptr element = _elements.value().at(pos);
if ( ElementTraits<T>::TypeId == element->type() )
if ( RedisTypeTraits<T>::TypeId == element->type() )
{
Type<T>* concrete = dynamic_cast<Type<T>* >(element.get());
if ( concrete != NULL ) return concrete->value();
@ -198,7 +198,7 @@ inline size_t Array::size() const
template<>
struct ElementTraits<Array>
struct RedisTypeTraits<Array>
{
enum { TypeId = RedisType::REDIS_ARRAY };
@ -223,28 +223,29 @@ struct ElementTraits<Array>
}
return result.str();
}
};
template<> inline
void Type<Array>::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);
}
}
}
}
};
}}

View File

@ -143,13 +143,13 @@ public:
/// the reply is not of the given type.
{
RedisType::Ptr redisResult = readReply();
if (redisResult->type() == ElementTraits<Error>::TypeId)
if (redisResult->type() == RedisTypeTraits<Error>::TypeId)
{
Type<Error>* error = dynamic_cast<Type<Error>*>(redisResult.get());
throw RedisException(error->value().getMessage());
}
if (redisResult->type() == ElementTraits<T>::TypeId)
if (redisResult->type() == RedisTypeTraits<T>::TypeId)
{
Type<T>* type = dynamic_cast<Type<T>*>(redisResult.get());
if (type != NULL) result = type->value();

View File

@ -51,7 +51,7 @@ inline void Error::setMessage(const std::string& message)
}
template<>
struct ElementTraits<Error>
struct RedisTypeTraits<Error>
{
enum { TypeId = RedisType::REDIS_ERROR };
@ -61,15 +61,13 @@ struct ElementTraits<Error>
{
return marker + value.getMessage() + LineEnding::NEWLINE_CRLF;
}
static void read(RedisInputStream& input, Error& value)
{
value.setMessage(input.getline());
}
};
template<> inline
void Type<Error>::read(RedisInputStream& input)
{
_value = input.getline();
}
}} // Namespace Poco::Redis
#endif // Redis_Error_INCLUDED

View File

@ -102,27 +102,34 @@ inline bool RedisType::isSimpleString() const
}
template<typename T>
struct ElementTraits
struct RedisTypeTraits
{
};
template<>
struct ElementTraits<Poco::Int64>
struct RedisTypeTraits<Int64>
{
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<std::string>
struct RedisTypeTraits<std::string>
{
enum { TypeId = RedisType::REDIS_SIMPLE_STRING };
@ -132,6 +139,11 @@ struct ElementTraits<std::string>
{
return marker + value + LineEnding::NEWLINE_CRLF;
}
static void read(RedisInputStream& input, std::string& value)
{
value = input.getline();
}
};
@ -141,7 +153,7 @@ typedef Nullable<std::string> BulkString;
template<>
struct ElementTraits<BulkString>
struct RedisTypeTraits<BulkString>
{
enum { TypeId = RedisType::REDIS_BULK_STRING };
@ -163,6 +175,24 @@ struct ElementTraits<BulkString>
+ 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<T>::TypeId;
return RedisTypeTraits<T>::TypeId;
}
virtual void read(RedisInputStream& socket);
virtual std::string toString() const
void read(RedisInputStream& socket)
{
return ElementTraits<T>::toString(_value);
RedisTypeTraits<T>::read(socket, _value);
}
std::string toString() const
{
return RedisTypeTraits<T>::toString(_value);
}
T& value()
@ -214,41 +247,6 @@ private:
T _value;
};
template<> inline
void Type<Int64>::read(RedisInputStream& input)
{
std::string number = input.getline();
_value = NumberParser::parse64(number);
}
template<> inline
void Type<std::string>::read(RedisInputStream& input)
{
_value.clear();
_value = input.getline();
}
template<> inline
void Type<BulkString>::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

View File

@ -46,9 +46,9 @@ Array& Array::add(RedisType::Ptr value)
}
std::string Array::toString() const
std::string Array::toString() const
{
return ElementTraits<Array>::toString(*this);
return RedisTypeTraits<Array>::toString(*this);
}
} }

View File

@ -38,19 +38,19 @@ RedisType::Ptr RedisType::createRedisType(char marker)
switch(marker)
{
case ElementTraits<Int64>::marker :
case RedisTypeTraits<Int64>::marker :
result = new Type<Int64>();
break;
case ElementTraits<std::string>::marker :
case RedisTypeTraits<std::string>::marker :
result = new Type<std::string>();
break;
case ElementTraits<BulkString>::marker :
case RedisTypeTraits<BulkString>::marker :
result = new Type<BulkString>();
break;
case ElementTraits<Array>::marker :
case RedisTypeTraits<Array>::marker :
result = new Type<Array>();
break;
case ElementTraits<Error>::marker :
case RedisTypeTraits<Error>::marker :
result = new Type<Error>();
break;
}