My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _SALHELPER_REFOBJ_HXX_
30 #define _SALHELPER_REFOBJ_HXX_
31 
32 #include <sal/types.h>
33 #include <rtl/alloc.h>
34 #include <rtl/ref.hxx>
35 #include <osl/diagnose.h>
36 #include <osl/interlck.h>
37 
38 namespace salhelper
39 {
40 
41 //----------------------------------------------------------------------------
42 
44 {
47  oslInterlockedCount m_nReferenceCount;
48 
52  ReferenceObject& operator= (const ReferenceObject&);
53 
54 public:
57  static void* operator new (size_t n) SAL_THROW(())
58  {
60  }
61  static void operator delete (void* p) SAL_THROW(())
62  {
63  ::rtl_freeMemory (p);
64  }
65  static void* operator new (size_t, void* p) SAL_THROW(())
66  {
67  return (p);
68  }
69  static void operator delete (void*, void*) SAL_THROW(())
70  {}
71 
72 public:
75  inline ReferenceObject() SAL_THROW(()) : m_nReferenceCount (0)
76  {}
77 
78 
81  virtual oslInterlockedCount SAL_CALL acquire() SAL_THROW(())
82  {
83  return ::osl_incrementInterlockedCount (&m_nReferenceCount);
84  }
85 
86  virtual oslInterlockedCount SAL_CALL release() SAL_THROW(())
87  {
88  oslInterlockedCount result;
89  result = ::osl_decrementInterlockedCount (&m_nReferenceCount);
90  if (result == 0)
91  {
92  // Last reference released.
93  delete this;
94  }
95  return (result);
96  }
97 
98 protected:
102  {
103  OSL_ASSERT(m_nReferenceCount == 0);
104  }
105 };
106 
107 //----------------------------------------------------------------------------
108 
109 } // namespace salhelper
110 
111 #endif /* !_SALHELPER_REFOBJ_HXX_ */
112 
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */