blob: 87fcedd40af7bb2c465922d9183f033835cbe5ea [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2004-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39#include <cc++/config.h>
40#include <cc++/export.h>
41#include <cc++/thread.h>
42#include <cc++/object.h>
43#include "private.h"
44
45#ifdef CCXX_NAMESPACES
46namespace ost {
47#endif
48
49RefObject::~RefObject() {}
50
51void RefPointer::enterLock(void) {}
52
53void RefPointer::leaveLock(void) {}
54
55void RefPointer::detach(void)
56{
57 if(ref) {
58 enterLock();
59 --(ref->refCount);
60 if(!ref->refCount)
61 delete ref;
62 leaveLock();
63 ref = NULL;
64 }
65}
66
67RefPointer::RefPointer(RefObject *obj)
68{
69 enterLock();
70 ++obj->refCount;
71 leaveLock();
72 ref = obj;
73}
74
75RefPointer::RefPointer(const RefPointer &ptr)
76{
77 detach();
78 ref = ptr.ref;
79 if(ref) {
80 enterLock();
81 ++ref->refCount;
82 leaveLock();
83 }
84}
85
86RefPointer::~RefPointer()
87{
88 detach();
89}
90
91void *RefPointer::getObject() const
92{
93 if(ref)
94 return ref->getObject();
95
96 return NULL;
97}
98
99bool RefPointer::operator!() const
100{
101 if(ref && ref->refCount == 1)
102 return true;
103
104 return false;
105}
106
107#ifdef CCXX_NAMESPACES
108}
109#endif
110
111/** EMACS **
112 * Local variables:
113 * mode: c++
114 * c-basic-offset: 4
115 * End:
116 */