/
named.go
119 lines (97 loc) · 2.44 KB
/
named.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
117
118
119
// Copyright ©2017 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"
"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"
)
// The TNamed class is the base class for all named ROOT classes
// A TNamed contains the essential elements (name, title)
// to identify a derived object in containers, directories and files.
// Most member functions defined in this base class are in general
// overridden by the derived classes.
type Named struct {
obj Object
name string
title string
}
func NewNamed(name, title string) *Named {
return &Named{
obj: *NewObject(),
name: name,
title: title,
}
}
func (*Named) RVersion() int16 {
return rvers.Named
}
// Name returns the name of the instance
func (n *Named) Name() string {
return n.name
}
// Title returns the title of the instance
func (n *Named) Title() string {
return n.title
}
func (n *Named) SetName(name string) { n.name = name }
func (n *Named) SetTitle(title string) { n.title = title }
func (*Named) Class() string {
return "TNamed"
}
func (n *Named) UID() uint32 {
return n.obj.UID()
}
func (n *Named) Sizeof() int32 {
return tstringSizeof(n.name) + tstringSizeof(n.title)
}
// tstringSizeof returns the size in bytes of the TString structure.
func tstringSizeof(v string) int32 {
n := int32(len(v))
if n > 254 {
return n + 1 + 4
}
return n + 1
}
func (n *Named) UnmarshalROOT(r *rbytes.RBuffer) error {
if r.Err() != nil {
return r.Err()
}
hdr := r.ReadHeader(n.Class())
if hdr.Vers > rvers.Named {
panic(fmt.Errorf("rbase: invalid named version=%d > %d", hdr.Vers, rvers.Named))
}
r.ReadObject(&n.obj)
n.name = r.ReadString()
n.title = r.ReadString()
r.CheckHeader(hdr)
return r.Err()
}
func (n *Named) MarshalROOT(w *rbytes.WBuffer) (int, error) {
if w.Err() != nil {
return 0, w.Err()
}
hdr := w.WriteHeader(n.Class(), n.RVersion())
w.WriteObject(&n.obj)
w.WriteString(n.name)
w.WriteString(n.title)
return w.SetHeader(hdr)
}
func init() {
f := func() reflect.Value {
o := NewNamed("", "")
return reflect.ValueOf(o)
}
rtypes.Factory.Add("TNamed", f)
}
var (
_ root.Object = (*Named)(nil)
_ root.UIDer = (*Named)(nil)
_ root.Named = (*Named)(nil)
_ rbytes.Marshaler = (*Named)(nil)
_ rbytes.Unmarshaler = (*Named)(nil)
)