diff --git a/autotests/server/handlertest.cpp b/autotests/server/handlertest.cpp --- a/autotests/server/handlertest.cpp +++ b/autotests/server/handlertest.cpp @@ -99,6 +99,15 @@ { //MAKE_CMD_ROW(Protocol::Command::Invalid, UnknownCommandHandler) } + + template + QByteArray typeName(const T &t) + { + const auto &v = *t.get(); + return typeid(v).name(); + } + + private Q_SLOTS: void testFindAuthenticatedCommand_data() { @@ -110,9 +119,9 @@ { QFETCH(Protocol::Command::Type, command); QFETCH(QByteArray, className); - QScopedPointer handler(Handler::findHandlerForCommandAuthenticated(command)); - QVERIFY(!handler.isNull()); - QCOMPARE(QByteArray(typeid(*handler.data()).name()), className); + const auto handler = Handler::findHandlerForCommandAuthenticated(command); + QVERIFY(handler); + QCOMPARE(typeName(handler), className); } void testFindAuthenticatedCommandNegative_data() @@ -128,8 +137,8 @@ QFETCH(Protocol::Command::Type, command); QFETCH(QByteArray, className); - QScopedPointer handler(Handler::findHandlerForCommandAuthenticated(command)); - QVERIFY(handler.isNull()); + const auto handler = Handler::findHandlerForCommandAuthenticated(command); + QVERIFY(!handler); } void testFindNonAutenticatedCommand_data() @@ -143,26 +152,10 @@ QFETCH(Protocol::Command::Type, command); QFETCH(QByteArray, className); - QScopedPointer handler(Handler::findHandlerForCommandNonAuthenticated(command)); - QVERIFY(!handler.isNull()); - QCOMPARE(QByteArray(typeid(*handler.data()).name()), className); - } - - void testFindNonAutenticatedCommandNegative_data() - { - setupTestData(); - addAuthCommands(); - addAlwaysCommands(); - addInvalidCommands(); - } - - void testFindNonAutenticatedCommandNegative() - { - QFETCH(Protocol::Command::Type, command); - QFETCH(QByteArray, className); - QScopedPointer handler(Handler::findHandlerForCommandNonAuthenticated(command)); - QVERIFY(handler.isNull()); + auto handler = Handler::findHandlerForCommandNonAuthenticated(command); + QVERIFY(handler); + QCOMPARE(typeName(handler), className); } void testFindAlwaysCommand_data() @@ -176,9 +169,9 @@ QFETCH(Protocol::Command::Type, command); QFETCH(QByteArray, className); - QScopedPointer handler(Handler::findHandlerForCommandAlwaysAllowed(command)); - QVERIFY(!handler.isNull()); - QCOMPARE(QByteArray(typeid(*handler.data()).name()), className); + const auto handler = Handler::findHandlerForCommandAlwaysAllowed(command); + QVERIFY(handler); + QCOMPARE(typeName(handler), className); } void testFindAlwaysCommandNegative_data() @@ -194,8 +187,8 @@ QFETCH(Protocol::Command::Type, command); QFETCH(QByteArray, className); - QScopedPointer handler(Handler::findHandlerForCommandAlwaysAllowed(command)); - QVERIFY(handler.isNull()); + const auto handler = Handler::findHandlerForCommandAlwaysAllowed(command); + QVERIFY(!handler); } }; diff --git a/src/server/connection.h b/src/server/connection.h --- a/src/server/connection.h +++ b/src/server/connection.h @@ -100,7 +100,7 @@ void init() override; void quit() override; - Handler *findHandlerForCommand(Protocol::Command::Type cmd); + std::unique_ptr findHandlerForCommand(Protocol::Command::Type cmd); qint64 currentTag() const; diff --git a/src/server/connection.cpp b/src/server/connection.cpp --- a/src/server/connection.cpp +++ b/src/server/connection.cpp @@ -266,7 +266,7 @@ Tracer::self()->connectionInput(m_identifier, tag, cmd); } - m_currentHandler = std::unique_ptr(findHandlerForCommand(cmd->type())); + m_currentHandler = findHandlerForCommand(cmd->type()); if (!m_currentHandler) { qCWarning(AKONADISERVER_LOG) << "Invalid command: no such handler for" << cmd->type() << "on connection" << m_identifier; @@ -365,9 +365,9 @@ return const_cast(&m_context); } -Handler *Connection::findHandlerForCommand(Protocol::Command::Type command) +std::unique_ptr Connection::findHandlerForCommand(Protocol::Command::Type command) { - Handler *handler = Handler::findHandlerForCommandAlwaysAllowed(command); + auto handler = Handler::findHandlerForCommandAlwaysAllowed(command); if (handler) { return handler; } diff --git a/src/server/handler.h b/src/server/handler.h --- a/src/server/handler.h +++ b/src/server/handler.h @@ -71,21 +71,21 @@ * @param cmd the command string * @return an instance to the handler. The handler is deleted after @see handelLine is executed. The caller needs to delete the handler in case an exception is thrown from handelLine. */ - static Handler *findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd); + static std::unique_ptr findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd); /** * Find a handler for a command that is allowed when the client is not yet authenticated, like LOGIN. * @param cmd the command string * @return an instance to the handler. The handler is deleted after @see handelLine is executed. The caller needs to delete the handler in case an exception is thrown from handelLine. */ - static Handler *findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd); + static std::unique_ptr findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd); /** * Find a handler for a command that is allowed when the client is authenticated, like LIST, FETCH, etc. * @param cmd the command string * @return an instance to the handler. The handler is deleted after @see handelLine is executed. The caller needs to delete the handler in case an exception is thrown from handelLine. */ - static Handler *findHandlerForCommandAuthenticated(Protocol::Command::Type cmd); + static std::unique_ptr findHandlerForCommandAuthenticated(Protocol::Command::Type cmd); void setConnection(Connection *connection); Connection *connection() const; diff --git a/src/server/handler.cpp b/src/server/handler.cpp --- a/src/server/handler.cpp +++ b/src/server/handler.cpp @@ -56,144 +56,144 @@ using namespace Akonadi; using namespace Akonadi::Server; -Handler *Handler::findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd) +std::unique_ptr Handler::findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd) { // allowed are LOGIN if (cmd == Protocol::Command::Login) { - return new LoginHandler(); + return std::make_unique(); } - return nullptr; + return {}; } -Handler *Handler::findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd) +std::unique_ptr Handler::findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd) { // allowed is LOGOUT if (cmd == Protocol::Command::Logout) { - return new LogoutHandler(); + return std::make_unique(); } return nullptr; } -Handler *Handler::findHandlerForCommandAuthenticated(Protocol::Command::Type cmd) +std::unique_ptr Handler::findHandlerForCommandAuthenticated(Protocol::Command::Type cmd) { switch (cmd) { case Protocol::Command::Invalid: Q_ASSERT_X(cmd != Protocol::Command::Invalid, __FUNCTION__, "Invalid command is not allowed"); - return nullptr; + return {}; case Protocol::Command::Hello: Q_ASSERT_X(cmd != Protocol::Command::Hello, __FUNCTION__, "Hello command is not allowed in this context"); - return nullptr; + return {}; case Protocol::Command::Login: - return nullptr; + return {}; case Protocol::Command::Logout: - return nullptr; + return {}; case Protocol::Command::_ResponseBit: Q_ASSERT_X(cmd != Protocol::Command::_ResponseBit, __FUNCTION__, "ResponseBit is not a valid command type"); - return nullptr; + return {}; case Protocol::Command::Transaction: - return new TransactionHandler(); + return std::make_unique(); case Protocol::Command::CreateItem: - return new ItemCreateHandler(); + return std::make_unique(); case Protocol::Command::CopyItems: - return new ItemCopyHandler(); + return std::make_unique(); case Protocol::Command::DeleteItems: - return new ItemDeleteHandler(); + return std::make_unique(); case Protocol::Command::FetchItems: - return new ItemFetchHandler(); + return std::make_unique(); case Protocol::Command::LinkItems: - return new ItemLinkHandler(); + return std::make_unique(); case Protocol::Command::ModifyItems: - return new ItemModifyHandler(); + return std::make_unique(); case Protocol::Command::MoveItems: - return new ItemMoveHandler(); + return std::make_unique(); case Protocol::Command::CreateCollection: - return new CollectionCreateHandler(); + return std::make_unique(); case Protocol::Command::CopyCollection: - return new CollectionCopyHandler(); + return std::make_unique(); case Protocol::Command::DeleteCollection: - return new CollectionDeleteHandler(); + return std::make_unique(); case Protocol::Command::FetchCollections: - return new CollectionFetchHandler(); + return std::make_unique(); case Protocol::Command::FetchCollectionStats: - return new CollectionStatsFetchHandler(); + return std::make_unique(); case Protocol::Command::ModifyCollection: - return new CollectionModifyHandler(); + return std::make_unique(); case Protocol::Command::MoveCollection: - return new CollectionMoveHandler(); + return std::make_unique(); case Protocol::Command::Search: - return new SearchHandler(); + return std::make_unique(); case Protocol::Command::SearchResult: - return new SearchResultHandler(); + return std::make_unique(); case Protocol::Command::StoreSearch: - return new SearchCreateHandler(); + return std::make_unique(); case Protocol::Command::CreateTag: - return new TagCreateHandler(); + return std::make_unique(); case Protocol::Command::DeleteTag: - return new TagDeleteHandler(); + return std::make_unique(); case Protocol::Command::FetchTags: - return new TagFetchHandler(); + return std::make_unique(); case Protocol::Command::ModifyTag: - return new TagModifyHandler(); + return std::make_unique(); case Protocol::Command::FetchRelations: - return new RelationFetchHandler(); + return std::make_unique(); case Protocol::Command::ModifyRelation: - return new RelationModifyHandler(); + return std::make_unique(); case Protocol::Command::RemoveRelations: - return new RelationRemoveHandler(); + return std::make_unique(); case Protocol::Command::SelectResource: - return new ResourceSelectHandler(); + return std::make_unique(); case Protocol::Command::StreamPayload: Q_ASSERT_X(cmd != Protocol::Command::StreamPayload, __FUNCTION__, "StreamPayload command is not allowed in this context"); - return nullptr; + return {}; case Protocol::Command::ItemChangeNotification: Q_ASSERT_X(cmd != Protocol::Command::ItemChangeNotification, __FUNCTION__, "ItemChangeNotification command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::CollectionChangeNotification: Q_ASSERT_X(cmd != Protocol::Command::CollectionChangeNotification, __FUNCTION__, "CollectionChangeNotification command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::TagChangeNotification: Q_ASSERT_X(cmd != Protocol::Command::TagChangeNotification, __FUNCTION__, "TagChangeNotification command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::RelationChangeNotification: Q_ASSERT_X(cmd != Protocol::Command::RelationChangeNotification, __FUNCTION__, "RelationChangeNotification command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::SubscriptionChangeNotification: Q_ASSERT_X(cmd != Protocol::Command::SubscriptionChangeNotification, __FUNCTION__, "SubscriptionChangeNotification command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::DebugChangeNotification: Q_ASSERT_X(cmd != Protocol::Command::DebugChangeNotification, __FUNCTION__, "DebugChangeNotification command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::ModifySubscription: Q_ASSERT_X(cmd != Protocol::Command::ModifySubscription, __FUNCTION__, "ModifySubscription command is not allowed on this connection"); - return nullptr; + return {}; case Protocol::Command::CreateSubscription: Q_ASSERT_X(cmd != Protocol::Command::CreateSubscription, __FUNCTION__, "CreateSubscription command is not allowed on this connection"); - return nullptr; + return {}; } - return nullptr; + return {}; } void Handler::setTag(quint64 tag)