update// changed datastore to factory pattern to get only the desired datastores

This commit is contained in:
maxisman
2020-05-08 19:39:16 +02:00
parent d599bf9190
commit 2e8b46c863
5 changed files with 199 additions and 51 deletions

View File

@@ -0,0 +1,64 @@
#ifndef DATASTORE_H
#define DATASTORE_H
using json = nlohmann::json;
class ChatDataStore
{
private:
static bool instanced;
static ChatDataStore* instance;
std::map<QString, ChatItem> data;
ChatDataStore()
{
}
public:
static ChatDataStore<T>* getInstance()
{
if(!ChatDataStore<T>::instanced)
{
ChatDataStore<T>::instanced = true;
ChatDataStore<T>::instance = new ChatDataStore();
}
return ChatDataStore<ChatItem>::instance;
}
void clear();
void setData(QString key, ChatItem value);
ChatItem getData(QString key);
QString dump();
~ChatDataStore()
{
ChatDataStore<T>::instanced = false;
}
};
void ChatDataStore::clear()
{
this->data.clear();
}
void ChatDataStore:setData(QString key, ChatItem value)
{
this->data[key] = value;
}
ChatItem ChatDataStore::getData(QString key)
{
return this->data[key];
}
QString ChatDataStore::dump()
{
return "";
}
ChatDataStore* ChatDataStore::instance = nullptr;
bool ChatDataStore::instanced = false;
#endif

View File

@@ -0,0 +1,58 @@
#ifndef DATASTORE_H
#define DATASTORE_H
#include <QString>
#include <map>
template <class T>
class DataStore
{
private:
static bool instanced;
static DataStore<T>* instance;
std::map<QString, T> data;
DataStore()
{
}
public:
static DataStore<T>* getInstance()
{
if(!DataStore<T>::instanced)
{
DataStore<T>::instanced = true;
DataStore<T>::instance = new DataStore<T>();
}
return DataStore<T>::instance;
}
void clear();
void setData(QString key, T value);
QString getData(QString key);
~DataStore()
{
DataStore<T>::instanced = false;
}
};
template <class T>
void DataStore<T>::clear()
{
this->data.clear();
}
template <class T>
void DataStore<T>::setData(QString key, T value)
{
this->data[key] = value;
}
template <class T>
QString DataStore<T>::getData(QString key)
{
return this->data[key];
}
#endif

View File

@@ -1,58 +1,21 @@
#ifndef DATASTORE_H
#define DATASTORE_H
#include <QString>
#include <map>
#include "SietchDataStore.h"
#include "ChatDataStore.h"
template <class T>
class DataStore
{
private:
static bool instanced;
static DataStore<T>* instance;
std::map<QString, T> data;
DataStore()
{
public:
static SietchDataStore* getSietchDataStore()
{
return SietchDataStore::getInstance();
}
}
public:
static DataStore<T>* getInstance()
{
if(!DataStore<T>::instanced)
{
DataStore<T>::instanced = true;
DataStore<T>::instance = new DataStore<T>();
}
return DataStore<T>::instance;
}
void clear();
void setData(QString key, T value);
QString getData(QString key);
~DataStore()
{
DataStore<T>::instanced = false;
}
};
template <class T>
void DataStore<T>::clear()
{
this->data.clear();
static ChatDataStore* getChatDataStore()
{
return ChatDataStore::getInstance();
}
}
template <class T>
void DataStore<T>::setData(QString key, T value)
{
this->data[key] = value;
}
template <class T>
QString DataStore<T>::getData(QString key)
{
return this->data[key];
}
#endif

View File

@@ -0,0 +1,63 @@
#ifndef SietchDataStore_H
#define SietchDataStore_H
using json = nlohmann::json;
class SietchDataStore
{
private:
static bool instanced;
static SietchDataStore* instance;
std::map<QString, QString> data;
SietchDataStore()
{
}
public:
static SietchDataStore* getInstance()
{
if(!SietchDataStore::instanced)
{
SietchDataStore::instanced = true;
SietchDataStore::instance = new SietchDataStore();
}
return SietchDataStore::instance;
}
void clear();
void setData(QString key, QString value);
QString getData(QString key);
QString dump();
~ChatDataStore()
{
ChatDataStore::instanced = false;
}
};
void SietchDataStore::clear()
{
this->data.clear();
}
void SietchDataStore::setData(QString key, QString value)
{
this->data[key] = value;
}
QString SietchDataStore::getData(QString key)
{
return this->data[key];
}
QString ChatDataStore::dump()
{
return "";
}
ChatDataStore* ChatDataStore::instance = nullptr;
bool ChatDataStore::instanced = false;
#endif

View File

@@ -97,7 +97,7 @@ void Controller::setConnection(Connection* c)
{
zrpc->createNewSietchZaddr( [=] (json reply) {
QString zdust = QString::fromStdString(reply.get<json::array_t>()[0]);
DataStore<QString>::getInstance()->setData("Sietch" + QString(i), zdust.toUtf8());
DataStore::getSietchDataStore()->setData("Sietch" + QString(i), zdust.toUtf8());
});
}
}
@@ -120,7 +120,7 @@ void Controller::fillTxJsonParams(json& allRecepients, Tx tx)
{
zrpc->createNewSietchZaddr( [=] (json reply) {
QString zdust = QString::fromStdString(reply.get<json::array_t>()[0]);
DataStore<QString>::getInstance()->setData(QString("Sietch") + QString(i), zdust.toUtf8());
DataStore::getSietchDataStore()->setData(QString("Sietch") + QString(i), zdust.toUtf8());
} );
}
@@ -128,7 +128,7 @@ void Controller::fillTxJsonParams(json& allRecepients, Tx tx)
// Using DataStore singelton, to store the data into the dusts, bing bada boom :D
for(uint8_t i = 0; i < 10; i++)
{
dust.at(i)["address"] = DataStore<QString>::getInstance()->getData(QString("Sietch" + QString(i))).toStdString();
dust.at(i)["address"] = DataStore::getSietchDataStore()->getData(QString("Sietch" + QString(i))).toStdString();
}
DataStore<QString>::getInstance()->clear(); // clears the datastore