Object.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 onChildRemoved(T* x, T* parent)
  8. {
  9. std::cout << __PRETTY_FUNCTION__ << std::endl;
  10. Alloc alloc = x->getAllocator();
  11. alloc.destroy(x);
  12. alloc.deallocate(x, 1);
  13. }
  14. void onChildAdded(T*, T*)
  15. {}
  16. };
  17. struct Foo2: public Object<Foo2, Allocator<Foo2>,
  18. Deleter<Foo2, Allocator<Foo2>>>
  19. {
  20. typedef Object<Foo2, Allocator<Foo2>,
  21. Deleter<Foo2, Allocator<Foo2>>> Base;
  22. int x = 666;
  23. static int constructorCallCount;
  24. static int destructorCallCount;
  25. Foo2(Foo2* parent)
  26. : Base(parent)
  27. {
  28. std::cout << __PRETTY_FUNCTION__ << std::endl;
  29. ++constructorCallCount;
  30. }
  31. ~Foo2()
  32. {
  33. std::cout << __PRETTY_FUNCTION__ << std::endl;
  34. ++destructorCallCount;
  35. }
  36. Foo2& operator=(const Foo2& b)
  37. {
  38. std::cout << __PRETTY_FUNCTION__ << std::endl;
  39. x = b.x;
  40. return *this;
  41. }
  42. Foo2& operator=(Foo2&& b)
  43. {
  44. std::cout << __PRETTY_FUNCTION__ << std::endl;
  45. x = b.x;
  46. b.x = 0;
  47. return *this;
  48. }
  49. static void reset()
  50. {
  51. destructorCallCount = constructorCallCount = 0;
  52. }
  53. };
  54. int Foo2::constructorCallCount = 0;
  55. int Foo2::destructorCallCount = 0;
  56. ANKI_TEST(Object, Test)
  57. {
  58. Foo2* a = new Foo2(nullptr);
  59. Foo2* b = new Foo2(a);
  60. Foo2* c = new Foo2(b);
  61. (void)c;
  62. delete a;
  63. }