Mephistopheles_

Signals and handlers, main class (coursework)

Jun 28th, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1. h FILE:
  2.  
  3. #ifndef __Main
  4. #define __Main
  5. #include<vector>
  6. #include<string>
  7. #include<iostream>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<iomanip>
  11. #include<unordered_map>
  12. using namespace std;
  13. class node;
  14. typedef void (node::*TYPE_SIGNAL) (string&);
  15. typedef void (node::*TYPE_HANDLER) (string);
  16. #define signal(signal_f) ((TYPE_SIGNAL) (&signal_f))
  17. #define headler(head_f) ((TYPE_HANDLER) (&head_f))
  18. class node{
  19. protected:
  20.     int number=1;
  21.     int ready=1;
  22.     string name;
  23.     node* parent{};
  24.     vector<node*>children;
  25.     struct ost{
  26.         TYPE_SIGNAL si;
  27.         node* ptr;
  28.         TYPE_HANDLER ha;
  29.     };
  30.    vector<ost> connects;
  31. public:
  32.     explicit node(node* p=nullptr,string s="Node");
  33.     void set_name(string);
  34.     int get_num();
  35.     void set_con(TYPE_SIGNAL ,node* p,  TYPE_HANDLER);
  36.     void del_con (TYPE_SIGNAL ,node* p, TYPE_HANDLER);
  37.     void emit_sig(TYPE_SIGNAL , string& com);
  38.     string get_name() const;
  39.     void set_parent(node*);
  40.     node* get_parent()const;
  41.     void show() const;
  42.     virtual ~node();
  43.     node*  findpr(string);
  44.     void check(node* d,bool b);
  45.     void rec (node*, string)const;
  46.     node* bi(string);
  47. };
  48. #endif
  49.  
  50.  
  51. cpp FILE:
  52.  
  53. #include"node.h"
  54. node::node(node* p,string s){
  55.     set_parent(p);
  56.     set_name(move(s));
  57. }
  58. void node::set_name(string s){
  59.     name=move(s);
  60. }
  61. string node::get_name()const{
  62.     return name;
  63. }
  64. node* node::get_parent()const{
  65.     return parent;
  66. }
  67. void node:: set_parent(node* p){
  68.     if(p==parent) return;
  69.     if(parent){
  70.         auto it=find(parent->children.begin(),parent->children.end(),this);
  71.         parent->children.erase(it);
  72.     }
  73.     if(p){
  74.         p->children.push_back(this);
  75.     }
  76.     parent =p;
  77. }
  78. void node:: show()const{
  79.     cerr<<name<<'\n';
  80.     queue<const node*>q;
  81.     q.push(this);
  82.     while(!q.empty()){
  83.         auto root=q.front();
  84.         q.pop();
  85.         cerr<<root->name;
  86.         for(auto child: root->children){
  87.             cerr<<"  "<<child->name;
  88.             if(!child->children.empty())
  89.                 q.push(child);
  90.         }
  91.         if(!q.empty())cerr<<'\n';
  92.     }
  93. }
  94. node:: ~node(){
  95.     for(auto a:children)
  96.         delete a;
  97. }
  98. node* node::findpr(string s){
  99.     if(s==this->name)
  100.         return this;
  101.     queue<const node*>q;
  102.     q.push(this);
  103.     while(!q.empty()){
  104.         auto root=q.front();
  105.         q.pop();
  106.         for(auto child: root->children){
  107.             if(s==child->name)
  108.                 return child;
  109.             if(!child->children.empty())
  110.                 q.push(child);
  111.         }
  112.         if(q.empty())
  113.             return nullptr;
  114.     }
  115.  
  116. }
  117. int node::get_num(){
  118.     return this->number;
  119. }
  120. void node::set_con(TYPE_SIGNAL si ,node* p,  TYPE_HANDLER ha){
  121.     connects.push_back(ost{si,p,ha});
  122. }
  123. void node::del_con (TYPE_SIGNAL si ,node* p,  TYPE_HANDLER ha){
  124.     for(auto t= connects.begin();t!=connects.end();++t){
  125.         if(t->si==si && t->ptr==p && t->ha==ha){
  126.             connects.erase(t);
  127.             return;
  128.         }
  129.     }
  130. }
  131. void node::emit_sig(TYPE_SIGNAL si, string& com){
  132.     TYPE_HANDLER h;
  133.     node* object;
  134.     (this->*si)(com);
  135.     for(auto a:connects)
  136.         if(a.si==si) {
  137.             h=a.ha;
  138.             object=a.ptr;
  139.             (object->*h)(com);
  140.         }
  141. }
  142. node* node::bi(string parent){
  143.     node* p=this;
  144.     if(parent[0] =='/' && parent[1]=='/') {
  145.         parent.erase(parent.begin(),parent.begin()+2);
  146.         p = findpr(parent);
  147.         return p;
  148.     }
  149.     if(parent.find('/')==string::npos){
  150.         p=findpr(parent);
  151.         return p;
  152.     }
  153.     if(parent.size()>this->get_name().size()+1)
  154.         parent.erase(parent.begin(),parent.begin()+this->get_name().size()+2);
  155.     else
  156.         return this;
  157.     string u;
  158.     int pos=-1;
  159.     while(parent.size()>0){
  160.         bool b=0;
  161.         u="";
  162.         pos=-1;
  163.         for(int i=0;i<parent.size();++i){
  164.             if(parent[i]=='/'){
  165.                 pos=i;
  166.                 break;
  167.             }
  168.             u+=parent[i];
  169.         }
  170.         for(node* a:p->children){
  171.             if(a->get_name()==u){
  172.                 b=1;
  173.                 p=a;
  174.                 break;
  175.             }
  176.         }
  177.         if(!b)
  178.             return nullptr;
  179.         if(pos==-1)
  180.             break;
  181.         parent.erase(parent.begin(),parent.begin()+pos+1);
  182.     }
  183.     return p;
  184. }
  185. void node::check(node* d,bool b=1){
  186.     if(b)
  187.         cerr<<'\n';
  188.     if(d->ready>0)
  189.         cerr<<"The object "+d->get_name()+" is ready";
  190.     else
  191.         cerr<<"The object "+d->get_name()+" is not ready";
  192.     for(auto child: d->children)
  193.         check(child);
  194. }
  195. void node::rec(node* ptr, string s)const{
  196.     cerr<<'\n'<<s<<ptr->get_name();
  197.     for(auto child: ptr->children){
  198.         rec(child,s+"    ");
  199.     }
  200. }
  201.  
Add Comment
Please, Sign In to add comment