Fixed referencing and dereferencing errors in apparatus
This commit is contained in:
parent
d69b18f083
commit
f99974d8ae
12 changed files with 148 additions and 112 deletions
|
|
@ -4,27 +4,15 @@
|
|||
apparatus *apparatus::_instance = nullptr;
|
||||
const QString apparatus::filename = "data.bin";
|
||||
|
||||
void apparatus::open_stream() {
|
||||
this->_bin_file = new QFile(apparatus::filename);
|
||||
this->_bin_file->open(QIODevice::ReadWrite);
|
||||
|
||||
stream.setDevice(_bin_file);
|
||||
}
|
||||
|
||||
void apparatus::close_stream() {
|
||||
stream.setDevice(nullptr);
|
||||
|
||||
this->_bin_file->close();
|
||||
delete this->_bin_file;
|
||||
this->_bin_file = nullptr;
|
||||
}
|
||||
|
||||
apparatus::apparatus() {
|
||||
|
||||
this->_auth_system = new auth_system();
|
||||
this->_object_system = new object_system();
|
||||
}
|
||||
|
||||
apparatus::~apparatus() {
|
||||
|
||||
this->save();
|
||||
delete this->_auth_system;
|
||||
delete this->_object_system;
|
||||
}
|
||||
|
||||
void apparatus::generate_lock_file() {
|
||||
|
|
@ -42,52 +30,79 @@ apparatus* apparatus::instance() {
|
|||
return apparatus::_instance;
|
||||
}
|
||||
|
||||
void apparatus::save() {
|
||||
if (_instance == nullptr) {
|
||||
return;
|
||||
}
|
||||
QFile f(apparatus::filename);
|
||||
f.open(QIODevice::WriteOnly);
|
||||
QDataStream stream(&f);
|
||||
|
||||
// saving GIDs
|
||||
entity_id vgid = vessel_entity::GID();
|
||||
entity_id sgid = storage_entity::GID();
|
||||
stream << vgid << sgid;
|
||||
|
||||
// serializing data
|
||||
this->_auth_system->serialize_data(&stream);
|
||||
this->_object_system->serialize_data(&stream);
|
||||
|
||||
f.close();
|
||||
}
|
||||
|
||||
void apparatus::load() {
|
||||
if (_instance == nullptr) {
|
||||
throw std::runtime_error("HOW DU FUCK INSTANCE IS NULL????");
|
||||
}
|
||||
QFile f(apparatus::filename);
|
||||
f.open(QIODevice::ReadOnly);
|
||||
QDataStream stream(&f);
|
||||
|
||||
// loading GIDs
|
||||
entity_id vgid, sgid;
|
||||
stream >> vgid >> sgid;
|
||||
vessel_entity::preloadGlobalId(vgid);
|
||||
storage_entity::preloadGlobalId(sgid);
|
||||
|
||||
// deserializing data
|
||||
this->_auth_system->deserialize_data(&stream);
|
||||
this->_object_system->deserialize_data(&stream);
|
||||
|
||||
f.close();
|
||||
}
|
||||
|
||||
bool apparatus::isFirstRun() {
|
||||
return !QFile().exists("lock");
|
||||
}
|
||||
|
||||
auth_system& apparatus::get_auth_subsystem() {
|
||||
auth_system* apparatus::get_auth_subsystem() {
|
||||
return this->_auth_system;
|
||||
}
|
||||
|
||||
object_system& apparatus::get_object_subsystem() {
|
||||
object_system* apparatus::get_object_subsystem() {
|
||||
return this->_object_system;
|
||||
}
|
||||
|
||||
void apparatus::init() {
|
||||
if (apparatus::_instance != nullptr) {
|
||||
throw std::runtime_error("System already initialized!");
|
||||
}
|
||||
|
||||
bool fr = apparatus::isFirstRun();
|
||||
apparatus::_instance = new apparatus();
|
||||
|
||||
apparatus::_instance->open_stream();
|
||||
apparatus::_instance->loadGIDS();
|
||||
apparatus::_instance->deserialize_data();
|
||||
if (fr) {
|
||||
if (!QFile().exists(apparatus::filename)) {
|
||||
QFile init(apparatus::filename);
|
||||
init.open(QIODevice::ReadWrite);
|
||||
init.close();
|
||||
}
|
||||
apparatus::_instance->save();
|
||||
}
|
||||
|
||||
apparatus::_instance->load();
|
||||
}
|
||||
|
||||
void apparatus::shutdown() {
|
||||
apparatus::_instance->writeGIDS();
|
||||
apparatus::_instance->serialize_data();
|
||||
apparatus::_instance->close_stream();
|
||||
delete apparatus::_instance;
|
||||
}
|
||||
|
||||
void apparatus::writeGIDS() {
|
||||
entity_id vgid = vessel_entity::GID();
|
||||
entity_id sgid = storage_entity::GID();
|
||||
this->stream << vgid << sgid;
|
||||
}
|
||||
|
||||
void apparatus::loadGIDS() {
|
||||
entity_id vgid, sgid;
|
||||
this->stream >> vgid >> sgid;
|
||||
vessel_entity::preloadGlobalId(vgid);
|
||||
storage_entity::preloadGlobalId(sgid);
|
||||
}
|
||||
|
||||
void apparatus::serialize_data() {
|
||||
this->_auth_system.serialize_data(this->stream);
|
||||
this->_object_system.serialize_data(this->stream);
|
||||
}
|
||||
|
||||
void apparatus::deserialize_data() {
|
||||
this->_auth_system.deserialize_data(this->stream);
|
||||
this->_object_system.deserialize_data(this->stream);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,29 +18,18 @@ private:
|
|||
static apparatus *_instance;
|
||||
static const QString filename;
|
||||
|
||||
QFile *_bin_file;
|
||||
QDataStream stream;
|
||||
|
||||
auth_system _auth_system;
|
||||
object_system _object_system;
|
||||
|
||||
void open_stream();
|
||||
void close_stream();
|
||||
|
||||
void writeGIDS();
|
||||
void loadGIDS();
|
||||
auth_system* _auth_system;
|
||||
object_system* _object_system;
|
||||
|
||||
public:
|
||||
apparatus();
|
||||
explicit apparatus();
|
||||
~apparatus();
|
||||
|
||||
void generate_empty_data();
|
||||
auth_system& get_auth_subsystem();
|
||||
object_system& get_object_subsystem();
|
||||
auth_system* get_auth_subsystem();
|
||||
object_system* get_object_subsystem();
|
||||
|
||||
|
||||
void serialize_data();
|
||||
void deserialize_data();
|
||||
void save();
|
||||
void load();
|
||||
|
||||
static bool isFirstRun();
|
||||
static void generate_lock_file();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
#include "apparatus.h"
|
||||
#include "auth_system.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
const user_entity* auth_system::get_user(const QString &login, bool &success) {
|
||||
|
|
@ -32,29 +34,35 @@ bool auth_system::register_user(const QString &login, const QString &password, U
|
|||
bool exists = false;
|
||||
this->get_user(login, exists);
|
||||
if (!exists) {
|
||||
std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl;
|
||||
this->_users.push_back(user_entity(login, password, role));
|
||||
std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl;
|
||||
apparatus::instance()->save();
|
||||
std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const QVector<user_entity> auth_system::users() const {
|
||||
const QVector<user_entity>& auth_system::users() const {
|
||||
return this->_users;
|
||||
}
|
||||
|
||||
void auth_system::deserialize_data(QDataStream &stream) {
|
||||
void auth_system::deserialize_data(QDataStream *stream) {
|
||||
int icnt;
|
||||
stream >> icnt;
|
||||
this->_users.resize(icnt);
|
||||
for (int i = 0; i < icnt; i++) {
|
||||
this->_users[i].deserialize(stream);
|
||||
*stream >> icnt;
|
||||
if (icnt > 0) {
|
||||
this->_users.resize(icnt);
|
||||
for (int i = 0; i < icnt; i++) {
|
||||
this->_users[i].deserialize(*stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void auth_system::serialize_data(QDataStream &stream) {
|
||||
stream << this->_users.size();
|
||||
void auth_system::serialize_data(QDataStream *stream) {
|
||||
*stream << this->_users.size();
|
||||
for (auto &item : this->_users) {
|
||||
item.serialize(stream);
|
||||
item.serialize(*stream);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ public:
|
|||
bool remove_user(const QString &login);
|
||||
bool register_user(const QString &login, const QString &password, UserRole role);
|
||||
|
||||
const QVector<user_entity> users() const;
|
||||
const QVector<user_entity>& users() const;
|
||||
|
||||
void deserialize_data(QDataStream &stream);
|
||||
void serialize_data(QDataStream &stream);
|
||||
void deserialize_data(QDataStream *stream);
|
||||
void serialize_data(QDataStream *stream);
|
||||
};
|
||||
|
||||
#endif // AUTH_SYSTEM_H
|
||||
|
|
|
|||
|
|
@ -85,30 +85,34 @@ const QVector<vessel_entity> object_system::vessels() const {
|
|||
return this->_vessels;
|
||||
}
|
||||
|
||||
void object_system::deserialize_data(QDataStream &stream) {
|
||||
void object_system::deserialize_data(QDataStream *stream) {
|
||||
int dicnt;
|
||||
stream >> dicnt;
|
||||
this->_dpoints.resize(dicnt);
|
||||
for (int i = 0; i < dicnt; i++) {
|
||||
this->_dpoints[i].deserialize(stream);
|
||||
*stream >> dicnt;
|
||||
if (dicnt > 0) {
|
||||
this->_dpoints.resize(dicnt);
|
||||
for (int i = 0; i < dicnt; i++) {
|
||||
this->_dpoints[i].deserialize(*stream);
|
||||
}
|
||||
}
|
||||
|
||||
int vicnt;
|
||||
stream >> vicnt;
|
||||
this->_vessels.resize(vicnt);
|
||||
for (int i = 0; i < vicnt; i++) {
|
||||
this->_vessels[i].deserialize(stream);
|
||||
*stream >> vicnt;
|
||||
if (vicnt > 0) {
|
||||
this->_vessels.resize(vicnt);
|
||||
for (int i = 0; i < vicnt; i++) {
|
||||
this->_vessels[i].deserialize(*stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void object_system::serialize_data(QDataStream &stream) {
|
||||
stream << this->_dpoints.size();
|
||||
void object_system::serialize_data(QDataStream *stream) {
|
||||
*stream << this->_dpoints.size();
|
||||
for (auto &item : this->_dpoints) {
|
||||
item.serialize(stream);
|
||||
item.serialize(*stream);
|
||||
}
|
||||
|
||||
stream << this->_vessels.size();
|
||||
*stream << this->_vessels.size();
|
||||
for (auto &item : this->_vessels) {
|
||||
item.serialize(stream);
|
||||
item.serialize(*stream);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ public:
|
|||
const QVector<dpoint_entity> dpoints() const;
|
||||
const QVector<vessel_entity> vessels() const;
|
||||
|
||||
void deserialize_data(QDataStream &stream);
|
||||
void serialize_data(QDataStream &stream);
|
||||
void deserialize_data(QDataStream *stream);
|
||||
void serialize_data(QDataStream *stream);
|
||||
};
|
||||
|
||||
#endif // OBJECT_SYSTEM_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue