Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Base.h"
- using namespace std;
- Base::Base(string name, Base* parent)
- {
- this->name = name;
- this->parent = parent;
- if (parent)
- parent->children.push_back(this);
- return;
- }
- string Base::get_name(){
- return name;
- }
- void Base::set_name(string& name){
- this->name = name;
- }
- void Base::set_parent(Base* new_parent){
- if (parent)
- for (int i = 0; i < parent->children.size(); i++)
- if (parent->children[i] == this)
- parent->children.erase(parent -> children.begin() + i);
- parent = new_parent;
- if (new_parent)
- new_parent->children.push_back(this);
- }
- Base* Base::get_parent(){
- return this->parent;
- }
- void Base::print(){
- if (children.size())
- {
- cout << endl << get_name();
- for (int i = 0; i < children.size(); i++)
- cout << " " << children[i]->get_name();
- for (int i = 0; i < children.size(); i++)
- children[i]->print();
- }
- return;
- }
- void Base::print_new(string depth)
- {
- cout << endl << depth << get_name();
- for (auto child : children)
- child->print_new(depth + " ");
- }
- void Base::print_new_state(string depth)
- {
- cout << endl << depth << get_name();
- if (!state)
- cout << " is not ready";
- else
- cout << " is ready";
- for (Base* child : children)
- child->print_new_state(depth + " ");
- }
- Base* Base::find_object_by_name(string name)
- {
- if (this->name == name)
- return this;
- else
- {
- for (Base* child : children)
- {
- Base* result = child->find_object_by_name(name);
- if (result)
- return result;
- }
- return nullptr;
- }
- }
- int Base::get_state()
- {
- return this->state;
- }
- bool Base::set_state(int state)
- {
- if (!state)
- if ((parent && parent->get_state() == 0) || get_state() == 0)
- return true;
- else
- {
- for (Base* child : children)
- child->set_state(0);
- this->state = 0;
- return true;
- }
- else
- if (parent && parent->get_state() == 0)
- return false;
- else
- {
- this->state = state;
- return true;
- }
- }
- vector<Base*>Base::get_children(){
- return this->children;
- }
- void Base::set_on(Base* root){
- if (root == nullptr)
- return;
- root->set_state(1);
- for (Base* child : root->get_children()){
- if (child == nullptr)
- return;
- else
- set_on(child);
- }
- }
- Base* Base::find_object_by_coord(Base* current_object, Base* root_object,string coord){
- if (coord == "")
- return nullptr;
- if (coord == ".")
- return current_object;
- if (coord == "/")
- return root_object;
- if (coord[0] == '/' && coord[1] == '/')
- {
- string name = "";
- for (int i = 2; i < coord.length(); ++i)
- name += coord[i];
- return root_object->find_object_by_name(name);
- }
- Base* now_object;
- string name = "";
- int i;
- if (coord[0] == '/')
- {
- now_object = root_object;
- i = 1;
- }
- else
- {
- now_object = current_object;
- i = 0;
- }
- for (; i < coord.length(); ++i)
- {
- if (coord[i] == '/' || i == coord.length() - 1)
- {
- if (i == coord.length() - 1)
- name += coord[i];
- if (name == ".")
- continue;
- bool found = false;
- for (Base* pointer : now_object->get_children())
- if (pointer->get_name() == name)
- {
- now_object = pointer;
- name = "";
- found = true;
- }
- if (!found)
- return nullptr;
- }
- else
- name += coord[i];
- }
- return now_object;
- }
- void Base::set_connect(TYPE_SIGNAL sign_n, Base *obj_n, TYPE_HANDLER hand_n){
- for(int i = 0; i < connections.size(); i++)
- if(connections[i].p_signal == sign_n && connections[i].p_Base == obj_n && connections[i].p_handler == hand_n)
- return;
- connections.push_back(o_sh(obj_n,sign_n,hand_n));
- }
- void Base::delete_connect(TYPE_SIGNAL sign_d, Base *obj_d, TYPE_HANDLER hand_d){
- for(int i = 0; i < connections.size(); i++){
- if(connections[i].p_signal == sign_d && connections[i].p_Base == obj_d&& connections[i].p_handler == hand_d){
- connections.erase(connections.begin() + i);
- return;
- }
- }
- }
- void Base::emit_signal(TYPE_SIGNAL sign_e, string request){
- if(!state)
- return;
- (this->*sign_e)(request);
- for(int i = 0; i < connections.size(); i++)
- if(connections[i].p_signal == sign_e && connections[i].p_Base->state)
- (connections[i].p_Base->*connections[i].p_handler)(request);
- }
- string Base::coord(string path){
- if (parent)
- return parent->coord("/"+get_name()+path);
- else
- path==""?path="/":path;
- return path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement