Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: compiled/intrusive_ptr.h

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Reworked JS binding generation Created Feb. 1, 2016, 9:14 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: compiled/intrusive_ptr.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/intrusive_ptr.h
@@ -0,0 +1,185 @@
+// Parts of this code have been copied from boost/smart_ptr/intrusive_ptr.hpp.
+//
+// Copyright (c) 2001, 2002 Peter Dimov
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef ADBLOCK_PLUS_INTRUSIVE_PTR_H
+#define ADBLOCK_PLUS_INTRUSIVE_PTR_H
+
+#include <type_traits>
+
+class ref_counted
+{
+public:
+ void AddRef()
+ {
+ mRefCount++;
+ }
+
+ void ReleaseRef()
+ {
+ if (--mRefCount == 0)
René Jeschke 2016/02/02 11:11:23 Nit: having a check on `0` here feels wrong and ma
Wladimir Palant 2016/02/02 17:55:19 It's quite the opposite I think - if we are alread
+ delete this;
+ }
+
+protected:
+ ref_counted()
+ : mRefCount(0)
+ {
+ }
+
+ // We need this virtual destructor, otherwise pointers to ref_counted and
+ // pointers to derived classes won't have the same value (e.g. converting from
+ // Filter* to ref_counted* decreases pointer value by 4).
+ virtual ~ref_counted()
+ {
+ }
+
+private:
+ int mRefCount;
+};
+
+template<class T,
+ class = typename std::enable_if<std::is_base_of<ref_counted,T>::value>::type>
+class intrusive_ptr
+{
+public:
+ intrusive_ptr()
+ : mPointer(nullptr)
+ {
+ }
+
+ intrusive_ptr(T* pointer)
+ : mPointer(pointer)
+ {
+ if (mPointer)
+ mPointer->AddRef();
+ }
+
+ intrusive_ptr(const intrusive_ptr& other)
+ : mPointer(other.mPointer)
+ {
+ if (mPointer)
+ mPointer->AddRef();
+ }
+
+ intrusive_ptr(intrusive_ptr&& other)
+ : mPointer(other.mPointer)
+ {
+ other.mPointer = nullptr;
+ }
+
+ ~intrusive_ptr()
+ {
+ if (mPointer)
+ mPointer->ReleaseRef();
+ }
+
+ intrusive_ptr& operator=(intrusive_ptr& other)
+ {
+ intrusive_ptr(other).swap(*this);
+ return *this;
+ }
+
+ intrusive_ptr& operator=(intrusive_ptr&& other)
+ {
+ intrusive_ptr(other).swap(*this);
+ return *this;
+ }
+
+ intrusive_ptr& operator=(T* other)
+ {
+ intrusive_ptr(other).swap(*this);
+ }
+
+ void reset()
+ {
+ intrusive_ptr().swap(*this);
+ }
+
+ void reset(T* other)
+ {
+ intrusive_ptr(other).swap(*this);
+ }
+
+ T* get() const
+ {
+ return mPointer;
+ }
+
+ T& operator*() const
+ {
+ return *mPointer;
+ }
+
+ T* operator->() const
+ {
+ return mPointer;
+ }
+
+ operator bool() const
+ {
+ return mPointer != nullptr;
+ }
+
+ operator T*() const
+ {
+ return mPointer;
+ }
+
+ bool operator!() const
+ {
+ return mPointer == nullptr;
+ }
+
+ void swap(intrusive_ptr& other)
+ {
+ T* tmp = mPointer;
René Jeschke 2016/02/02 11:11:24 Nit: is there a specific reason why you're not usi
Wladimir Palant 2016/02/02 17:55:19 You have to ask Boost devs :) I haven't heard abo
+ mPointer = other.mPointer;
+ other.mPointer = tmp;
+ }
+
+private:
+ T* mPointer;
+};
+
+template<class T, class U>
+inline bool operator==(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b)
+{
+ return a.get() == b.get();
+}
+
+template<class T, class U>
+inline bool operator!=(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b)
+{
+ return a.get() != b.get();
+}
+
+template<class T, class U>
+inline bool operator==(const intrusive_ptr<T>& a, const U* b)
+{
+ return a.get() == b;
+}
+
+template<class T, class U>
+inline bool operator!=(const intrusive_ptr<T>& a, const U* b)
+{
+ return a.get() != b;
+}
+
+template<class T, class U>
+inline bool operator==(const T* a, const intrusive_ptr<U>& b)
+{
+ return a == b.get();
+}
+
+template<class T, class U>
+inline bool operator!=(const T* a, intrusive_ptr<U> const& b)
+{
+ return a != b.get();
+}
+
+#endif
« compiled/StringMap.h ('K') | « compiled/debug.h ('k') | compiled/shell.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld