go-hep.org/x/hep@v0.38.1/groot/rbase/objstring.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rbase 6 7 import ( 8 "reflect" 9 10 "go-hep.org/x/hep/groot/rbytes" 11 "go-hep.org/x/hep/groot/root" 12 "go-hep.org/x/hep/groot/rtypes" 13 "go-hep.org/x/hep/groot/rvers" 14 ) 15 16 type ObjString struct { 17 obj Object 18 str string 19 } 20 21 // NewObjString creates a new ObjString. 22 func NewObjString(s string) *ObjString { 23 return &ObjString{ 24 obj: *NewObject(), 25 str: s, 26 } 27 } 28 29 func (*ObjString) RVersion() int16 { 30 return rvers.ObjString 31 } 32 33 func (*ObjString) Class() string { 34 return "TObjString" 35 } 36 37 func (obj *ObjString) UID() uint32 { 38 return obj.obj.UID() 39 } 40 41 func (obj *ObjString) Name() string { 42 return obj.str 43 } 44 45 func (*ObjString) Title() string { 46 return "Collectable string class" 47 } 48 49 func (obj *ObjString) String() string { 50 return obj.str 51 } 52 53 // ROOTUnmarshaler is the interface implemented by an object that can 54 // unmarshal itself from a ROOT buffer 55 func (obj *ObjString) UnmarshalROOT(r *rbytes.RBuffer) error { 56 hdr := r.ReadHeader(obj.Class(), obj.RVersion()) 57 r.ReadObject(&obj.obj) 58 obj.str = r.ReadString() 59 60 r.CheckHeader(hdr) 61 return r.Err() 62 } 63 64 func (obj *ObjString) MarshalROOT(w *rbytes.WBuffer) (int, error) { 65 if w.Err() != nil { 66 return 0, w.Err() 67 } 68 69 hdr := w.WriteHeader(obj.Class(), obj.RVersion()) 70 w.WriteObject(&obj.obj) 71 w.WriteString(obj.str) 72 return w.SetHeader(hdr) 73 } 74 75 func init() { 76 f := func() reflect.Value { 77 o := &ObjString{} 78 return reflect.ValueOf(o) 79 } 80 rtypes.Factory.Add("TObjString", f) 81 } 82 83 var ( 84 _ root.Object = (*ObjString)(nil) 85 _ root.UIDer = (*ObjString)(nil) 86 _ root.Named = (*ObjString)(nil) 87 _ root.ObjString = (*ObjString)(nil) 88 _ rbytes.Marshaler = (*ObjString)(nil) 89 _ rbytes.Unmarshaler = (*ObjString)(nil) 90 )