#include "base.h"
base::base(base* inst, std::string name, int status) {
this->name = name;
this->status = status;
if (inst != nullptr) {
this->parent = inst;
inst->children.push_back(this);
}
}
base::base(base* inst, std::string name) {
this->name = name;
if (inst != nullptr) {
this->parent = inst;
inst->children.push_back(this);
}
}
base::base(base* inst) {
if (inst != nullptr) {
this->parent = inst;
inst->children.push_back(this);
}
}
base::base(std::string name) {
this->name = name;
}
base::~base() {
delete parent;
}
std::string base::getName() {
return name;
}
void base::setName(std::string name) {
this->name = name;
}
int base::getStatus() {
return status;
}
void base::setStatus(int status) {
this->status = status;
}
bool base::isReady() {
return status > 0;
}
void base::setParent(base* inst) {
this->parent = inst;
inst->children.push_back(this);
}
base* base::getChild(std::string name) {
for (int i = 0; i < children.size(); i++)
{
if (children[i]->getName() == name) {
return children[i];
}
}
}
bool base::hasChildren() {
return children.size() > 0;
}
void base::printTree(std::string prefix) {
if (hasChildren())
{
int spaces = 4;
base* checker = this->parent;
while (checker != nullptr)
{
checker = checker->parent;
spaces += 4;
}
for (int i = 0; i < children.size(); i++)
{
std::cout << std::endl << std::setw(spaces + int(children[i]->name.size())) << children[i]->name;
if (children[i]->hasChildren())
children[i]->printTree();
}
}
}
void base::printStatus() {
if (isReady()) {
std::cout << "\nThe object " << name << " is ready";
}
else {
std::cout << "\nThe object " << name << " is not ready";
}
if (hasChildren()) {
for (int i = 0; i < children.size(); i++) {
children[i]->printStatus();
}
}
}
base* base::getObjectWithName(std::string name) {
if (getName() == name) return this;
for (int i = 0; i < children.size(); i++) {
base* ret = children[i]->getObjectWithName(name);
if (ret != nullptr) {
return ret;
}
}
return nullptr;
}
base* base::getObjectWithCoord(std::string name) {
std::string buffer = "";
for (int i = 1; i < name.size(); ++i) {
if (name[i] != '/') {
buffer += name[i];
continue;
}
break;
}
if (buffer != this->getName())
return nullptr;
if (name == ('/' + buffer))
return this;
name.erase(0, buffer.size() + 1);
for (auto x : children) {
if (x->getObjectWithCoord(name) != nullptr)
return x->getObjectWithCoord(name);
}
return nullptr;
}
void base::setConnect(TYPE_SIGNAL p_signal, base* p_object, TYPE_HANDLER p_ob_handler) {
o_sh* p_value;
for (unsigned int i = 0; i < connects.size(); i++)
{
if (connects[i]->p_signal == p_signal &&
connects[i]->p_cl_base == p_object &&
connects[i]->p_handler == p_ob_handler)
{
return;
}
}
p_value = new o_sh();
p_value->p_signal = p_signal;
p_value->p_cl_base = p_object;
p_value->p_handler = p_ob_handler;
connects.push_back(p_value);
}
void base::deleteConnect(TYPE_SIGNAL p_signal, base* p_object, TYPE_HANDLER p_ob_handler)
{
for (unsigned int i = 0; i < connects.size(); i++)
{
if (connects[i]->p_signal == p_signal &&
connects[i]->p_cl_base == p_object &&
connects[i]->p_handler == p_ob_handler)
{
connects.erase(connects.begin() + i);
return;
}
}
}
void base::emitSignal(TYPE_SIGNAL p_signal, std::string& s_command)
{
TYPE_HANDLER p_handler;
base* p_object;
(this->*p_signal) (s_command);
for (unsigned int i = 0; i < connects.size(); i++)
{
if (connects[i]->p_signal == p_signal)
{
p_handler = connects[i]->p_handler;
p_object = connects[i]->p_cl_base;
(p_object->*p_handler) (s_command);
}
}
}
void base::addMessage(std::string message) {
signalMessage = message;
}
void base::addConnection(int idx, std::string objectSender, std::string objectAccepter) {
std::string index = std::to_string(idx);
std::string connection = index + " " + objectSender + " " + objectAccepter;
connections.push_back(connection);
}
void base::addSignal(base* objectSender) {
signals.push_back(objectSender);
}
void base::signal(std::string& s) {
s = " Text: " + this->getName() + " -> " + s;
}
void base::handler(std::string s)
{
std::cout << std::endl << "Signal to " << this->getName() << s;
}
std::vector <std::string> base::getConnect() {
return connections;
}
std::vector <base*> base::getSign() {
return signals;
}
std::string base::getMessage() {
return signalMessage;
}