Chromium Code Reviews| Index: Source/wtf/Ptr.h |
| diff --git a/Source/wtf/Ptr.h b/Source/wtf/Ptr.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9036c94f81dc69b2f2d052a8111866d23f4631eb |
| --- /dev/null |
| +++ b/Source/wtf/Ptr.h |
| @@ -0,0 +1,83 @@ |
| +/* |
| + * Copyright (C) 2014 Google, Inc. All Rights Reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions |
| + * are met: |
| + * 1. Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * 2. Redistributions in binary form must reproduce the above copyright |
| + * notice, this list of conditions and the following disclaimer in the |
| + * documentation and/or other materials provided with the distribution. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
|
haraken
2014/01/16 05:21:22
Not APPLE :) Please use the copyright in heap/Heap
Mads Ager (chromium)
2014/01/16 07:04:15
Heh! Without thinking I copied this from RefPtr.h.
|
| + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#ifndef WTF_Ptr_h |
| +#define WTF_Ptr_h |
| + |
| +// Ptr is a simple wrapper for a raw pointer that provides the |
| +// interface (get, clear) of other pointer types such as RefPtr, |
|
haraken
2014/01/16 05:21:22
Are compilers smart enough to optimize Ptr operati
Mads Ager (chromium)
2014/01/16 07:04:15
Yes, they should be. The issue that we saw when we
haraken
2014/01/16 07:06:40
Makes sense. Thanks for the clarification.
|
| +// Persistent and Member. This is used for the Blink garbage |
| +// collection work in order to be able to write shared code that will |
| +// use reference counting or garbage collection based on a |
| +// compile-time flag. |
| + |
| +namespace WTF { |
| + |
| +template<typename T> |
| +class Ptr { |
| +public: |
| + Ptr(T* ptr) : m_ptr(ptr) { } |
| + Ptr(std::nullptr_t) : m_ptr(0) { } |
| + |
| + template<typename U> |
| + Ptr(const Ptr<U>& other) |
| + : m_ptr(other.get()) |
| + { |
| + } |
| + |
| + T* get() const { return m_ptr; } |
| + void clear() { m_ptr = 0; } |
| + |
| + Ptr& operator=(T* ptr) |
| + { |
| + m_ptr = ptr; |
| + return *this; |
| + } |
| + |
| + Ptr& operator=(std::nullptr_t) |
| + { |
| + m_ptr = 0; |
| + return *this; |
| + } |
| + |
| + operator T*() const { return m_ptr; } |
| + T& operator*() const { return *m_ptr; } |
|
haraken
2014/01/16 05:21:22
Don't we need:
const T& operator*() const;
?
Mads Ager (chromium)
2014/01/16 07:04:15
I don't think so, no. T itself can be something li
|
| + T* operator->() const { return m_ptr; } |
| + bool operator!() const { return !m_ptr; } |
| + |
| + // This conversion operator allows implicit conversion to bool but |
| + // not to other integer types. |
| + typedef T* Ptr::*UnspecifiedBoolType; |
| + operator UnspecifiedBoolType() const { return m_ptr ? &m_ptr : 0; } |
| + |
| +private: |
| + T* m_ptr; |
| +}; |
| + |
| +} // namespace WTF |
| + |
| +using WTF::Ptr; |
| + |
| +#endif |