1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
25 #define INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
27 #include "osl/interlck.h"
28 #include "salhelper/salhelperdllapi.h"
35 /** A simple base implementation for reference-counted objects.
37 Classes that want to implement a reference-counting mechanism based on the
38 acquire()/release() interface should derive from this class.
40 The reason to have class local operators new and delete here is technical.
41 Imagine a class D derived from SimpleReferenceObject, but implemented in
42 another shared library that happens to use different global operators new
43 and delete from those used in this shared library (which, sadly, seems to
44 be possible with shared libraries). Now, without the class local
45 operators new and delete here, a code sequence like "new D" would use the
46 global operator new as found in the other shared library, while the code
47 sequence "delete this" in release() would use the global operator delete
48 as found in this shared library---and these two operators would not be
51 There are no overloaded operators new and delete for placement new here,
52 because it is felt that the concept of placement new does not work well
53 with the concept of reference-counted objects; so it seems best to simply
54 leave those operators out.
56 The same problem as with operators new and delete would also be there with
57 operators new[] and delete[]. But since arrays of reference-counted
58 objects are of no use, anyway, it seems best to simply
59 define operators new[] and delete[] as deleted.
61 class SALHELPER_DLLPUBLIC SimpleReferenceObject
64 SimpleReferenceObject(): m_nCount(0) {}
67 The results are undefined if, for any individual instance of
68 SimpleReferenceObject, the total number of calls to acquire() exceeds
69 the total number of calls to release() by a platform dependent amount
70 (which, hopefully, is quite large).
73 { osl_atomic_increment(&m_nCount
); }
76 { if (osl_atomic_decrement(&m_nCount
) == 0) delete this; }
78 /** see general class documentation
80 static void * operator new(std::size_t nSize
);
82 /** see general class documentation
84 static void * operator new(std::size_t nSize
,
85 std::nothrow_t
const & rNothrow
);
87 /** see general class documentation
89 static void operator delete(void * pPtr
);
91 /** see general class documentation
93 static void operator delete(void * pPtr
, std::nothrow_t
const & rNothrow
);
96 virtual ~SimpleReferenceObject() COVERITY_NOEXCEPT_FALSE
;
98 oslInterlockedCount m_nCount
;
103 SimpleReferenceObject(SimpleReferenceObject
&) SAL_DELETED_FUNCTION
;
107 void operator =(SimpleReferenceObject
) SAL_DELETED_FUNCTION
;
109 /** see general class documentation
111 static void * operator new[](std::size_t) SAL_DELETED_FUNCTION
;
113 /** see general class documentation
115 static void operator delete[](void * pPtr
) SAL_DELETED_FUNCTION
;
120 #endif // INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */