NEURON
resource.cpp
Go to the documentation of this file.
1
#ifdef HAVE_CONFIG_H
2
#include <../../nrnconf.h>
3
#endif
4
/*
5
* Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
6
* Copyright (c) 1991 Silicon Graphics, Inc.
7
*
8
* Permission to use, copy, modify, distribute, and sell this software and
9
* its documentation for any purpose is hereby granted without fee, provided
10
* that (i) the above copyright notices and this permission notice appear in
11
* all copies of the software and related documentation, and (ii) the names of
12
* Stanford and Silicon Graphics may not be used in any advertising or
13
* publicity relating to the software without the specific, prior written
14
* permission of Stanford and Silicon Graphics.
15
*
16
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19
*
20
* IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
21
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25
* OF THIS SOFTWARE.
26
*/
27
28
#include <
InterViews/resource.h
>
29
#include <
OS/list.h
>
30
31
declarePtrList
(ResourceList,
Resource
)
32
implementPtrList
(ResourceList,
Resource
)
33
34
class
ResourceImpl
{
35
friend
class
Resource
;
36
37
static
bool
deferred_
;
38
static
ResourceList*
deletes_
;
39
};
40
41
bool
ResourceImpl::deferred_
=
false
;
42
ResourceList*
ResourceImpl::deletes_
;
43
44
Resource::Resource
() {
refcount_
= 0; }
45
Resource::~Resource
() { }
46
47
void
Resource::ref
()
const
{
48
Resource
* r = (
Resource
*)
this
;
49
r->
refcount_
+= 1;
50
}
51
52
void
Resource::unref
()
const
{
53
Resource
* r = (
Resource
*)
this
;
54
if
(r->
refcount_
!= 0) {
55
r->
refcount_
-= 1;
56
}
57
if
(r->
refcount_
== 0) {
58
r->
cleanup
();
59
delete
r;
60
}
61
}
62
63
void
Resource::unref_deferred
()
const
{
64
Resource
* r = (
Resource
*)
this
;
65
if
(r->
refcount_
!= 0) {
66
r->
refcount_
-= 1;
67
}
68
if
(r->
refcount_
== 0) {
69
r->
cleanup
();
70
if
(
ResourceImpl::deferred_
) {
71
if
(
ResourceImpl::deletes_
==
nil
) {
72
ResourceImpl::deletes_
=
new
ResourceList;
73
}
74
ResourceImpl::deletes_
->append(r);
75
}
else
{
76
delete
r;
77
}
78
}
79
}
80
81
void
Resource::cleanup
() { }
82
83
void
Resource::ref
(
const
Resource
* r) {
84
if
(r !=
nil
) {
85
r->
ref
();
86
}
87
}
88
89
void
Resource::unref
(
const
Resource
* r) {
90
if
(r !=
nil
) {
91
r->
unref
();
92
}
93
}
94
95
void
Resource::unref_deferred
(
const
Resource
* r) {
96
if
(r !=
nil
) {
97
r->
unref_deferred
();
98
}
99
}
100
101
bool
Resource::defer
(
bool
b) {
102
bool
previous =
ResourceImpl::deferred_
;
103
if
(b != previous) {
104
flush
();
105
ResourceImpl::deferred_
= b;
106
}
107
return
previous;
108
}
109
110
void
Resource::flush
() {
111
ResourceList* list =
ResourceImpl::deletes_
;
112
if
(list !=
nil
) {
113
bool
previous =
ResourceImpl::deferred_
;
114
ResourceImpl::deferred_
=
false
;
115
for
(
ListItr
(ResourceList)
i
(*list);
i
.more();
i
.next()) {
116
Resource
* r =
i
.cur();
117
delete
r;
118
}
119
list->remove_all();
120
ResourceImpl::deferred_
= previous;
121
}
122
}
nil
#define nil
Definition:
enter-scope.h:36
Resource
Definition:
resource.h:34
Resource::~Resource
virtual ~Resource()
Definition:
resource.cpp:45
Resource::ref
virtual void ref() const
Definition:
resource.cpp:47
Resource::cleanup
virtual void cleanup()
Definition:
resource.cpp:81
Resource::Resource
Resource()
Definition:
resource.cpp:44
Resource::refcount_
unsigned refcount_
Definition:
resource.h:57
Resource::defer
static bool defer(bool)
Definition:
resource.cpp:101
Resource::unref
virtual void unref() const
Definition:
resource.cpp:52
Resource::unref_deferred
virtual void unref_deferred() const
Definition:
resource.cpp:63
Resource::flush
static void flush()
Definition:
resource.cpp:110
ResourceImpl
Definition:
resource.cpp:34
ResourceImpl::deletes_
static ResourceList * deletes_
Definition:
resource.cpp:38
ResourceImpl::deferred_
static bool deferred_
Definition:
resource.cpp:37
list.h
implementPtrList
#define implementPtrList(PtrList, T)
ListItr
#define ListItr(List)
Definition:
list.h:39
i
#define i
Definition:
md1redef.h:12
declarePtrList
declarePtrList(ResourceList, Resource) implementPtrList(ResourceList
resource.h
src
ivos
resource.cpp