gc.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef AFUN_GC_H
  2. #define AFUN_GC_H
  3. #include <queue>
  4. #include <list>
  5. #include "aFuntool.h"
  6. #include "aFunCoreExport.h"
  7. namespace aFuncore {
  8. typedef unsigned GcCount;
  9. class Inter;
  10. class AFUN_CORE_EXPORT GcObjectBase {
  11. public:
  12. GcObjectBase(const GcObjectBase &) = delete;
  13. GcObjectBase &operator=(const GcObjectBase &) = delete;
  14. inline void addReference();
  15. inline void delReference();
  16. [[nodiscard]] inline GcCount getReference() const;
  17. inline void setClear(bool clear=false);
  18. inline void setReachable(bool is_reference=false);
  19. static void destructAll(std::list<GcObjectBase *> &list, Inter &gc_inter);
  20. protected:
  21. std::mutex lock;
  22. inline GcObjectBase();
  23. virtual ~GcObjectBase() = default;
  24. private:
  25. bool not_clear; // 不清除
  26. bool reachable; // 可达标记 [同时标识已迭代]
  27. GcCount reference; // 引用计数
  28. };
  29. class AFUN_CORE_EXPORT GcList {
  30. public :
  31. size_t add(GcObjectBase *obj);
  32. GcObjectBase *pop();
  33. [[nodiscard]] inline size_t getSize() const;
  34. [[nodiscard]] inline size_t isEmpty() const;
  35. private:
  36. std::queue<GcObjectBase *> queue;
  37. };
  38. };
  39. #include "gc.inline.h"
  40. #endif //AFUN_GC_H