LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
refobj.hxx
Go to the documentation of this file.
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 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_SALHELPER_REFOBJ_HXX
25 #define INCLUDED_SALHELPER_REFOBJ_HXX
26 
27 #include <cassert>
28 
29 #include "sal/types.h"
30 #include "rtl/alloc.h"
31 #include "osl/interlck.h"
32 
33 namespace salhelper
34 {
35 
41 {
44  oslInterlockedCount m_nReferenceCount;
45 
48 
49 public:
52  static void* operator new (size_t n)
53  {
55  }
56  static void operator delete (void* p)
57  {
58  ::rtl_freeMemory (p);
59  }
60  static void* operator new (size_t, void* p)
61  {
62  return p;
63  }
64  static void operator delete (void*, void*)
65  {}
66 
67 public:
70  ReferenceObject() : m_nReferenceCount(0)
71  {}
72 
73 
74  void SAL_CALL acquire()
75  {
76  osl_atomic_increment(&m_nReferenceCount);
77  }
78 
79  void SAL_CALL release()
80  {
81  if (osl_atomic_decrement(&m_nReferenceCount) == 0)
82  {
83  // Last reference released.
84  delete this;
85  }
86  }
87 
88 protected:
91  virtual ~ReferenceObject()
92  {
93  assert(m_nReferenceCount == 0);
94  }
95 };
96 
97 
98 } // namespace salhelper
99 
100 #endif /* ! INCLUDED_SALHELPER_REFOBJ_HXX */
101 
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~ReferenceObject()
Destruction.
Definition: refobj.hxx:91
void release()
Definition: refobj.hxx:79
ReferenceObject()
Construction.
Definition: refobj.hxx:70
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
A base implementation for reference-counted objects.
Definition: refobj.hxx:40
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
void acquire()
Definition: refobj.hxx:74
sal_Int32 oslInterlockedCount
Definition: interlck.h:44
Definition: condition.hxx:33
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.