Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / public / platform / WebPrivatePtr.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef WebPrivatePtr_h
32 #define WebPrivatePtr_h
33
34 #include "WebCommon.h"
35
36 #if INSIDE_BLINK
37 #include "wtf/PassRefPtr.h"
38 #endif
39
40 namespace blink {
41
42 // This class is an implementation detail of the Blink API. It exists to help
43 // simplify the implementation of Blink interfaces that merely wrap a reference
44 // counted WebCore class.
45 //
46 // A typical implementation of a class which uses WebPrivatePtr might look like
47 // this:
48 //    class WebFoo {
49 //    public:
50 //        BLINK_EXPORT ~WebFoo();
51 //        WebFoo() { }
52 //        WebFoo(const WebFoo& other) { assign(other); }
53 //        WebFoo& operator=(const WebFoo& other)
54 //        {
55 //            assign(other);
56 //            return *this;
57 //        }
58 //        BLINK_EXPORT void assign(const WebFoo&);  // Implemented in the body.
59 //
60 //        // Methods that are exposed to Chromium and which are specific to
61 //        // WebFoo go here.
62 //        BLINK_EXPORT doWebFooThing();
63 //
64 //        // Methods that are used only by other Blink classes should only be
65 //        // declared when INSIDE_BLINK is set.
66 //    #if INSIDE_BLINK
67 //        WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
68 //    #endif
69 //
70 //    private:
71 //        WebPrivatePtr<WebCore::Foo> m_private;
72 //    };
73 //
74 //    // WebFoo.cpp
75 //    WebFoo::~WebFoo() { m_private.reset(); }
76 //    void WebFoo::assign(const WebFoo& other) { ... }
77 //
78 template <typename T>
79 class WebPrivatePtr {
80 public:
81     WebPrivatePtr() : m_ptr(0) { }
82     ~WebPrivatePtr()
83     {
84         // We don't destruct the object pointed by m_ptr here because we don't
85         // want to expose destructors of core classes to embedders. We should
86         // call reset() manually in destructors of classes with WebPrivatePtr
87         // members.
88         BLINK_ASSERT(!m_ptr);
89     }
90
91     bool isNull() const { return !m_ptr; }
92
93 #if INSIDE_BLINK
94     WebPrivatePtr(const PassRefPtr<T>& prp)
95         : m_ptr(prp.leakRef())
96     {
97     }
98
99     void reset()
100     {
101         assign(0);
102     }
103
104     WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other)
105     {
106         T* p = other.m_ptr;
107         if (p)
108             p->ref();
109         assign(p);
110         return *this;
111     }
112
113     WebPrivatePtr<T>& operator=(const PassRefPtr<T>& prp)
114     {
115         assign(prp.leakRef());
116         return *this;
117     }
118
119     T* get() const
120     {
121         return m_ptr;
122     }
123
124     T* operator->() const
125     {
126         ASSERT(m_ptr);
127         return m_ptr;
128     }
129 #endif
130
131 private:
132 #if INSIDE_BLINK
133     void assign(T* p)
134     {
135         // p is already ref'd for us by the caller
136         if (m_ptr)
137             m_ptr->deref();
138         m_ptr = p;
139     }
140 #else
141     // Disable the assignment operator; we define it above for when
142     // INSIDE_BLINK is set, but we need to make sure that it is not
143     // used outside there; the compiler-provided version won't handle reference
144     // counting properly.
145     WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other);
146 #endif
147     // Disable the copy constructor; classes that contain a WebPrivatePtr
148     // should implement their copy constructor using assign().
149     WebPrivatePtr(const WebPrivatePtr<T>&);
150
151     T* m_ptr;
152 };
153
154 } // namespace blink
155
156 #endif