xref: /AOO41X/main/vos/inc/vos/ref.hxx (revision 1be3ed10e7d2672e4fa7df184b766627fd8bc1dd)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #ifndef _VOS_REF_HXX_
26 #define _VOS_REF_HXX_
27 
28 /**
29     ORef<T>
30 
31     template type to implement handle/body behaviour
32     with reference-counting.
33 
34     Note that the template-type T MUST implement IReferenceCounter.
35 
36 */
37 
38 #   include <vos/refernce.hxx>
39 #ifndef _VOS_DIAGNOSE_HXX_
40 #   include <vos/diagnose.hxx>
41 #endif
42 
43 namespace vos
44 {
45 
46 
47 template <class T>
48 class ORef
49 {
50 public:
51 
52     /** Creates an "empty" reference, use "create()" or
53         assignment/copy-operator to make it a valid reference.
54     */
55     ORef();
56 
57     /** Creates a reference which points to pBody.
58         pBodys reference is not changed!. (like create(...)).
59     */
60     ORef(T* pBody);
61 
62     /** Overload copy-constr. to implement ref-counting.
63         As a result of the following code:
64 
65             ORef<x> h1, h2;
66 
67             h1.create();
68             h2= h1;
69 
70         h1 and h2 will represent the same body.
71 
72     */
73     ORef(const ORef<T>& handle);
74 
75     /** Decreases ref-count of underlying body.
76     */
77     inline ~ORef();
78 
79     /** Overload assignment-operator to implement ref-counting.
80         Unbinds this instance from its body (if bound) and
81         bind it to the body represented by the handle.
82     */
83     ORef<T>& SAL_CALL operator= (const ORef<T>& handle);
84 
85 
86 
87     /** Binds the body to this handle.
88         The "pBody"s reference-count is increased.
89 
90         If you call bind() on an existing body,
91         the old body is unbound before the new one is
92         assigned.
93 
94     */
95     void SAL_CALL bind(T* pBody);
96 
97     /** Unbind the body from this handle.
98         Note that for a handle representing a large body,
99         "handle.unbind().bind(new body());" _might_
100         perform a little bit better than "handle.bind(new body());",
101         since in the second case two large objects exist in memory
102         (the old body and the new body).
103     */
104     ORef<T>& SAL_CALL unbind();
105 
106     /** Same as bind().
107     */
108     void SAL_CALL operator= (T* pBody);
109 
110     /** Just in case you want to call handle().someBodyOp()...
111     */
112     T& SAL_CALL operator() () const;
113 
114     /** Allows (*handle).someBodyOp().
115     */
116     T& SAL_CALL operator* () const;
117 
118     /** Probably most common used: handle->someBodyOp().
119     */
120     T* SAL_CALL operator->() const;
121 
122     /** Gives access to the handles body.
123     */
124     T& SAL_CALL getBody() const;
125 
126     /** Can be used instead of operator->.
127         I.e. handle->someBodyOp() and handle.getBodyPtr()->someBodyOp()
128         are the same.
129     */
130     T* SAL_CALL getBodyPtr() const;
131 
132     /** Returns True is the body is empty (the handle
133         does not point to a valid body).
134     */
135     sal_Bool SAL_CALL isEmpty() const;
136 
137     /** Returns True is the body is "full" (the handle
138         does point to a valid body).
139     */
140     sal_Bool SAL_CALL isValid() const;
141 
142     /** Returns True is handle points to the same body.
143     */
144     sal_Bool SAL_CALL isEqualBody(const ORef<T>& handle) const;
145 
146     /** Delegates comparison to the body.
147     */
148     sal_Bool SAL_CALL operator== (const ORef<T>& handle) const;
149 
150     /** Delegates comparison to the body.
151     */
152     sal_Bool SAL_CALL operator!= (const ORef<T>& handle) const;
153 
154     /** Returns True is "this" points to pBody.
155     */
156     sal_Bool SAL_CALL operator== (const T* pBody) const;
157 
158     /** Needed to place ORefs into STL collection.
159         Delegates comparison to the body.
160     */
161     sal_Bool SAL_CALL operator< (const ORef<T>& handle) const;
162 
163     /** Needed to place ORefs into STL collection.
164         Delegates comparison to the body.
165     */
166     sal_Bool SAL_CALL operator> (const ORef<T>& handle) const;
167 
168 protected:
169 
170     T* m_refBody;
171 };
172 
173 // include template implementation
174 #include <vos/ref.inl>
175 
176 }
177 
178 
179 #endif // _VOS_REF_HXX_
180