xref: /trunk/main/vos/inc/vos/refernce.hxx (revision d98e0520)
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 #ifndef _VOS_REFERNCE_HXX_
25 #define _VOS_REFERNCE_HXX_
26 
27 #	include <vos/types.hxx>
28 #	include <osl/interlck.h>
29 #	include <vos/object.hxx>
30 #	include <vos/vosdllapi.h>
31 
32 namespace vos
33 {
34 
35 /** Interface for refernce-counting
36 */
37 class IReference
38 {
39 public:
40 
IReference()41     IReference() { }
~IReference()42     virtual ~IReference() { }
43 
44 	typedef oslInterlockedCount RefCount;
45 
46 	virtual RefCount SAL_CALL acquire()=0;
47 	virtual RefCount SAL_CALL release()=0;
48 
49 	virtual RefCount SAL_CALL referenced() const=0;
50 };
51 
52 class VOS_DLLPUBLIC ORefCount
53 {
54 public:
55 	typedef IReference::RefCount RefCount;
56 
ORefCount()57 	ORefCount() { m_RefCount = 0; }
ORefCount(RefCount n)58 	ORefCount(RefCount n) { m_RefCount = n; }
59     virtual ~ORefCount();
60 
acquire()61 	RefCount SAL_CALL acquire() { return (osl_incrementInterlockedCount(&m_RefCount)); };
release()62 	RefCount SAL_CALL release() { return (osl_decrementInterlockedCount(&m_RefCount)); };
63 
operator ++()64 	RefCount SAL_CALL operator++() 	 { return acquire(); }
65 	// don't implement the postfix operator, it won't function this way!
66 
operator --()67 	RefCount SAL_CALL operator--() 	 { return release(); }
68 	// don't implement the postfix operator, it won't function this way!
69 
referenced() const70 	RefCount SAL_CALL referenced() const
71 		{ return (m_RefCount); }
72 
73 protected:
74 	RefCount m_RefCount;
75 
76 private:
77 	// disable copy/assignment
78 	ORefCount(const ORefCount&);
79 	ORefCount& SAL_CALL operator= (const ORefCount&);
80 };
81 
82 class VOS_DLLPUBLIC OReference : public vos::IReference
83 {
84 public:
85 	OReference();
86 	virtual ~OReference();
87 
88 	virtual RefCount SAL_CALL acquire();
89 	virtual RefCount SAL_CALL release();
90 
referenced() const91 	virtual RefCount SAL_CALL referenced() const
92 		{ return (m_RefCount.referenced()); }
93 
94 protected:
95 	ORefCount m_RefCount;
96 
97 private:
98 	// disable copy/assignment
99 	OReference(const OReference&);
100 	OReference& SAL_CALL operator= (const OReference&);
101 };
102 
103 }
104 
105 #endif	// _VOS_REFERNCE_HXX_
106 
107