mirror of
https://github.com/logos-co/logos-cpp-sdk.git
synced 2026-08-31 01:31:10 +00:00
add overloards using cpp types to replace qt ones later (#51)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "logos_api_client.h"
|
||||
#include "logos_api_provider.h"
|
||||
#include "token_manager.h"
|
||||
#include <string>
|
||||
|
||||
LogosAPI::LogosAPI(const QString& module_name, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -19,6 +20,11 @@ LogosAPI::LogosAPI(const QString& module_name, QObject *parent)
|
||||
qRegisterMetaType<LogosResult>("LogosResult");
|
||||
}
|
||||
|
||||
LogosAPI::LogosAPI(const std::string& module_name, QObject *parent)
|
||||
: LogosAPI(QString::fromStdString(module_name), parent)
|
||||
{
|
||||
}
|
||||
|
||||
LogosAPI::~LogosAPI()
|
||||
{
|
||||
// Provider and client will be automatically deleted as child objects
|
||||
@@ -48,6 +54,11 @@ LogosAPIClient* LogosAPI::getClient(const QString& target_module) const
|
||||
return client;
|
||||
}
|
||||
|
||||
LogosAPIClient* LogosAPI::getClient(const std::string& target_module) const
|
||||
{
|
||||
return getClient(QString::fromStdString(target_module));
|
||||
}
|
||||
|
||||
TokenManager* LogosAPI::getTokenManager() const
|
||||
{
|
||||
return m_token_manager;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QHash>
|
||||
#include <string>
|
||||
#include "logos_types.h"
|
||||
|
||||
class LogosAPIClient;
|
||||
@@ -26,6 +27,17 @@ public:
|
||||
* @param parent Parent QObject
|
||||
*/
|
||||
explicit LogosAPI(const QString& module_name, QObject *parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Construct a new LogosAPI instance (const char* overload — resolves ambiguity)
|
||||
*/
|
||||
explicit LogosAPI(const char* module_name, QObject *parent = nullptr)
|
||||
: LogosAPI(QString(module_name), parent) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a new LogosAPI instance (std::string overload)
|
||||
*/
|
||||
explicit LogosAPI(const std::string& module_name, QObject *parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Destructor
|
||||
@@ -45,6 +57,17 @@ public:
|
||||
*/
|
||||
LogosAPIClient* getClient(const QString& target_module) const;
|
||||
|
||||
/**
|
||||
* @brief Get the client instance — const char* overload (resolves ambiguity)
|
||||
*/
|
||||
LogosAPIClient* getClient(const char* target_module) const
|
||||
{ return getClient(QString(target_module)); }
|
||||
|
||||
/**
|
||||
* @brief Get the client instance for communicating with a module (std::string overload)
|
||||
*/
|
||||
LogosAPIClient* getClient(const std::string& target_module) const;
|
||||
|
||||
/**
|
||||
* @brief Get the token manager instance
|
||||
* @return TokenManager* Pointer to the token manager
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "logos_object.h"
|
||||
#include "token_manager.h"
|
||||
#include <QMetaObject>
|
||||
#include <string>
|
||||
|
||||
LogosAPIClient::LogosAPIClient(const QString& module_to_talk_to, const QString& origin_module, TokenManager* token_manager, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -189,6 +190,13 @@ bool LogosAPIClient::informModuleToken(const QString& authToken, const QString&
|
||||
return m_consumer->informModuleToken(authToken, moduleName, token);
|
||||
}
|
||||
|
||||
bool LogosAPIClient::informModuleToken(const std::string& authToken, const std::string& moduleName, const std::string& token)
|
||||
{
|
||||
return informModuleToken(QString::fromStdString(authToken),
|
||||
QString::fromStdString(moduleName),
|
||||
QString::fromStdString(token));
|
||||
}
|
||||
|
||||
bool LogosAPIClient::informModuleToken_module(const QString& authToken, const QString& originModule, const QString& moduleName, const QString& token)
|
||||
{
|
||||
return m_consumer->informModuleToken_module(authToken, originModule, moduleName, token);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <QVariantList>
|
||||
#include <QMap>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "logos_mode.h"
|
||||
|
||||
@@ -112,6 +113,9 @@ public:
|
||||
void onEventResponse(QObject* object, const QString& eventName, const QVariantList& data);
|
||||
|
||||
bool informModuleToken(const QString& authToken, const QString& moduleName, const QString& token);
|
||||
bool informModuleToken(const char* authToken, const char* moduleName, const char* token)
|
||||
{ return informModuleToken(QString(authToken), QString(moduleName), QString(token)); }
|
||||
bool informModuleToken(const std::string& authToken, const std::string& moduleName, const std::string& token);
|
||||
bool informModuleToken_module(const QString& authToken, const QString& originModule, const QString& moduleName, const QString& token);
|
||||
|
||||
TokenManager* getTokenManager() const;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "logos_transport.h"
|
||||
#include "logos_transport_factory.h"
|
||||
#include <QDebug>
|
||||
#include <string>
|
||||
|
||||
LogosAPIProvider::LogosAPIProvider(const QString& module_name, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -63,6 +64,11 @@ bool LogosAPIProvider::registerObject(const QString& name, QObject* object)
|
||||
return publishProvider(name, m_qtProviderObject);
|
||||
}
|
||||
|
||||
bool LogosAPIProvider::registerObject(const std::string& name, QObject* object)
|
||||
{
|
||||
return registerObject(QString::fromStdString(name), object);
|
||||
}
|
||||
|
||||
// New path: LogosProviderObject* -> ModuleProxy -> transport
|
||||
bool LogosAPIProvider::registerObject(const QString& name, LogosProviderObject* provider)
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <QVariantList>
|
||||
#include <QMap>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class LogosTransportHost;
|
||||
class LogosObject;
|
||||
@@ -36,6 +37,17 @@ public:
|
||||
*/
|
||||
bool registerObject(const QString& name, QObject* object);
|
||||
|
||||
/**
|
||||
* @brief Register a legacy QObject-based plugin — const char* overload (resolves ambiguity)
|
||||
*/
|
||||
bool registerObject(const char* name, QObject* object)
|
||||
{ return registerObject(QString(name), object); }
|
||||
|
||||
/**
|
||||
* @brief Register a legacy QObject-based plugin (std::string overload).
|
||||
*/
|
||||
bool registerObject(const std::string& name, QObject* object);
|
||||
|
||||
/**
|
||||
* @brief Register a new-API LogosProviderObject plugin.
|
||||
* Wraps directly in ModuleProxy.
|
||||
|
||||
@@ -23,18 +23,33 @@ void TokenManager::saveToken(const QString& key, const QString& token)
|
||||
emit tokenSaved(key);
|
||||
}
|
||||
|
||||
void TokenManager::saveToken(const std::string& key, const std::string& token)
|
||||
{
|
||||
saveToken(QString::fromStdString(key), QString::fromStdString(token));
|
||||
}
|
||||
|
||||
QString TokenManager::getToken(const QString& key) const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
return m_tokens.value(key, QString());
|
||||
}
|
||||
|
||||
std::string TokenManager::getToken(const std::string& key) const
|
||||
{
|
||||
return getToken(QString::fromStdString(key)).toStdString();
|
||||
}
|
||||
|
||||
bool TokenManager::hasToken(const QString& key) const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
return m_tokens.contains(key);
|
||||
}
|
||||
|
||||
bool TokenManager::hasToken(const std::string& key) const
|
||||
{
|
||||
return hasToken(QString::fromStdString(key));
|
||||
}
|
||||
|
||||
bool TokenManager::removeToken(const QString& key)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
@@ -46,6 +61,11 @@ bool TokenManager::removeToken(const QString& key)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TokenManager::removeToken(const std::string& key)
|
||||
{
|
||||
return removeToken(QString::fromStdString(key));
|
||||
}
|
||||
|
||||
void TokenManager::clearAllTokens()
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QString>
|
||||
#include <QHash>
|
||||
#include <QMutex>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief TokenManager provides a singleton interface for managing authentication tokens
|
||||
@@ -30,6 +31,17 @@ public:
|
||||
*/
|
||||
void saveToken(const QString& key, const QString& token);
|
||||
|
||||
/**
|
||||
* @brief Save a token — const char* overload (resolves ambiguity, delegates to QString)
|
||||
*/
|
||||
void saveToken(const char* key, const char* token)
|
||||
{ saveToken(QString(key), QString(token)); }
|
||||
|
||||
/**
|
||||
* @brief Save a token with the given key (std::string overload)
|
||||
*/
|
||||
void saveToken(const std::string& key, const std::string& token);
|
||||
|
||||
/**
|
||||
* @brief Retrieve a token by key
|
||||
* @param key The identifier for the token
|
||||
@@ -37,6 +49,18 @@ public:
|
||||
*/
|
||||
QString getToken(const QString& key) const;
|
||||
|
||||
/**
|
||||
* @brief Retrieve a token — const char* overload (resolves ambiguity, delegates to QString)
|
||||
*/
|
||||
QString getToken(const char* key) const
|
||||
{ return getToken(QString(key)); }
|
||||
|
||||
/**
|
||||
* @brief Retrieve a token by key (std::string overload)
|
||||
* @return std::string The token value, or empty string if not found
|
||||
*/
|
||||
std::string getToken(const std::string& key) const;
|
||||
|
||||
/**
|
||||
* @brief Check if a token exists for the given key
|
||||
* @param key The identifier to check
|
||||
@@ -44,6 +68,17 @@ public:
|
||||
*/
|
||||
bool hasToken(const QString& key) const;
|
||||
|
||||
/**
|
||||
* @brief hasToken — const char* overload (resolves ambiguity, delegates to QString)
|
||||
*/
|
||||
bool hasToken(const char* key) const
|
||||
{ return hasToken(QString(key)); }
|
||||
|
||||
/**
|
||||
* @brief Check if a token exists for the given key (std::string overload)
|
||||
*/
|
||||
bool hasToken(const std::string& key) const;
|
||||
|
||||
/**
|
||||
* @brief Remove a token by key
|
||||
* @param key The identifier for the token to remove
|
||||
@@ -51,6 +86,17 @@ public:
|
||||
*/
|
||||
bool removeToken(const QString& key);
|
||||
|
||||
/**
|
||||
* @brief removeToken — const char* overload (resolves ambiguity, delegates to QString)
|
||||
*/
|
||||
bool removeToken(const char* key)
|
||||
{ return removeToken(QString(key)); }
|
||||
|
||||
/**
|
||||
* @brief Remove a token by key (std::string overload)
|
||||
*/
|
||||
bool removeToken(const std::string& key);
|
||||
|
||||
/**
|
||||
* @brief Clear all tokens
|
||||
*/
|
||||
|
||||
@@ -39,6 +39,7 @@ add_executable(sdk_tests
|
||||
test_event_system.cpp
|
||||
test_async_calls.cpp
|
||||
test_provider_dispatch.cpp
|
||||
test_std_string_overloads.cpp
|
||||
fixtures/sample_provider.cpp
|
||||
${GENERATED_DISPATCH}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <QtTest/QSignalSpy>
|
||||
#include "logos_mock.h"
|
||||
#include "token_manager.h"
|
||||
#include "logos_api.h"
|
||||
#include "logos_api_client.h"
|
||||
#include "logos_api_provider.h"
|
||||
#include "logos_provider_object.h"
|
||||
|
||||
// ============================================================
|
||||
// TokenManager std::string overloads
|
||||
// ============================================================
|
||||
|
||||
class TokenManagerStdStringTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
TokenManager::instance().clearAllTokens();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, SaveAndGetWithStdString)
|
||||
{
|
||||
TokenManager::instance().saveToken(std::string("mod_a"), std::string("token_a"));
|
||||
EXPECT_EQ(TokenManager::instance().getToken(std::string("mod_a")), "token_a");
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, GetMissingKeyReturnsEmpty)
|
||||
{
|
||||
EXPECT_TRUE(TokenManager::instance().getToken(std::string("missing")).empty());
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, HasTokenWithStdString)
|
||||
{
|
||||
EXPECT_FALSE(TokenManager::instance().hasToken(std::string("key")));
|
||||
TokenManager::instance().saveToken(std::string("key"), std::string("val"));
|
||||
EXPECT_TRUE(TokenManager::instance().hasToken(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, RemoveTokenWithStdString)
|
||||
{
|
||||
TokenManager::instance().saveToken(std::string("key"), std::string("val"));
|
||||
EXPECT_TRUE(TokenManager::instance().removeToken(std::string("key")));
|
||||
EXPECT_FALSE(TokenManager::instance().hasToken(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, RemoveNonexistentKeyReturnsFalse)
|
||||
{
|
||||
EXPECT_FALSE(TokenManager::instance().removeToken(std::string("nonexistent")));
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, CrossTypeInteropSaveStdGetQString)
|
||||
{
|
||||
TokenManager::instance().saveToken(std::string("key1"), std::string("val1"));
|
||||
EXPECT_EQ(TokenManager::instance().getToken(QString("key1")), QString("val1"));
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, CrossTypeInteropSaveQStringGetStd)
|
||||
{
|
||||
TokenManager::instance().saveToken(QString("key2"), QString("val2"));
|
||||
EXPECT_EQ(TokenManager::instance().getToken(std::string("key2")), "val2");
|
||||
}
|
||||
|
||||
TEST_F(TokenManagerStdStringTest, OverwriteWithStdString)
|
||||
{
|
||||
TokenManager::instance().saveToken(std::string("key"), std::string("old"));
|
||||
TokenManager::instance().saveToken(std::string("key"), std::string("new"));
|
||||
EXPECT_EQ(TokenManager::instance().getToken(std::string("key")), "new");
|
||||
EXPECT_EQ(TokenManager::instance().tokenCount(), 1);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// LogosAPI std::string overloads
|
||||
// ============================================================
|
||||
|
||||
class LogosApiStdStringTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
m_mock = new LogosMockSetup();
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete m_mock;
|
||||
}
|
||||
LogosMockSetup* m_mock = nullptr;
|
||||
};
|
||||
|
||||
TEST_F(LogosApiStdStringTest, ConstructWithStdStringCreatesProvider)
|
||||
{
|
||||
LogosAPI api(std::string("test_module"));
|
||||
EXPECT_NE(api.getProvider(), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(LogosApiStdStringTest, ConstructWithStdStringGetsTokenManager)
|
||||
{
|
||||
LogosAPI api(std::string("test_module"));
|
||||
EXPECT_EQ(api.getTokenManager(), &TokenManager::instance());
|
||||
}
|
||||
|
||||
TEST_F(LogosApiStdStringTest, GetClientWithStdStringReturnsNonNull)
|
||||
{
|
||||
LogosAPI api(std::string("origin"));
|
||||
LogosAPIClient* client = api.getClient(std::string("target_module"));
|
||||
EXPECT_NE(client, nullptr);
|
||||
}
|
||||
|
||||
TEST_F(LogosApiStdStringTest, GetClientWithStdStringCachesSameModule)
|
||||
{
|
||||
LogosAPI api(std::string("origin"));
|
||||
LogosAPIClient* c1 = api.getClient(std::string("target"));
|
||||
LogosAPIClient* c2 = api.getClient(std::string("target"));
|
||||
EXPECT_EQ(c1, c2);
|
||||
}
|
||||
|
||||
TEST_F(LogosApiStdStringTest, GetClientStdStringAndQStringSameModule)
|
||||
{
|
||||
// Both overloads should resolve to the same cached client
|
||||
LogosAPI api(std::string("origin"));
|
||||
LogosAPIClient* cStd = api.getClient(std::string("target"));
|
||||
LogosAPIClient* cQt = api.getClient(QString("target"));
|
||||
EXPECT_EQ(cStd, cQt);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// LogosAPIClient::informModuleToken std::string overload
|
||||
// ============================================================
|
||||
|
||||
class LogosApiClientStdStringTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
m_mock = new LogosMockSetup();
|
||||
m_api = new LogosAPI("origin");
|
||||
TokenManager::instance().saveToken(QString("capability_module"), QString("cap-token"));
|
||||
m_client = m_api->getClient("capability_module");
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete m_api;
|
||||
delete m_mock;
|
||||
}
|
||||
LogosMockSetup* m_mock = nullptr;
|
||||
LogosAPI* m_api = nullptr;
|
||||
LogosAPIClient* m_client = nullptr;
|
||||
};
|
||||
|
||||
TEST_F(LogosApiClientStdStringTest, InformModuleTokenStdStringCompilable)
|
||||
{
|
||||
// Verifies the std::string overload compiles and delegates without crashing.
|
||||
// Return value is transport-dependent (false in mock); we only confirm no crash.
|
||||
bool result = m_client->informModuleToken(
|
||||
std::string("cap-token"),
|
||||
std::string("some_module"),
|
||||
std::string("some-token-value")
|
||||
);
|
||||
(void)result;
|
||||
SUCCEED();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// LogosAPIProvider::registerObject std::string overload
|
||||
// ============================================================
|
||||
|
||||
// Minimal LogosProviderObject for testing the LogosProviderObject* registration path (with QString name)
|
||||
class StdStringTestProvider : public LogosProviderBase {
|
||||
public:
|
||||
QString providerName() const override { return "std_string_test"; }
|
||||
QString providerVersion() const override { return "1.0.0"; }
|
||||
QVariant callMethod(const QString&, const QVariantList&) override { return QVariant(); }
|
||||
QJsonArray getMethods() override { return QJsonArray(); }
|
||||
};
|
||||
|
||||
// Minimal QObject to exercise the QObject* overload with std::string name - kept for future use
|
||||
|
||||
|
||||
class LogosApiProviderStdStringTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
m_mock = new LogosMockSetup();
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete m_mock;
|
||||
}
|
||||
LogosMockSetup* m_mock = nullptr;
|
||||
};
|
||||
|
||||
TEST_F(LogosApiProviderStdStringTest, RegisterQObjectWithStdStringNullFails)
|
||||
{
|
||||
LogosAPI api(std::string("std_mod_null"));
|
||||
LogosAPIProvider* prov = api.getProvider();
|
||||
EXPECT_FALSE(prov->registerObject(std::string("std_mod_null"),
|
||||
static_cast<QObject*>(nullptr)));
|
||||
}
|
||||
|
||||
TEST_F(LogosApiProviderStdStringTest, RegisterQObjectWithStdStringSucceeds)
|
||||
{
|
||||
LogosAPI api(std::string("std_mod_qobj"));
|
||||
LogosAPIProvider* prov = api.getProvider();
|
||||
// Plain QObject* — the std::string name overload delegates to registerObject(QString, QObject*)
|
||||
QObject* obj = new QObject();
|
||||
EXPECT_TRUE(prov->registerObject(std::string("std_mod_qobj"), obj));
|
||||
}
|
||||
|
||||
TEST_F(LogosApiProviderStdStringTest, RegisterQObjectWithStdStringDoubleRegisterFails)
|
||||
{
|
||||
LogosAPI api(std::string("std_mod_dbl"));
|
||||
LogosAPIProvider* prov = api.getProvider();
|
||||
|
||||
QObject* obj1 = new QObject();
|
||||
QObject* obj2 = new QObject();
|
||||
EXPECT_TRUE(prov->registerObject(std::string("std_mod_dbl"), obj1));
|
||||
EXPECT_FALSE(prov->registerObject(std::string("std_mod_dbl"), obj2));
|
||||
delete obj2;
|
||||
}
|
||||
Reference in New Issue
Block a user