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_SIMPLEREFERENCEOBJECT_HXX
21 : #define INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
22 :
23 : #include <osl/interlck.h>
24 : #include <salhelper/salhelperdllapi.h>
25 :
26 : #include <cstddef>
27 : #include <new>
28 :
29 : namespace salhelper {
30 :
31 : /** A simple base implementation for reference-counted objects.
32 :
33 : Classes that want to implement a reference-counting mechanism based on the
34 : acquire()/release() interface should derive from this class.
35 :
36 : The reason to have class local operators new and delete here is technical.
37 : Imagine a class D derived from SimpleReferenceObject, but implemented in
38 : another shared library that happens to use different global operators new
39 : and delete from those used in this shared library (which, sadly, seems to
40 : be possible with shared libraries). Now, without the class local
41 : operators new and delete here, a code sequence like "new D" would use the
42 : global operator new as found in the other shared library, while the code
43 : sequence "delete this" in release() would use the global operator delete
44 : as found in this shared library---and these two operators would not be
45 : guaranteed to match.
46 :
47 : There are no overloaded operators new and delete for placement new here,
48 : because it is felt that the concept of placement new does not work well
49 : with the concept of reference-counted objects; so it seems best to simply
50 : leave those operators out.
51 :
52 : The same problem as with operators new and delete would also be there with
53 : operators new[] and delete[]. But since arrays of reference-counted
54 : objects are of no use, anyway, it seems best to simply declare and not
55 : define (private) operators new[] and delete[].
56 : */
57 : class SALHELPER_DLLPUBLIC SimpleReferenceObject
58 : {
59 : public:
60 13986380 : inline SimpleReferenceObject(): m_nCount(0) {}
61 :
62 : /** @attention
63 : The results are undefined if, for any individual instance of
64 : SimpleReferenceObject, the total number of calls to acquire() exceeds
65 : the total number of calls to release() by a platform dependent amount
66 : (which, hopefully, is quite large).
67 : */
68 940614386 : inline void acquire()
69 940614386 : { osl_atomic_increment(&m_nCount); }
70 :
71 940519946 : inline void release()
72 940519946 : { if (osl_atomic_decrement(&m_nCount) == 0) delete this; }
73 :
74 : /** see general class documentation
75 : */
76 : static void * operator new(std::size_t nSize);
77 :
78 : /** see general class documentation
79 : */
80 : static void * operator new(std::size_t nSize,
81 : std::nothrow_t const & rNothrow);
82 :
83 : /** see general class documentation
84 : */
85 : static void operator delete(void * pPtr);
86 :
87 : /** see general class documentation
88 : */
89 : static void operator delete(void * pPtr, std::nothrow_t const & rNothrow);
90 :
91 : protected:
92 : virtual ~SimpleReferenceObject();
93 :
94 : private:
95 : oslInterlockedCount m_nCount;
96 :
97 : /** not implemented
98 : */
99 : SimpleReferenceObject(SimpleReferenceObject &) SAL_DELETED_FUNCTION;
100 :
101 : /** not implemented
102 : */
103 : void operator =(SimpleReferenceObject) SAL_DELETED_FUNCTION;
104 :
105 : /// @cond INTERNAL
106 :
107 : #ifdef _MSC_VER
108 : /* We can't now have these private with MSVC2008 at least, it leads to
109 : compilation errors in xmloff and other places.
110 : */
111 : protected:
112 : #endif
113 : /** not implemented (see general class documentation)
114 : */
115 : static void * operator new[](std::size_t);
116 :
117 : /** not implemented (see general class documentation)
118 : */
119 : static void operator delete[](void * pPtr);
120 :
121 : /// @endcond
122 : };
123 :
124 : }
125 :
126 : #endif // INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
127 :
128 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|