Fix number of compile errors and warnings with GCC 14
[lsnes.git] / include / library / gc.hpp
blob07cfcd75596b2578b094b1e63472ad1ed060421a
1 #ifndef _library__gc__hpp__included__
2 #define _library__gc__hpp__included__
4 #include <cstdlib>
6 namespace GC
8 class item
10 public:
11 item();
12 virtual ~item();
13 void mark_root();
14 void unmark_root();
15 static void do_gc();
16 protected:
17 virtual void trace() = 0;
18 void mark();
19 private:
20 size_t root_count;
21 bool reachable;
24 struct obj_tag {};
25 template<class T> class pointer
27 public:
28 pointer()
30 ptr = NULL;
32 pointer(T* obj)
34 ptr = obj;
36 template<typename... U> pointer(obj_tag tag, U... args)
38 ptr = new T(args...);
40 pointer(const pointer& p)
42 if(p.ptr) p.ptr->mark_root();
43 ptr = p.ptr;
45 pointer& operator=(const pointer& p)
47 if(ptr == p.ptr) return *this;
48 if(ptr) ptr->unmark_root();
49 if(p.ptr) p.ptr->mark_root();
50 ptr = p.ptr;
51 return *this;
53 ~pointer()
55 if(ptr) ptr->unmark_root();
57 operator bool()
59 return (ptr != NULL);
61 T* operator->() { return ptr; }
62 T& operator*() { return *ptr; }
63 T* as_pointer() { return ptr; }
64 private:
65 T* ptr;
69 #endif