myxlib/src/myx/redis/request.hpp

82 lines
1.4 KiB
C++

#ifndef MYX_REDIS_REQUEST_HPP_
#define MYX_REDIS_REQUEST_HPP_
#pragma once
#include <myx/base/config.hpp>
#include <myx/redis/reply.hpp>
#include <QEventLoop>
#include <QScopedPointer>
namespace myx {
namespace redis {
class Loop : public QObject
{
Q_OBJECT
friend class Request;
explicit Loop( QObject* parent = nullptr ) { Q_UNUSED( parent ) }
QEventLoop m_loop;
Q_SLOT void quitEventLoop() { m_loop.exit( 1 ); }
};
/**
* @brief Represents a Redis command and its response
*/
class Request : public QObject
{
Q_OBJECT
public:
/**
* @brief Initializes the request
* @param parent the parent QObject
*/
explicit Request( QObject* parent = nullptr );
Request( const Request& ) = delete;
Request& operator=( const Request& ) = delete;
Request( Request&& ) = delete;
Request& operator=( Request&& ) = delete;
/**
* @brief Destroys the request
*/
~Request() override = default;
/**
* @brief Waits for the reply to be received
* @param msecs the amount of time in milliseconds to wait
* @return true if the reply was received
*/
bool waitForReply( int msecs = 10000 );
/**
* @brief Emitted when a reply is received
* @param reply the reply received
*/
Q_SIGNAL void reply( myx::redis::Reply& reply );
private:
const QScopedPointer< Loop > d;
}; // class Request
} // namespace redis
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "request-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_REQUEST_HPP_