-
Notifications
You must be signed in to change notification settings - Fork 5
/
releaser.go
51 lines (41 loc) · 835 Bytes
/
releaser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package util
import (
"errors"
)
var (
ErrReleased = errors.New("fbase/util.Releaser: resource already relesed")
ErrHasReleaser = errors.New("fbase/util.Releaser: already defined")
)
type Releaser interface {
Release()
}
type ReleaseSetter interface {
SetReleaser(releaser Releaser)
}
type BasicReleaser struct {
releaser Releaser
released bool
}
func (r *BasicReleaser) Released() bool {
return r.released
}
func (r *BasicReleaser) Release() {
if !r.released {
if r.releaser != nil {
r.releaser.Release()
r.releaser = nil
}
r.released = true
}
}
func (r *BasicReleaser) SetReleaser(releaser Releaser) {
if r.released {
panic(ErrReleased)
}
if r.releaser != nil && releaser != nil {
panic(ErrHasReleaser)
}
r.releaser = releaser
}
type NoopReleaser struct{}
func (NoopReleaser) Release() {}