Рефакторинг Redis

This commit is contained in:
Andrei Astafev 2020-04-23 13:14:15 +03:00
parent b242a96259
commit 16b6b5b322
7 changed files with 35 additions and 37 deletions

View File

@ -33,7 +33,7 @@ public:
/**
* @brief Destroys the client
*/
virtual ~Client() = default;
~Client() override = default;
/*
* Note: we specifically avoid an overload of connectToHost that

View File

@ -1,16 +1,16 @@
#ifndef MYX_REDIS_CLIENT_P_HPP_
#define MYX_REDIS_CLIENT_P_HPP_
#include <myx/redis/client.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/parser.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/request.hpp>
#include <QObject>
#include <QQueue>
#include <QTcpSocket>
#include <client.hpp>
#include <reply.hpp>
#include <request.hpp>
#include <lexer.hpp>
#include <parser.hpp>
namespace myx {
namespace redis {
@ -20,7 +20,7 @@ class ClientPrivate : public QObject
Q_OBJECT
public:
ClientPrivate( Client* );
explicit ClientPrivate( Client* = nullptr );
QTcpSocket socket;
QQueue< Request* > queue;

View File

@ -14,7 +14,7 @@ namespace redis {
Lexer::Lexer( QIODevice* device, QObject* parent ) :
QObject ( parent ),
m_device( device ),
m_state ( DoingNothing ),
m_state ( kDoingNothing ),
m_crlf ( 0 ),
m_length( 0 )
{
@ -28,27 +28,27 @@ void Lexer::readData()
while ( true )
{
if ( ( m_state == DoingNothing ) && !readCharacter() )
if ( ( m_state == kDoingNothing ) && !readCharacter() )
{
break;
}
switch ( m_state )
{
case ReadingLength:
case ReadingUnsafeString:
case kReadingLength:
case kReadingUnsafeString:
if ( !readUnsafeString() ) { return; }
break;
case ReadingSafeString:
case kReadingSafeString:
if ( !readSafeString() ) { return; }
break;
case DoingNothing:
case kDoingNothing:
break;
}
if ( m_state != ReadingSafeString )
if ( m_state != kReadingSafeString )
{
m_state = DoingNothing;
m_state = kDoingNothing;
}
}
} // Lexer::readData
@ -70,9 +70,9 @@ bool Lexer::readCharacter()
case '-':
case ':':
case '*':
m_state = ReadingUnsafeString; break;
m_state = kReadingUnsafeString; break;
case '$':
m_state = ReadingLength; break;
m_state = kReadingLength; break;
}
Q_EMIT character( c );
@ -92,10 +92,10 @@ bool Lexer::readUnsafeString()
QString s = m_buffer.mid( 0, m_crlf );
m_buffer.remove( 0, m_crlf + 2 );
if ( m_state == ReadingLength )
if ( m_state == kReadingLength )
{
m_length = s.toInt();
m_state = ReadingSafeString;
m_state = kReadingSafeString;
}
else
{
@ -119,7 +119,7 @@ bool Lexer::readSafeString()
Q_EMIT safeString( d );
m_state = DoingNothing;
m_state = kDoingNothing;
return( true );
}

View File

@ -17,8 +17,8 @@ class Lexer : public QObject
public:
Lexer( QIODevice*, QObject* = nullptr );
virtual ~Lexer() = default;
explicit Lexer( QIODevice*, QObject* = nullptr );
~Lexer() override = default;
Q_SIGNAL void character( char );
Q_SIGNAL void unsafeString( const QString& );
@ -38,10 +38,10 @@ private:
enum
{
DoingNothing,
ReadingLength,
ReadingUnsafeString,
ReadingSafeString
kDoingNothing,
kReadingLength,
kReadingUnsafeString,
kReadingSafeString
} m_state;
int m_crlf;

View File

@ -5,8 +5,8 @@
#include <myx/base/config.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/parser_p.hpp>
#include <myx/redis/reply.hpp>
#include <QList>
#include <QPair>
@ -21,17 +21,16 @@ class Parser : public QObject
Q_OBJECT
public:
Parser( Lexer*, QObject* = nullptr );
virtual ~Parser() = default;
explicit Parser( Lexer*, QObject* = nullptr );
~Parser() override = default;
Q_SIGNAL void reply( const myx::redis::Reply& );
private:
Q_SLOT void readCharacter( const char );
Q_SLOT void readCharacter( char );
Q_SLOT void readUnsafeString( const QString& );
Q_SLOT void readSafeString( const QByteArray& );
private:
void descend();
QList< ParserTaskPrivate > m_stack;

View File

@ -75,14 +75,13 @@ public:
/**
* @brief Creates an empty reply
*/
Reply() :
m_type( Invalid ) {}
Reply() = default;
/**
* @brief Initializes the reply
* @param type the type of the reply
*/
Reply( Type type ) :
explicit Reply( Type type ) :
m_type( type ) {}
/**
@ -99,7 +98,7 @@ public:
private:
Type m_type;
Type m_type { Invalid };
QVariant m_value;
}; // class Reply

View File

@ -33,7 +33,7 @@ public:
/**
* @brief Destroys the request
*/
virtual ~Request() = default;
~Request() override = default;
/**
* @brief Waits for the reply to be received