1
0

visit.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. int main()
  36. {
  37. std::vector<std::shared_ptr<card_list>> v;
  38. //std::thread th(run);
  39. for(int i=0;i<10000;i++)
  40. {
  41. card_list::instance()->add(i,std::make_shared<card>(i));//每次生成一个对象
  42. printf("%d %p\n",i,card_list::instance().get());
  43. v.push_back(card_list::instance());
  44. }
  45. for(int i=0;i<100;i++)
  46. {
  47. // printf("%d %p\n",i,v[i].get());
  48. }
  49. //th.join();
  50. visit_log vl;
  51. card_list::instance()->accept(vl);
  52. // printf("%p",card_list::instance()->get(5));
  53. auto x = card_list::instance()->get(100);
  54. if(x!=nullptr)
  55. printf("...........id:%d\n",x->id);
  56. else
  57. printf("nullptr...\n");
  58. return 0;
  59. }
  60. #endif