xref: /aoo41x/main/vos/inc/vos/refernce.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _VOS_REFERNCE_HXX_
29 #define _VOS_REFERNCE_HXX_
30 
31 #	include <vos/types.hxx>
32 #	include <osl/interlck.h>
33 #	include <vos/object.hxx>
34 
35 namespace vos
36 {
37 
38 /** Interface for refernce-counting
39 */
40 class IReference
41 {
42 public:
43 
44     IReference() { }
45     virtual ~IReference() { }
46 
47 	typedef oslInterlockedCount RefCount;
48 
49 	virtual RefCount SAL_CALL acquire()=0;
50 	virtual RefCount SAL_CALL release()=0;
51 
52 	virtual RefCount SAL_CALL referenced() const=0;
53 };
54 
55 class ORefCount
56 {
57 public:
58 	typedef IReference::RefCount RefCount;
59 
60 	ORefCount() { m_RefCount = 0; }
61 	ORefCount(RefCount n) { m_RefCount = n; }
62     virtual ~ORefCount();
63 
64 	RefCount SAL_CALL acquire() { return (osl_incrementInterlockedCount(&m_RefCount)); };
65 	RefCount SAL_CALL release() { return (osl_decrementInterlockedCount(&m_RefCount)); };
66 
67 	RefCount SAL_CALL operator++() 	 { return acquire(); }
68 	// don't implement the postfix operator, it won't function this way!
69 
70 	RefCount SAL_CALL operator--() 	 { return release(); }
71 	// don't implement the postfix operator, it won't function this way!
72 
73 	RefCount SAL_CALL referenced() const
74 		{ return (m_RefCount); }
75 
76 protected:
77 	RefCount m_RefCount;
78 
79 private:
80 	// disable copy/assignment
81 	ORefCount(const ORefCount&);
82 	ORefCount& SAL_CALL operator= (const ORefCount&);
83 };
84 
85 class OReference : public vos::IReference
86 {
87 public:
88 	OReference();
89 	virtual ~OReference();
90 
91 	virtual RefCount SAL_CALL acquire();
92 	virtual RefCount SAL_CALL release();
93 
94 	virtual RefCount SAL_CALL referenced() const
95 		{ return (m_RefCount.referenced()); }
96 
97 protected:
98 	ORefCount m_RefCount;
99 
100 private:
101 	// disable copy/assignment
102 	OReference(const OReference&);
103 	OReference& SAL_CALL operator= (const OReference&);
104 };
105 
106 }
107 
108 #endif	// _VOS_REFERNCE_HXX_
109 
110