go-hep.org/x/hep@v0.38.1/groot/rbase/string.go (about) 1 // Copyright ©2024 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 ) 14 15 // String is a bare-bone string value implementing root.Object 16 type String struct { 17 str string 18 } 19 20 func NewString(v string) *String { 21 return &String{v} 22 } 23 24 func (*String) Class() string { 25 return "*rbase.String" 26 } 27 28 func (*String) RVersion() int16 { 29 return 0 30 } 31 32 func (v *String) String() string { 33 return v.str 34 } 35 36 func (obj *String) UnmarshalROOT(r *rbytes.RBuffer) error { 37 obj.str = r.ReadString() 38 return r.Err() 39 } 40 41 func (obj *String) MarshalROOT(w *rbytes.WBuffer) (int, error) { 42 if w.Err() != nil { 43 return 0, w.Err() 44 } 45 46 pos := w.Pos() 47 w.WriteString(obj.str) 48 end := w.Pos() 49 return int(end - pos), w.Err() 50 } 51 52 func init() { 53 f := func() reflect.Value { 54 o := &String{} 55 return reflect.ValueOf(o) 56 } 57 rtypes.Factory.Add("*rbase.String", f) 58 } 59 60 var ( 61 _ root.Object = (*String)(nil) 62 _ rbytes.Marshaler = (*String)(nil) 63 _ rbytes.Unmarshaler = (*String)(nil) 64 )