go-hep.org/x/hep@v0.38.1/groot/rbase/named.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  // The TNamed class is the base class for all named ROOT classes
    17  // A TNamed contains the essential elements (name, title)
    18  // to identify a derived object in containers, directories and files.
    19  // Most member functions defined in this base class are in general
    20  // overridden by the derived classes.
    21  type Named struct {
    22  	obj   Object
    23  	name  string
    24  	title string
    25  }
    26  
    27  func NewNamed(name, title string) *Named {
    28  	return &Named{
    29  		obj:   *NewObject(),
    30  		name:  name,
    31  		title: title,
    32  	}
    33  }
    34  
    35  func (*Named) RVersion() int16 {
    36  	return rvers.Named
    37  }
    38  
    39  // Name returns the name of the instance
    40  func (n *Named) Name() string {
    41  	return n.name
    42  }
    43  
    44  // Title returns the title of the instance
    45  func (n *Named) Title() string {
    46  	return n.title
    47  }
    48  
    49  func (n *Named) SetName(name string)   { n.name = name }
    50  func (n *Named) SetTitle(title string) { n.title = title }
    51  
    52  func (*Named) Class() string {
    53  	return "TNamed"
    54  }
    55  
    56  func (n *Named) UID() uint32 {
    57  	return n.obj.UID()
    58  }
    59  
    60  func (n *Named) Sizeof() int32 {
    61  	return tstringSizeof(n.name) + tstringSizeof(n.title)
    62  }
    63  
    64  // tstringSizeof returns the size in bytes of the TString structure.
    65  func tstringSizeof(v string) int32 {
    66  	n := int32(len(v))
    67  	if n > 254 {
    68  		return n + 1 + 4
    69  	}
    70  	return n + 1
    71  }
    72  
    73  func (n *Named) UnmarshalROOT(r *rbytes.RBuffer) error {
    74  	if r.Err() != nil {
    75  		return r.Err()
    76  	}
    77  
    78  	hdr := r.ReadHeader(n.Class(), n.RVersion())
    79  
    80  	r.ReadObject(&n.obj)
    81  
    82  	n.name = r.ReadString()
    83  	n.title = r.ReadString()
    84  
    85  	r.CheckHeader(hdr)
    86  	return r.Err()
    87  }
    88  
    89  func (n *Named) MarshalROOT(w *rbytes.WBuffer) (int, error) {
    90  	if w.Err() != nil {
    91  		return 0, w.Err()
    92  	}
    93  
    94  	hdr := w.WriteHeader(n.Class(), n.RVersion())
    95  	w.WriteObject(&n.obj)
    96  	w.WriteString(n.name)
    97  	w.WriteString(n.title)
    98  	return w.SetHeader(hdr)
    99  }
   100  
   101  func (n *Named) RMembers() []rbytes.Member {
   102  	o := n.obj.RMembers()
   103  	return append(o, []rbytes.Member{
   104  		{Name: "fName", Value: &n.name},
   105  		{Name: "fTitle", Value: &n.title},
   106  	}...)
   107  }
   108  
   109  func init() {
   110  	f := func() reflect.Value {
   111  		o := NewNamed("", "")
   112  		return reflect.ValueOf(o)
   113  	}
   114  	rtypes.Factory.Add("TNamed", f)
   115  }
   116  
   117  var (
   118  	_ root.Object        = (*Named)(nil)
   119  	_ root.UIDer         = (*Named)(nil)
   120  	_ root.Named         = (*Named)(nil)
   121  	_ rbytes.Marshaler   = (*Named)(nil)
   122  	_ rbytes.Unmarshaler = (*Named)(nil)
   123  )