Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(941)

Unified Diff: Source/wtf/NonNullPtr.h

Issue 23890025: WIP (Introduce WTF::NonNullPtr<T>.) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/wtf/Forward.h ('k') | Source/wtf/wtf.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/wtf/NonNullPtr.h
diff --git a/Source/wtf/NonNullPtr.h b/Source/wtf/NonNullPtr.h
new file mode 100644
index 0000000000000000000000000000000000000000..e42a425065fa1bad8b0765fb1955ac9b1cb09209
--- /dev/null
+++ b/Source/wtf/NonNullPtr.h
@@ -0,0 +1,39 @@
+#ifndef NonNullPtr_h
+#define NonNullPtr_h
+
+#include "wtf/Assertions.h"
+
+namespace WTF {
+
+template<typename T> class NonNullPtr {
+public:
+ NonNullPtr(T* ptr) : m_ptr(ptr) { ASSERT(m_ptr); }
+ NonNullPtr(const NonNullPtr& other) : m_ptr(other.m_ptr) { ASSERT(m_ptr); }
+ // For non-const -> const conversion, derived class -> base class conversion.
+ template<typename U> NonNullPtr(const NonNullPtr<U>& other) : m_ptr(other.get()) { ASSERT(m_ptr); }
+ T* operator->() const { return m_ptr; }
+ T* get() const { return m_ptr; }
+ NonNullPtr& operator=(T* ptr)
+ {
+ ASSERT(ptr);
+ m_ptr = ptr;
+ }
+
+private:
+ T* m_ptr;
+};
+
+
+template<typename T, typename U> inline bool operator==(const NonNullPtr<T>& a, const NonNullPtr<U>& b) { return a.get() == b.get(); }
+template<typename T, typename U> inline bool operator!=(const NonNullPtr<T>& a, const NonNullPtr<U>& b) { return a.get() != b.get(); }
+template<typename T, typename U> inline bool operator==(const NonNullPtr<T>& a, U* b) { return a.get() == b; }
+template<typename T, typename U> inline bool operator!=(const NonNullPtr<T>& a, U* b) { return a.get() != b; }
+template<typename T, typename U> inline bool operator==(T* a, const NonNullPtr<U>& b) { return a == b.get(); }
+template<typename T, typename U> inline bool operator!=(T* a, const NonNullPtr<U>& b) { return a != b.get(); }
+
+
+}
+
+using WTF::NonNullPtr;
+
+#endif
« no previous file with comments | « Source/wtf/Forward.h ('k') | Source/wtf/wtf.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698