cl.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <iostream>
  2. #include <cl.h>
  3. cl_base::cl_base(cl_base* m){
  4. name = m->getName();
  5. master = m->getMaster();
  6. ready = m->getReady();
  7. }
  8. cl_base* cl_base::getMaster(){
  9. return master;
  10. }
  11. string cl_base::getName(){
  12. return name;
  13. }
  14. int cl_base::getReady(){
  15. return ready;
  16. }
  17. void cl_base::add_slave(cl_base* new_ob){
  18. slaves.push_back(new_ob);
  19. }
  20. void cl_base::showTree(){
  21. coutIsReady();
  22. for (it_slave = slaves.begin();it_slave!=slaves.end();++it_slave)
  23. (*it_slave)->showTree();
  24. }
  25. void cl_base::coutIsReady(){
  26. if (master!=nullptr)
  27. cout<<endl;
  28. if (ready>0)
  29. cout<<"The object "<<name<<" is ready";
  30. else
  31. cout<<"The object "<<name<<" is not ready";
  32. }
  33. cl_base* cl_base::get_slave(string n){
  34. if (name == n)
  35. return this;
  36. for (it_slave = slaves.begin();it_slave!=slaves.end();++it_slave)
  37. if ((* it_slave)->getName() == n){
  38. return (* it_slave);
  39. }
  40. for (it_slave = slaves.begin();it_slave!=slaves.end();++it_slave){
  41. cl_base* tmp = (* it_slave)->get_slave(n);
  42. if (tmp!=nullptr)
  43. return tmp;
  44. }
  45. return nullptr;
  46. }
  47. void cl_base::delete_children(){
  48. for (it_slave = slaves.begin();it_slave!=slaves.end();++it_slave)
  49. delete (*it_slave);
  50. }
  51. void cl_base::kill_slave(string n){
  52. for (it_slave = slaves.begin();it_slave!=slaves.end();++it_slave)
  53. if ((* it_slave)->getName() == n){
  54. (*it_slave)->delete_children();
  55. delete (*it_slave);
  56. }
  57. }
  58. void cl_application::add_slave(string master,cl_base* cl){
  59. root->get_slave(master)->add_slave(cl);
  60. }
  61. void cl_application::build_tree(){
  62. string master;
  63. cin>>master;
  64. root = new cl_base(nullptr, master, 1);
  65. cin>>master;
  66. while(master!="endtree"){
  67. string slave;cin>>slave;
  68. int cl,ready;cin>>cl>>ready;
  69. if (cl == 2)
  70. add_slave(master, new cl_base2(root->get_slave(master), slave, ready));
  71. if (cl == 3)
  72. add_slave(master, new cl_base3(root->get_slave(master), slave, ready));
  73. if (cl == 4)
  74. add_slave(master, new cl_base4(root->get_slave(master), slave, ready));
  75. cin>>master;
  76. }
  77. }
  78. void cl_application::show_tree(){
  79. root->showTree();
  80. }