gc.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. bool reachable; // 可达标记 [同时标识已迭代]
  11. GcCount reference; // 引用计数
  12. public:
  13. GcObjectBase() : not_clear{false}, reference{0}, reachable{false} {}
  14. void addReference() {reference++;}
  15. void delReference() {reference--;}
  16. void setClear(bool clear=false) {not_clear=!clear;}
  17. void setReachable(bool is_reference=false) {reachable=is_reference;}
  18. };
  19. template <class T>
  20. class GcObject : public GcObjectBase {
  21. T *prev;
  22. T *next;
  23. public:
  24. GcObject();
  25. virtual ~GcObject()=default;
  26. void addObject(T *&chain);
  27. void delObject(T *&chain);
  28. static void destruct(T *&chain);
  29. };
  30. class GcList {
  31. std::queue<GcObjectBase *> queue;
  32. public :
  33. AFUN_CORE_EXPORT size_t add(GcObjectBase *obj);
  34. AFUN_CORE_EXPORT GcObjectBase *pop();
  35. [[nodiscard]] size_t getSize() const {return queue.size();}
  36. [[nodiscard]] size_t isEmpty() const {return queue.empty();}
  37. };
  38. };
  39. #endif //AFUN_GC_HPP