Advertisement
malixds_

Untitled

May 30th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include "Base.h"
  2. using namespace std;
  3. Base::Base(string name, Base* parent)
  4. {
  5.     this->name = name;
  6.     this->parent = parent;
  7.     if (parent)
  8.         parent->children.push_back(this);
  9.     return;
  10. }
  11. string Base::get_name(){
  12.     return name;
  13. }
  14. void Base::set_name(string& name){
  15.     this->name = name;
  16. }
  17. void Base::set_parent(Base* new_parent){
  18.     if (parent)
  19.         for (int i = 0; i < parent->children.size(); i++)
  20.             if (parent->children[i] == this)
  21.                 parent->children.erase(parent -> children.begin() + i);
  22.     parent = new_parent;
  23.     if (new_parent)
  24.         new_parent->children.push_back(this);
  25. }
  26. Base* Base::get_parent(){
  27.     return this->parent;
  28. }
  29. void Base::print(){
  30.     if (children.size())
  31.     {
  32.         cout << endl << get_name();
  33.         for (int i = 0; i < children.size(); i++)
  34.             cout << " " << children[i]->get_name();
  35.         for (int i = 0; i < children.size(); i++)
  36.             children[i]->print();
  37.     }
  38.     return;
  39. }
  40. void Base::print_new(string depth)
  41. {
  42.     cout << endl << depth << get_name();
  43.     for (auto child : children)
  44.         child->print_new(depth + "    ");
  45. }
  46. void Base::print_new_state(string depth)
  47. {
  48.     cout << endl << depth << get_name();
  49.     if (!state)
  50.         cout << " is not ready";
  51.     else
  52.         cout << " is ready";
  53.     for (Base* child : children)
  54.         child->print_new_state(depth + "    ");
  55. }
  56. Base* Base::find_object_by_name(string name)
  57. {
  58.     if (this->name == name)
  59.         return this;
  60.     else
  61.     {
  62.         for (Base* child : children)
  63.         {
  64.             Base* result = child->find_object_by_name(name);
  65.             if (result)
  66.                 return result;
  67.         }
  68.         return nullptr;
  69.     }
  70. }
  71. int Base::get_state()
  72. {
  73.     return this->state;
  74. }
  75. bool Base::set_state(int state)
  76. {
  77.     if (!state)
  78.         if ((parent && parent->get_state() == 0) || get_state() == 0)
  79.             return true;
  80.     else
  81.     {
  82.         for (Base* child : children)
  83.             child->set_state(0);
  84.         this->state = 0;
  85.         return true;
  86.     }
  87.     else
  88.         if (parent && parent->get_state() == 0)
  89.             return false;
  90.     else
  91.     {
  92.         this->state = state;
  93.         return true;
  94.     }
  95. }
  96. vector<Base*>Base::get_children(){
  97.     return this->children;
  98. }
  99. void Base::set_on(Base* root){
  100.     if (root == nullptr)
  101.         return;
  102.     root->set_state(1);
  103.     for (Base* child : root->get_children()){
  104.         if (child == nullptr)
  105.            
  106.             return;
  107.         else
  108.             set_on(child);
  109.     }
  110. }
  111. Base* Base::find_object_by_coord(Base* current_object, Base* root_object,string coord){
  112.     if (coord == "")
  113.         return nullptr;
  114.     if (coord == ".")
  115.         return current_object;
  116.     if (coord == "/")
  117.         return root_object;
  118.     if (coord[0] == '/' && coord[1] == '/')
  119.     {
  120.         string name = "";
  121.         for (int i = 2; i < coord.length(); ++i)
  122.             name += coord[i];
  123.         return root_object->find_object_by_name(name);
  124.     }
  125.     Base* now_object;
  126.     string name = "";
  127.     int i;
  128.     if (coord[0] == '/')
  129.     {
  130.         now_object = root_object;
  131.         i = 1;
  132.     }
  133.     else
  134.     {
  135.         now_object = current_object;
  136.         i = 0;
  137.     }
  138.     for (; i < coord.length(); ++i)
  139.     {
  140.         if (coord[i] == '/' || i == coord.length() - 1)
  141.         {
  142.             if (i == coord.length() - 1)
  143.                 name += coord[i];
  144.             if (name == ".")
  145.                 continue;
  146.             bool found = false;
  147.             for (Base* pointer : now_object->get_children())
  148.                 if (pointer->get_name() == name)
  149.                 {
  150.                     now_object = pointer;
  151.                     name = "";
  152.                     found = true;
  153.                 }
  154.             if (!found)
  155.                 return nullptr;
  156.         }
  157.         else
  158.             name += coord[i];
  159.     }
  160.     return now_object;
  161. }
  162. void Base::set_connect(TYPE_SIGNAL sign_n, Base *obj_n, TYPE_HANDLER hand_n){
  163.     for(int i = 0; i < connections.size(); i++)
  164.         if(connections[i].p_signal == sign_n && connections[i].p_Base == obj_n && connections[i].p_handler == hand_n)
  165.             return;
  166.     connections.push_back(o_sh(obj_n,sign_n,hand_n));
  167. }
  168. void Base::delete_connect(TYPE_SIGNAL sign_d, Base *obj_d, TYPE_HANDLER hand_d){
  169.     for(int i = 0; i < connections.size(); i++){
  170.         if(connections[i].p_signal == sign_d && connections[i].p_Base == obj_d&& connections[i].p_handler == hand_d){
  171.             connections.erase(connections.begin() + i);
  172.             return;
  173.         }
  174.     }
  175. }
  176. void Base::emit_signal(TYPE_SIGNAL sign_e, string request){
  177.     if(!state)
  178.         return;
  179.     (this->*sign_e)(request);
  180.     for(int i = 0; i < connections.size(); i++)
  181.         if(connections[i].p_signal == sign_e && connections[i].p_Base->state)
  182.             (connections[i].p_Base->*connections[i].p_handler)(request);
  183. }
  184. string Base::coord(string path){
  185.     if (parent)
  186.         return parent->coord("/"+get_name()+path);
  187.     else
  188.         path==""?path="/":path;
  189.     return path;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement