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 <vector>
29
30
#include <
InterViews/resource.h
>
31
32
class
ResourceImpl
{
33
friend
class
Resource
;
34
35
static
bool
deferred_
;
36
static
std::vector<Resource*>
deletes_
;
37
};
38
39
bool
ResourceImpl::deferred_
=
false
;
40
std::vector<Resource*>
ResourceImpl::deletes_
;
41
42
void
Resource::ref
()
const
{
43
Resource
* r = (
Resource
*)
this
;
44
r->
refcount_
+= 1;
45
}
46
47
void
Resource::unref
()
const
{
48
Resource
* r = (
Resource
*)
this
;
49
if
(r->
refcount_
!= 0) {
50
r->
refcount_
-= 1;
51
}
52
if
(r->
refcount_
== 0) {
53
r->
cleanup
();
54
delete
r;
55
}
56
}
57
58
void
Resource::unref_deferred
()
const
{
59
Resource
* r = (
Resource
*)
this
;
60
if
(r->
refcount_
!= 0) {
61
r->
refcount_
-= 1;
62
}
63
if
(r->
refcount_
== 0) {
64
r->
cleanup
();
65
if
(
ResourceImpl::deferred_
) {
66
ResourceImpl::deletes_
.push_back(r);
67
}
else
{
68
delete
r;
69
}
70
}
71
}
72
73
void
Resource::cleanup
() { }
74
75
void
Resource::ref
(
const
Resource
* r) {
76
if
(r !=
nil
) {
77
r->
ref
();
78
}
79
}
80
81
void
Resource::unref
(
const
Resource
* r) {
82
if
(r !=
nil
) {
83
r->
unref
();
84
}
85
}
86
87
void
Resource::unref_deferred
(
const
Resource
* r) {
88
if
(r !=
nil
) {
89
r->
unref_deferred
();
90
}
91
}
92
93
bool
Resource::defer
(
bool
b) {
94
bool
previous =
ResourceImpl::deferred_
;
95
if
(b != previous) {
96
flush
();
97
ResourceImpl::deferred_
= b;
98
}
99
return
previous;
100
}
101
102
void
Resource::flush
() {
103
bool
previous =
ResourceImpl::deferred_
;
104
ResourceImpl::deferred_
=
false
;
105
for
(
auto
& r:
ResourceImpl::deletes_
) {
106
delete
r;
107
}
108
ResourceImpl::deletes_
.clear();
109
ResourceImpl::deletes_
.shrink_to_fit();
110
ResourceImpl::deferred_
= previous;
111
}
nil
#define nil
Definition:
enter-scope.h:35
Resource
Definition:
resource.h:33
Resource::ref
virtual void ref() const
Definition:
resource.cpp:42
Resource::cleanup
virtual void cleanup()
Definition:
resource.cpp:73
Resource::refcount_
unsigned refcount_
Definition:
resource.h:56
Resource::defer
static bool defer(bool)
Definition:
resource.cpp:93
Resource::unref
virtual void unref() const
Definition:
resource.cpp:47
Resource::unref_deferred
virtual void unref_deferred() const
Definition:
resource.cpp:58
Resource::flush
static void flush()
Definition:
resource.cpp:102
ResourceImpl
Definition:
resource.cpp:32
ResourceImpl::deferred_
static bool deferred_
Definition:
resource.cpp:35
ResourceImpl::deletes_
static std::vector< Resource * > deletes_
Definition:
resource.cpp:36
resource.h
src
ivos
resource.cpp