object_pool.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include<__vic/defs.h>
  2. #include<__vic/object_pool.h>
  3. #include<iostream>
  4. #include<exception>
  5. #include<string>
  6. #include<cassert>
  7. namespace tests {
  8. class C : private __vic::non_copyable
  9. {
  10. std::string name_;
  11. public:
  12. explicit C(const std::string &name) : name_(name) {}
  13. std::string name() const { return name_; }
  14. };
  15. void run()
  16. {
  17. __vic::object_pool<C> pool(2);
  18. std::cout << "Pool size: " << pool.size() << '\n';
  19. new(pool.alloc()) C("C1");
  20. assert(pool.size() == 0);
  21. pool.push();
  22. assert(pool.size() == 1);
  23. #if __cpp_rvalue_references && __cpp_variadic_templates
  24. pool.emplace("C2");
  25. #else
  26. new(pool.alloc()) C("C2");
  27. pool.push();
  28. #endif
  29. assert(pool.size() == 2);
  30. std::cout << "Pool size: " << pool.size() << '\n';
  31. std::cout << "Pool access by index:\n";
  32. for(unsigned i = 0; i < pool.size(); i++)
  33. std::cout << pool[i].name() << ' ';
  34. std::cout << '\n';
  35. std::cout << "Pool access by iterator:\n";
  36. for(__vic::object_pool<C>::const_iterator it =
  37. pool.cbegin(); it != pool.cend(); ++it)
  38. std::cout << it->name() << ' ';
  39. std::cout << '\n';
  40. }
  41. } // namespace
  42. int main()
  43. {
  44. try
  45. {
  46. tests::run();
  47. return 0;
  48. }
  49. catch(const std::exception &ex)
  50. {
  51. std::cerr << ex.what() << '\n';
  52. }
  53. return 1;
  54. }