Browse Source

== and != operators for Ref<T> / T*

This is to prevent crashes for code like:

...
void Material::set_next_pass(const Ref<Material> &p_pass) {

	ERR_FAIL_COND(p_pass == this);
...

that's been fixed in 031f763d4fda4e0dbcdf90a170aad3124c50c062
Marcin Zawiejski 7 years ago
parent
commit
a1d2fbdeb2
2 changed files with 8 additions and 1 deletions
  1. 7 0
      core/reference.h
  2. 1 1
      scene/resources/material.cpp

+ 7 - 0
core/reference.h

@@ -87,6 +87,13 @@ class Ref {
 
 
 	//virtual Reference * get_reference() const { return reference; }
 	//virtual Reference * get_reference() const { return reference; }
 public:
 public:
+	_FORCE_INLINE_ bool operator==(const T *p_ptr) const {
+		return reference == p_ptr;
+	}
+	_FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
+		return reference != p_ptr;
+	}
+
 	_FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
 	_FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
 
 
 		return reference < p_r.reference;
 		return reference < p_r.reference;

+ 1 - 1
scene/resources/material.cpp

@@ -34,7 +34,7 @@
 
 
 void Material::set_next_pass(const Ref<Material> &p_pass) {
 void Material::set_next_pass(const Ref<Material> &p_pass) {
 
 
-	ERR_FAIL_COND(p_pass.ptr() == this);
+	ERR_FAIL_COND(p_pass == this);
 
 
 	if (next_pass == p_pass)
 	if (next_pass == p_pass)
 		return;
 		return;