Move project in sub

This commit is contained in:
Andrew nuark G 2020-12-18 17:40:22 +07:00
parent f868166756
commit ec1af1b8e5
43 changed files with 358 additions and 2 deletions

View file

@ -0,0 +1,106 @@
#include "apparatus.h"
apparatus *apparatus::_instance = nullptr;
const QString apparatus::filename = "data.bin";
void apparatus::open_reading_stream() {
this->_bin_file = new QFile(apparatus::filename);
this->_bin_file->open(QIODevice::ReadOnly);
stream.setDevice(_bin_file);
}
void apparatus::open_writing_stream() {
this->_bin_file = new QFile(apparatus::filename);
this->_bin_file->open(QIODevice::WriteOnly);
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->open_reading_stream();
this->loadGIDS();
this->deserialize_data();
this->close_stream();
}
apparatus::~apparatus() {
if (this->_bin_file) {
this->_bin_file->flush();
this->_bin_file->close();
delete this->_bin_file;
this->_bin_file = nullptr;
}
}
apparatus& apparatus::instance() {
if (apparatus::_instance == nullptr) {
apparatus::init();
}
return *apparatus::_instance;
}
bool apparatus::isFirstRun() {
return QFile(apparatus::filename).exists();
}
void apparatus::generate_empty_data() {
this->open_writing_stream();
this->writeGIDS();
this->serialize_data();
this->close_stream();
}
const auth_system& apparatus::get_auth_subsystem() {
return this->_auth_system;
}
const object_system& apparatus::get_object_subsystem() {
return this->_object_system;
}
void apparatus::init() {
apparatus::_instance = new apparatus();
}
void apparatus::shutdown() {
if (apparatus::_instance != nullptr) {
apparatus::_instance->open_writing_stream();
apparatus::_instance->writeGIDS();
apparatus::_instance->serialize_data();
apparatus::_instance->close_stream();
}
}
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.init(this->stream);
this->_object_system.init(this->stream);
}
void apparatus::deserialize_data() {
this->_auth_system.shutdown(this->stream);
this->_object_system.shutdown(this->stream);
}

View file

@ -0,0 +1,51 @@
#ifndef APPARATUS_H
#define APPARATUS_H
#include "auth_system.h"
#include "object_system.h"
#include <QString>
#include <QFile>
#include <QDataStream>
#include <entities/vessel_entity.h>
#include <entities/storage_entity.h>
class apparatus
{
private:
static apparatus *_instance;
static const QString filename;
QFile *_bin_file;
QDataStream stream;
auth_system _auth_system;
object_system _object_system;
void open_reading_stream();
void open_writing_stream();
void close_stream();
void writeGIDS();
void loadGIDS();
void serialize_data();
void deserialize_data();
public:
apparatus();
~apparatus();
bool isFirstRun();
void generate_empty_data();
const auth_system& get_auth_subsystem();
const object_system& get_object_subsystem();
static apparatus& instance();
static void init();
static void shutdown();
};
#endif // APPARATUS_H

View file

@ -0,0 +1,56 @@
#include "auth_system.h"
const user_entity* auth_system::get_user(const QString &login, bool &success) {
user_entity *out = nullptr;
success = false;
for (user_entity &item : this->_users) {
if (item.login() == login) {
success = true;
out = &item;
break;
}
}
return out;
}
bool auth_system::remove_user(const QString &login) {
auto vit = this->_users.begin();
for (; vit != this->_users.end(); vit++) {
if ((*vit).login() == login) {
this->_users.erase(vit);
return true;
}
}
return false;
}
bool auth_system::register_user(const QString &login, const QString &password, UserRole role) {
bool exists = false;
this->get_user(login, exists);
if (!exists) {
this->_users.push_back(user_entity(login, password, role));
return true;
}
return false;
}
void auth_system::init(QDataStream &stream) {
int icnt;
stream >> icnt;
this->_users.resize(icnt);
for (int i = 0; i < icnt; i++) {
this->_users[i].deserialize(stream);
}
}
void auth_system::shutdown(QDataStream &stream) {
stream << this->_users.size();
for (auto &item : this->_users) {
item.serialize(stream);
}
}

View file

@ -0,0 +1,24 @@
#ifndef AUTH_SYSTEM_H
#define AUTH_SYSTEM_H
#include<QVector>
#include <entities/user_entity.h>
class auth_system
{
private:
QVector<user_entity> _users;
public:
auth_system() = default;
const user_entity* get_user(const QString &login, bool &success);
bool remove_user(const QString &login);
bool register_user(const QString &login, const QString &password, UserRole role);
void init(QDataStream &stream);
void shutdown(QDataStream &stream);
};
#endif // AUTH_SYSTEM_H

View file

@ -0,0 +1,106 @@
#include "object_system.h"
const dpoint_entity* object_system::get_dpoint(entity_id oid, bool &success) {
dpoint_entity *out = nullptr;
success = false;
for (dpoint_entity &item : this->_dpoints) {
if (item.id() == oid) {
success = true;
out = &item;
break;
}
}
return out;
}
bool object_system::remove_dpoint(entity_id oid) {
auto vit = this->_dpoints.begin();
for (; vit != this->_dpoints.end(); vit++) {
if ((*vit).id() == oid) {
this->_dpoints.erase(vit);
return true;
}
}
return false;
}
bool object_system::add_dpoint(dpoint_entity dpoint) {
bool exists = false;
this->get_dpoint(dpoint.id(), exists);
if (!exists) {
this->_dpoints.push_back(dpoint);
return true;
}
return false;
}
const vessel_entity* object_system::get_vessel(entity_id oid, bool &success) {
vessel_entity *out = nullptr;
success = false;
for (vessel_entity &item : this->_vessels) {
if (item.id() == oid) {
success = true;
out = &item;
break;
}
}
return out;
}
bool object_system::remove_vessel(entity_id oid) {
auto vit = this->_vessels.begin();
for (; vit != this->_vessels.end(); vit++) {
if ((*vit).id() == oid) {
this->_vessels.erase(vit);
return true;
}
}
return false;
}
bool object_system::add_vessel(vessel_entity dpoint) {
bool exists = false;
this->get_dpoint(dpoint.id(), exists);
if (!exists) {
this->_vessels.push_back(dpoint);
return true;
}
return false;
}
void object_system::init(QDataStream &stream) {
int dicnt;
stream >> dicnt;
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);
}
}
void object_system::shutdown(QDataStream &stream) {
stream << this->_dpoints.size();
for (auto &item : this->_dpoints) {
item.serialize(stream);
}
stream << this->_vessels.size();
for (auto &item : this->_vessels) {
item.serialize(stream);
}
}

View file

@ -0,0 +1,32 @@
#ifndef OBJECT_SYSTEM_H
#define OBJECT_SYSTEM_H
#include <QVector>
#include <entities/dpoint_entity.h>
#include <entities/vessel_entity.h>
class object_system
{
private:
QVector<dpoint_entity> _dpoints;
QVector<vessel_entity> _vessels;
public:
object_system() = default;
const dpoint_entity* get_dpoint(entity_id oid, bool &success);
bool remove_dpoint(entity_id oid);
bool add_dpoint(dpoint_entity dpoint);
const vessel_entity* get_vessel(entity_id oid, bool &success);
bool remove_vessel(entity_id oid);
bool add_vessel(vessel_entity dpoint);
void init(QDataStream &stream);
void shutdown(QDataStream &stream);
};
#endif // OBJECT_SYSTEM_H