gc.hpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef AFUN_GC_HPP
  2. #define AFUN_GC_HPP
  3. #include "tool.hpp"
  4. #include "aFunCoreExport.h"
  5. #include "queue"
  6. namespace aFuncore {
  7. typedef unsigned GcCount;
  8. class GcObjectBase {
  9. bool not_clear; // 不清除
  10. GcCount reference; // 引用计数
  11. bool reachable; // 可达标记 [同时标识已迭代]
  12. public:
  13. GcObjectBase() : not_clear{false}, reference{0}, reachable{false} {}
  14. };
  15. template <class T>
  16. class GcObject : public GcObjectBase {
  17. T *prev;
  18. T *next;
  19. public:
  20. GcObject();
  21. virtual ~GcObject()=default;
  22. void addObject(T *&chain);
  23. void delObject(T *&chain);
  24. static void destruct(T *&chain);
  25. };
  26. class GcList {
  27. std::queue<GcObjectBase *> queue;
  28. public :
  29. AFUN_CORE_EXPORT size_t add(GcObjectBase *obj);
  30. AFUN_CORE_EXPORT GcObjectBase *pop();
  31. [[nodiscard]] size_t getSize() const {return queue.size();}
  32. [[nodiscard]] size_t isEmpty() const {return queue.empty();}
  33. };
  34. };
  35. #endif //AFUN_GC_HPP