1
0

visit.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <thread>
  2. #include <chrono>
  3. #include "write-copy.h"
  4. #ifdef _TEST_
  5. struct card
  6. {
  7. int id;
  8. card(int _id):id(_id){}
  9. };
  10. struct card_list:single_base<card_list,int,std::shared_ptr<card>>
  11. {
  12. void accept(visitor<std::shared_ptr<card>>&v)
  13. {
  14. for(std::pair<int,std::shared_ptr<card>> me:m_map)
  15. {
  16. v.visit(me.second);
  17. printf("====\n");
  18. }
  19. }
  20. };
  21. struct visit_log:visitor<std::shared_ptr<card>>
  22. {
  23. bool visit(std::shared_ptr<card> c)
  24. {
  25. printf("vist :::::::%d\n",c->id);
  26. return true;
  27. }
  28. };
  29. template<> std::shared_ptr<card_list> single_base<card_list,int,std::shared_ptr<card>>::m_instance=std::make_shared<card_list>();
  30. void run()
  31. {
  32. visit_log vl;
  33. card_list::instance()->accept(vl);
  34. /*
  35. while(true)
  36. {
  37. printf("111111111111\n");
  38. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  39. }
  40. */
  41. }
  42. int main()
  43. {
  44. std::vector<std::shared_ptr<card_list>> v;
  45. //std::thread th(run);
  46. for(int i=0;i<10000;i++)
  47. {
  48. card_list::instance()->add(i,std::make_shared<card>(i));//每次生成一个对象
  49. printf("%d %p\n",i,card_list::instance().get());
  50. v.push_back(card_list::instance());
  51. }
  52. for(int i=0;i<100;i++)
  53. {
  54. // printf("%d %p\n",i,v[i].get());
  55. }
  56. //th.join();
  57. visit_log vl;
  58. card_list::instance()->accept(vl);
  59. // printf("%p",card_list::instance()->get(5));
  60. auto x = card_list::instance()->get(100);
  61. if(x!=nullptr)
  62. printf("...........id:%d\n",x->id);
  63. else
  64. printf("nullptr...\n");
  65. return 0;
  66. }
  67. #endif