/
ref.go
116 lines (95 loc) · 2.19 KB
/
ref.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright ©2020 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rbase
import (
"fmt"
"reflect"
"github.com/hashicorp/go-uuid"
"go-hep.org/x/hep/groot/rbytes"
"go-hep.org/x/hep/groot/root"
"go-hep.org/x/hep/groot/rtypes"
"go-hep.org/x/hep/groot/rvers"
)
var (
gPID = ProcessID{
named: *NewNamed("ProcessID0", mustGenUUID()),
objs: make(map[uint32]root.Object),
}
)
func mustGenUUID() string {
id, err := uuid.GenerateUUID()
if err != nil {
panic(fmt.Errorf("groot/rbase: could not generate UUID: %+v", err))
}
return id
}
// Ref implements a persistent link to a root.Object.
type Ref struct {
obj Object
pid *ProcessID
}
func (*Ref) RVersion() int16 {
return rvers.Ref
}
func (*Ref) Class() string { return "TRef" }
func (*Ref) Name() string { return "TRef" }
func (*Ref) Title() string { return "Persistent Reference link to a TObject" }
func (ref *Ref) UID() uint32 {
return ref.obj.UID()
}
func (ref *Ref) String() string {
return fmt.Sprintf("Ref{id:%d}", ref.obj.ID)
}
// Object returns the root.Object being referenced by this Ref.
func (ref *Ref) Object() root.Object {
uid := ref.UID()
if uid == 0 {
return nil
}
obj, ok := ref.pid.objs[uid]
if !ok {
return nil
}
return obj
}
func (ref *Ref) UnmarshalROOT(r *rbytes.RBuffer) error {
if r.Err() != nil {
return r.Err()
}
r.ReadObject(&ref.obj)
switch {
case ref.obj.TestBits(kHasUUID):
_ = r.ReadString() // UUID string
default:
_ = r.ReadU16() // pid
}
return nil
}
func (ref *Ref) MarshalROOT(w *rbytes.WBuffer) (int, error) {
if w.Err() != nil {
return 0, w.Err()
}
beg := w.Pos()
w.WriteObject(&ref.obj)
switch {
case ref.obj.TestBits(kHasUUID):
panic("rbase: TRef with UUID not supported")
default:
w.WriteU16(uint16(ref.UID())) // FIXME(sbinet): this should go thru TFile.
}
return int(w.Pos() - beg), w.Err()
}
func init() {
f := func() reflect.Value {
o := &Ref{}
return reflect.ValueOf(o)
}
rtypes.Factory.Add("TRef", f)
}
var (
_ root.Object = (*Ref)(nil)
_ root.Named = (*Ref)(nil)
_ rbytes.Marshaler = (*Ref)(nil)
_ rbytes.Unmarshaler = (*Ref)(nil)
)