Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_SALHELPER_REFOBJ_HXX
21 : #define INCLUDED_SALHELPER_REFOBJ_HXX
22 :
23 : #include <sal/types.h>
24 : #include <rtl/alloc.h>
25 : #include <rtl/ref.hxx>
26 : #include <osl/diagnose.h>
27 : #include <osl/interlck.h>
28 :
29 : namespace salhelper
30 : {
31 :
32 :
33 :
34 : class ReferenceObject
35 : {
36 : /** Representation.
37 : */
38 : oslInterlockedCount m_nReferenceCount;
39 :
40 : /** Not implemented.
41 : */
42 : ReferenceObject (const ReferenceObject&);
43 : ReferenceObject& operator= (const ReferenceObject&);
44 :
45 : public:
46 : /** Allocation.
47 : */
48 0 : static void* operator new (size_t n)
49 : {
50 0 : return ::rtl_allocateMemory (n);
51 : }
52 0 : static void operator delete (void* p)
53 : {
54 0 : ::rtl_freeMemory (p);
55 0 : }
56 : static void* operator new (size_t, void* p)
57 : {
58 : return (p);
59 : }
60 : static void operator delete (void*, void*)
61 : {}
62 :
63 : public:
64 : /** Construction.
65 : */
66 0 : inline ReferenceObject() : m_nReferenceCount(0)
67 0 : {}
68 :
69 :
70 0 : void SAL_CALL acquire()
71 : {
72 0 : osl_atomic_increment(&m_nReferenceCount);
73 0 : }
74 :
75 0 : void SAL_CALL release()
76 : {
77 0 : if (osl_atomic_decrement(&m_nReferenceCount) == 0)
78 : {
79 : // Last reference released.
80 0 : delete this;
81 : }
82 0 : }
83 :
84 : protected:
85 : /** Destruction.
86 : */
87 0 : virtual ~ReferenceObject()
88 0 : {
89 : OSL_ASSERT(m_nReferenceCount == 0);
90 0 : }
91 : };
92 :
93 :
94 :
95 : } // namespace salhelper
96 :
97 : #endif /* ! INCLUDED_SALHELPER_REFOBJ_HXX */
98 :
99 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|