From 5e76da13f674631a125039a8b92dfde7e819adb0 Mon Sep 17 00:00:00 2001
From: HJfod <60038575+HJfod@users.noreply.github.com>
Date: Tue, 5 Mar 2024 18:25:25 +0200
Subject: [PATCH] add ObjWrapper

---
 loader/include/Geode/utils/cocos.hpp | 39 ++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp
index a740f2ad..b2b99dad 100644
--- a/loader/include/Geode/utils/cocos.hpp
+++ b/loader/include/Geode/utils/cocos.hpp
@@ -570,6 +570,45 @@ namespace geode {
             return nullptr;
         }
     };
+
+    /**
+     * A simple `CCObject` wrapper for a non-`CCObject` type
+     */
+    template <class T>
+        requires (!std::is_base_of_v<T, cocos2d::CCObject>)
+    class ObjWrapper : public cocos2d::CCObject {
+    protected:
+        T m_value;
+
+        ObjWrapper(T&& value) : m_value(std::forward<T>(value)) {
+            this->autorelease();
+        }
+        ObjWrapper(T const& value) : m_value(value) {
+            this->autorelease();
+        }
+
+    public:
+        /**
+         * Construct an object wrapper
+         */
+        static ObjWrapper* create(T&& value) {
+            return new ObjWrapper(std::forward<T>(value));
+        }
+        /**
+         * Construct an object wrapper
+         */
+        static ObjWrapper* create(T const& value) {
+            return new ObjWrapper(value);
+        }
+
+        // @note This returns a const& to allow move-only types to be returned!
+        T const& getValue() const& {
+            return m_value;
+        }
+        void setValue(T&& value) {
+            m_value = std::forward<T>(value);
+        }
+    };
 }
 
 // Cocos2d utils