xref: /AOO41X/main/salhelper/inc/salhelper/refobj.hxx (revision 8a25e0a89046e766b2f8dcc3b5f33bc291c2c738)
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 _SALHELPER_REFOBJ_HXX_
25 #define _SALHELPER_REFOBJ_HXX_
26 
27 #include <sal/types.h>
28 #include <rtl/alloc.h>
29 #include <rtl/ref.hxx>
30 #include <osl/diagnose.h>
31 #include <osl/interlck.h>
32 
33 namespace salhelper
34 {
35 
36 //----------------------------------------------------------------------------
37 
38 class ReferenceObject : public rtl::IReference
39 {
40     /** Representation.
41      */
42     oslInterlockedCount m_nReferenceCount;
43 
44     /** Not implemented.
45      */
46     ReferenceObject (const ReferenceObject&);
47     ReferenceObject& operator= (const ReferenceObject&);
48 
49 public:
50     /** Allocation.
51      */
operator new(size_t n)52     static void* operator new (size_t n) SAL_THROW(())
53     {
54         return ::rtl_allocateMemory (n);
55     }
operator delete(void * p)56     static void operator delete (void* p) SAL_THROW(())
57     {
58         ::rtl_freeMemory (p);
59     }
operator new(size_t,void * p)60     static void* operator new (size_t, void* p) SAL_THROW(())
61     {
62         return (p);
63     }
operator delete(void *,void *)64     static void operator delete (void*, void*) SAL_THROW(())
65     {}
66 
67 public:
68     /** Construction.
69      */
70     inline ReferenceObject() SAL_THROW(()) : m_nReferenceCount (0)
71     {}
72 
73 
74     /** IReference.
75      */
acquire()76     virtual oslInterlockedCount SAL_CALL acquire() SAL_THROW(())
77     {
78         return ::osl_incrementInterlockedCount (&m_nReferenceCount);
79     }
80 
release()81     virtual oslInterlockedCount SAL_CALL release() SAL_THROW(())
82     {
83         oslInterlockedCount result;
84         result = ::osl_decrementInterlockedCount (&m_nReferenceCount);
85         if (result == 0)
86         {
87             // Last reference released.
88             delete this;
89         }
90         return (result);
91     }
92 
93 protected:
94     /** Destruction.
95      */
96     virtual ~ReferenceObject() SAL_THROW(())
97     {
98         OSL_ASSERT(m_nReferenceCount == 0);
99     }
100 };
101 
102 //----------------------------------------------------------------------------
103 
104 } // namespace salhelper
105 
106 #endif /* !_SALHELPER_REFOBJ_HXX_ */
107