Object.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "tests/framework/Framework.h"
  2. #include "anki/util/Object.h"
  3. using namespace anki;
  4. template<typename T, typename Alloc>
  5. struct Deleter
  6. {
  7. void operator()(T* x)
  8. {
  9. std::cout << __PRETTY_FUNCTION__ << std::endl;
  10. Alloc alloc = x->getAllocator();
  11. alloc.destroy(x);
  12. alloc.deallocate(x, 1);
  13. }
  14. };
  15. struct Foo2: public Object<Foo2, Allocator<Foo2>,
  16. Deleter<Foo2, Allocator<Foo2>>>
  17. {
  18. typedef Object<Foo2, Allocator<Foo2>,
  19. Deleter<Foo2, Allocator<Foo2>>> Base;
  20. int x = 666;
  21. static int constructorCallCount;
  22. static int destructorCallCount;
  23. Foo2(Foo2* parent)
  24. : Base(parent)
  25. {
  26. std::cout << __PRETTY_FUNCTION__ << std::endl;
  27. ++constructorCallCount;
  28. }
  29. ~Foo2()
  30. {
  31. std::cout << __PRETTY_FUNCTION__ << std::endl;
  32. ++destructorCallCount;
  33. }
  34. Foo2& operator=(const Foo2& b)
  35. {
  36. std::cout << __PRETTY_FUNCTION__ << std::endl;
  37. x = b.x;
  38. return *this;
  39. }
  40. Foo2& operator=(Foo2&& b)
  41. {
  42. std::cout << __PRETTY_FUNCTION__ << std::endl;
  43. x = b.x;
  44. b.x = 0;
  45. return *this;
  46. }
  47. static void reset()
  48. {
  49. destructorCallCount = constructorCallCount = 0;
  50. }
  51. };
  52. int Foo2::constructorCallCount = 0;
  53. int Foo2::destructorCallCount = 0;
  54. ANKI_TEST(Object, Test)
  55. {
  56. Foo2* a = new Foo2(nullptr);
  57. Foo2* b = new Foo2(a);
  58. Foo2* c = new Foo2(b);
  59. (void)c;
  60. delete a;
  61. }