From 3b57d01b0f0219008bc14de7ad7b7a2bca9cdfb3 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 28 Nov 2016 09:50:48 +0000 Subject: [PATCH] Add some convenience methods to check the type of a reply. --- include/incredis/reply.hpp | 7 +++++++ src/reply.cpp | 34 +++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/include/incredis/reply.hpp b/include/incredis/reply.hpp index 2776b16..1539dc7 100644 --- a/include/incredis/reply.hpp +++ b/include/incredis/reply.hpp @@ -77,6 +77,13 @@ namespace redis { Reply ( StatusString&& parVal ) : base_class(std::move(parVal)) {} Reply ( std::nullptr_t parVal ) : base_class(parVal) {} ~Reply ( void ) noexcept = default; + + bool is_integer ( void ) const; + bool is_string ( void ) const; + bool is_array ( void ) const; + bool is_error ( void ) const; + bool is_status ( void ) const; + bool is_nil ( void ) const; }; const long long& get_integer ( const Reply& parReply ); diff --git a/src/reply.cpp b/src/reply.cpp index 1bc2bcc..c3aaf2a 100644 --- a/src/reply.cpp +++ b/src/reply.cpp @@ -21,16 +21,16 @@ namespace redis { const long long& get_integer (const Reply& parReply) { - assert(RedisVariantType_Integer == parReply.which()); + assert(parReply.is_integer()); return boost::get(parReply); } const std::string& get_string (const Reply& parReply) { static const std::string empty_str; - if (RedisVariantType_Nil == parReply.which()) + if (parReply.is_nil()) return empty_str; - assert(RedisVariantType_String == parReply.which()); + assert(parReply.is_string()); return boost::get(parReply); } @@ -50,12 +50,12 @@ namespace redis { } const std::vector& get_array (const Reply& parReply) { - assert(RedisVariantType_Array == parReply.which()); + assert(parReply.is_array()); return boost::get>(parReply); } const ErrorString& get_error_string (const Reply& parReply) { - assert(RedisVariantType_Error == parReply.which()); + assert(parReply.is_error()); return boost::get(parReply); } @@ -83,4 +83,28 @@ namespace redis { template const std::vector& get> ( const Reply& parReply ); template const long long& get ( const Reply& parReply ); template const ErrorString& get ( const Reply& parReply ); + + bool Reply::is_integer() const { + return RedisVariantType_Integer == this->which(); + } + + bool Reply::is_string() const { + return RedisVariantType_String == this->which(); + } + + bool Reply::is_array() const { + return RedisVariantType_Array == this->which(); + } + + bool Reply::is_error() const { + return RedisVariantType_Error == this->which(); + } + + bool Reply::is_status() const { + return RedisVariantType_Status == this->which(); + } + + bool Reply::is_nil() const { + return RedisVariantType_Nil == this->which(); + } } //namespace redis