* Reference counter is upgraded 4.x-dev

Sun, 13 Dec 2020 10:53:14 +0200

author
cemkalyoncu
date
Sun, 13 Dec 2020 10:53:14 +0200
branch
4.x-dev
changeset 1516
dc526bcc1ad3
parent 1515
1d8909ed4d1f
child 1517
868dcba12c8a

* Reference counter is upgraded

Source/Gorgon/Scripting/Embedding.h file | annotate | diff | comparison | revisions
Source/Gorgon/Scripting/Reflection.h file | annotate | diff | comparison | revisions
Source/Gorgon/TMP.h file | annotate | diff | comparison | revisions
Source/Gorgon/Utils/RefCounter.h file | annotate | diff | comparison | revisions
Source/Gorgon/Utils/dir.cmake file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/Scripting/Embedding.h	Wed Dec 09 09:29:17 2020 +0200
+++ b/Source/Gorgon/Scripting/Embedding.h	Sun Dec 13 10:53:14 2020 +0200
@@ -515,7 +515,7 @@
 		
 		template<int ...S_>
 		void check(TMP::Sequence<S_...>) {
-			char dummy[] = {0, (checkparam<S_>(),'\0')...};
+			char dummy[[gnu::unused]] [] = {0, (checkparam<S_>(),'\0')...};
 		}
 		
 		virtual void dochecks(bool ismethod) override {
@@ -1861,11 +1861,11 @@
 			}
 		}
 		
-		virtual std::string ToString(const Data &d) const {
+		virtual std::string ToString(const Data &d) const override {
 			return String::From(d.GetValue<E_>());
 		}
 		
-		virtual Data Parse(const std::string &s) const {
+		virtual Data Parse(const std::string &s) const override {
 			return {this, String::Parse<E_>(s)};
 		}
 		
@@ -1887,7 +1887,7 @@
 		}
 		
 	protected:
-		virtual bool compare(const Data& l, const Data& r) const {
+		virtual bool compare(const Data& l, const Data& r) const override {
 			return l.GetValue<E_>() == r.GetValue<E_>();
 		}
 		
--- a/Source/Gorgon/Scripting/Reflection.h	Wed Dec 09 09:29:17 2020 +0200
+++ b/Source/Gorgon/Scripting/Reflection.h	Sun Dec 13 10:53:14 2020 +0200
@@ -143,7 +143,7 @@
 			template <class ...Params_>
 			Parameter(const std::string &name, const std::string &help, const Type *type, 
 					 Data defaultvalue, OptionList options, Params_ ...tags) : 
-			name(name), type(type), help(help), defaultvalue(defaultvalue) {
+			name(name), help(help), type(type), defaultvalue(defaultvalue) {
 				ASSERT((type!=nullptr), "Parameter type cannot be nullptr", 1, 2);
 				using std::swap;
 
@@ -591,7 +591,7 @@
 				/// Regular constructor that can take as many tags as needed.
 				template<class ...P_>
 				Overload(const Type *returntype, ParameterList parameters, P_ ...tags) :
-				returntype(returntype), Parameters(this->parameters)
+				Parameters(this->parameters), returntype(returntype)
 				{
 					using std::swap;
 					swap(parameters, this->parameters);
@@ -602,9 +602,9 @@
 				/// A full constructor
 				Overload(const Type *returntype, ParameterList parameters, bool stretchlast, bool repeatlast, 
 						bool accessible, bool constant, bool returnsref, bool returnsconst, bool implicit) :
-				returntype(returntype), stretchlast(stretchlast), repeatlast(repeatlast), 
-				accessible(accessible), constant(constant), returnsref(returnsref), returnsconst(returnsconst),
-				implicit(implicit), Parameters(this->parameters)
+				Parameters(this->parameters), returntype(returntype), stretchlast(stretchlast), 
+				repeatlast(repeatlast), accessible(accessible), constant(constant), 
+				returnsref(returnsref), returnsconst(returnsconst), implicit(implicit)
 				{
 					using std::swap;
 					swap(parameters, this->parameters);
@@ -771,7 +771,7 @@
 			Function(const std::string &name, const std::string &help, const Type *parent, 
 					 const Containers::Collection<Overload> &overloads, const Containers::Collection<Overload> &methods, 
 					 Tag tag, P_ ...tags) : 
-			StaticMember(name, help), parent(parent), Overloads(this->overloads), Methods(this->methods)
+			StaticMember(name, help), Overloads(this->overloads), Methods(this->methods), parent(parent)
 			{
 				
 				unpacktags(tag);
@@ -792,7 +792,7 @@
 			template<class ...P_>
 			Function(const std::string &name, const std::string &help, const Type *parent, 
 					 const Containers::Collection<Overload> &overloads, Tag tag, P_ ...tags) :
-			StaticMember(name, help), parent(parent), Overloads(this->overloads), Methods(this->methods)
+			StaticMember(name, help), Overloads(this->overloads), Methods(this->methods), parent(parent)
 			{
 				unpacktags(tag);
 				unpacktags(tags...);
@@ -809,7 +809,7 @@
 			Function(const std::string &name, const std::string &help, const Type *parent,
 					 const Containers::Collection<Overload> &overloads, const Containers::Collection<Overload> &methods=Containers::Collection<Overload>()
 			) : 
-			StaticMember(name, help), parent(parent), Overloads(this->overloads), Methods(this->methods)
+			StaticMember(name, help), Overloads(this->overloads), Methods(this->methods), parent(parent)
 			{ 
 				for(auto &overload : overloads) {
 					AddOverload(overload);
--- a/Source/Gorgon/TMP.h	Wed Dec 09 09:29:17 2020 +0200
+++ b/Source/Gorgon/TMP.h	Sun Dec 13 10:53:14 2020 +0200
@@ -285,7 +285,7 @@
 			using NewType = typename std::remove_const<typename Choose<std::is_reference<T_>::value, typename std::remove_reference<T_>::type*, T_>::Type>::type;
 			using CloneType = const typename Choose<std::is_reference<T_>::value, typename std::remove_reference<T_>::type*, T_>::Type* const;
 			
-			virtual RTTS *Duplicate() const {
+			virtual RTTS *Duplicate() const override {
 				return new AbstractRTT<T_>();
 			}
 			
--- a/Source/Gorgon/Utils/RefCounter.h	Wed Dec 09 09:29:17 2020 +0200
+++ b/Source/Gorgon/Utils/RefCounter.h	Sun Dec 13 10:53:14 2020 +0200
@@ -1,114 +1,84 @@
-//DESCRIPTION
-//	RefCounter is generic reference counter. It uses curiously recurring
-//	template method to add reference counting to the classes that need without
-//	any additional overhead.
-
-//WARNING:
-// Might cause problems with multiple inheritance.
-
-//REQUIRES:
-//	--
-
-//LICENSE
-//	This program is free software: you can redistribute it and/or modify
-//	it under the terms of the Lesser GNU General Public License as published by
-//	the Free Software Foundation, either version 3 of the License, or
-//	(at your option) any later version.
-//
-//	This program is distributed in the hope that it will be useful,
-//	but WITHOUT ANY WARRANTY; without even the implied warranty of
-//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-//	Lesser GNU General Public License for more details.
-//
-//	You should have received a copy of the Lesser GNU General Public License
-//	along with this program. If not, see < http://www.gnu.org/licenses/ >.
-
-//COPYRIGHT
-//	Cem Kalyoncu, DarkGaze.Org (cemkalyoncu[at]gmail[dot]com)
-
 #pragma once
 
 
-#define REFCOUNTER_EXIST
+namespace Gorgon { namespace Utils {
 
-namespace gge { namespace utils {
+    /// RefCounter helps implementing reference counted objects. Suitable
+    /// for shared implicit heap management and flyweight objects.
+    /// RefCounter requires destroy method to be implemented by client
+    /// classes.
+    ///
+    /// Copy constructor should increment counter, assignment should call 
+    /// refassign, destructor should decrement counter. Add/subtract is not
+    /// called automatically.
+    template <class C_>
+    class RefCounter {
+    template<class C2_> friend class RefCounter;
+    protected:
+        RefCounter() : refcnt(new int(1)) {
+        }
 
-	//RefCounter requires following methods to be implemented by client
-	//classes:
-	// void dealloc() : called when no reference left, should deallocate
-	//                  buffers, set counts to 0, etc... object is still alive 
-	//                  and accessible, so control variables can be left out
-	// void destroy() : destroys everything, this is the last chance of freeing
-	//                  resources, nothing should be left out
-	//Copy constructor should increment counter, assignment should assign target ref 
-	//count to object's refcount, destructor should decrement it.
-	template <class C_>
-	class RefCounter {
-	  template<class C2_> friend class RefCounter;
-	protected:
-		RefCounter() : refcnt(new int(1)) {	
-		}
+        template <class C2_>
+        RefCounter(const RefCounter<C2_> &ref) : refcnt(ref.refcnt) {
+        }
 
-		template <class C2_>
-		RefCounter(const RefCounter<C2_> &ref) : refcnt(ref.refcnt) {
-		}
-
-		RefCounter(const RefCounter &ref) : refcnt(ref.refcnt) {
-		}
+        RefCounter(const RefCounter &ref) : refcnt(ref.refcnt) {
+        }
+        
+        ~RefCounter() {
+            if(*refcnt <= 0)
+                delete refcnt;
+        }
 
-		void refassign(const RefCounter &ref) {
-			removeref();
+        /// Assigns another counted object into this one
+        void refassign(const RefCounter &ref) {
+            removeref();
 
-			if(!refcheck()) {
-				static_cast<C_*>(this)->destroy();
-				delete refcnt;
-			}
+            if(!refcheck()) {
+                static_cast<C_*>(this)->destroy();
+                delete refcnt;
+            }
 
-			refcnt=ref.refcnt;
-			addref();
-		}
-	
-		bool refcheck() const {
-			return (*refcnt)>0;
-		}
+            refcnt = ref.refcnt;
+            addref();
+        }
+        
+        /// Checks if the reference is still alive
+        bool refcheck() const {
+            return (*refcnt) > 0;
+        }
 
-		void addref() const {
-			(*refcnt)++;
-		}
+        /// Adds a count to the reference
+        void addref() const {
+            (*refcnt)++;
+        }
 
-		void destructref() {
-			removeref();
-
-			if(!refcheck()) {
-				static_cast<C_*>(this)->destroy();
-				delete refcnt;
-			}
-		}
+        /// Destroys
+        void removeref() {
+            if(*refcnt <= 0)
+                return;
+            
+            (*refcnt)--;
 
-		void removeref() {
-			if(*refcnt>0)
-				(*refcnt)--;
-
-			if(*refcnt==0) {
-				static_cast<C_*>(this)->dealloc();
-			}
-		}
-
-		void removeref() const {
-			if(*refcnt>0)
-				(*refcnt)--;
+            if(!refcheck()) {
+                static_cast<C_*>(this)->destroy();
+            }
+        }
 
-			if(*refcnt==0) {
-				throw std::runtime_error("Cannot destroy a const object");
-			}
-		}
+        /// returns the number of references.
+        int getrefcount() const {
+            return *refcnt;
+        }
+        
+        /// creates a new instance of the contained data.
+        /// Effectively this creates a new reference counter
+        /// with value of 1. Old reference is not reduced.
+        void newinstance() {
+            refcnt = new int(1);
+        }
 
-		int getrefcount() const {
-			return *refcnt;
-		}
-
-		////Reference count
-		mutable int *refcnt;
-	};
+        /// Reference count
+        mutable int *refcnt;
+    };
 
 } }
--- a/Source/Gorgon/Utils/dir.cmake	Wed Dec 09 09:29:17 2020 +0200
+++ b/Source/Gorgon/Utils/dir.cmake	Sun Dec 13 10:53:14 2020 +0200
@@ -2,6 +2,7 @@
     Console.h
 	Assert.h
 	Logging.h
+	RefCounter.h
 	ScopeGuard.h
 )
 

mercurial