go-hep.org/x/hep@v0.38.1/groot/rcont/map.go (about)

     1  // Copyright ©2019 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 rcont
     6  
     7  import (
     8  	"reflect"
     9  
    10  	"go-hep.org/x/hep/groot/rbase"
    11  	"go-hep.org/x/hep/groot/rbytes"
    12  	"go-hep.org/x/hep/groot/root"
    13  	"go-hep.org/x/hep/groot/rtypes"
    14  	"go-hep.org/x/hep/groot/rvers"
    15  )
    16  
    17  // Map is a ROOT associative array of (key,value) pairs.
    18  // Keys and values must implement the root.Object interface.
    19  type Map struct {
    20  	obj  rbase.Object
    21  	name string
    22  	tbl  map[root.Object]root.Object
    23  }
    24  
    25  func NewMap() *Map {
    26  	return &Map{
    27  		obj:  *rbase.NewObject(),
    28  		name: "TMap",
    29  		tbl:  make(map[root.Object]root.Object),
    30  	}
    31  }
    32  
    33  func (*Map) RVersion() int16 { return rvers.Map }
    34  func (*Map) Class() string   { return "TMap" }
    35  func (m *Map) UID() uint32   { return m.obj.UID() }
    36  
    37  func (m *Map) Name() string        { return m.name }
    38  func (m *Map) Title() string       { return "A (key,value) map" }
    39  func (m *Map) SetName(name string) { m.name = name }
    40  
    41  // Table returns the underlying hash table.
    42  func (m *Map) Table() map[root.Object]root.Object { return m.tbl }
    43  
    44  // ROOTMarshaler is the interface implemented by an object that can
    45  // marshal itself to a ROOT buffer
    46  func (m *Map) MarshalROOT(w *rbytes.WBuffer) (int, error) {
    47  	if w.Err() != nil {
    48  		return 0, w.Err()
    49  	}
    50  
    51  	hdr := w.WriteHeader(m.Class(), m.RVersion())
    52  	w.WriteObject(&m.obj)
    53  	w.WriteString(m.name)
    54  
    55  	w.WriteI32(int32(len(m.tbl)))
    56  
    57  	for k, v := range m.tbl {
    58  		w.WriteObjectAny(k)
    59  		w.WriteObjectAny(v)
    60  	}
    61  
    62  	return w.SetHeader(hdr)
    63  }
    64  
    65  // ROOTUnmarshaler is the interface implemented by an object that can
    66  // unmarshal itself from a ROOT buffer
    67  func (m *Map) UnmarshalROOT(r *rbytes.RBuffer) error {
    68  	if r.Err() != nil {
    69  		return r.Err()
    70  	}
    71  
    72  	hdr := r.ReadHeader(m.Class(), m.RVersion())
    73  	if hdr.Vers > 2 {
    74  		r.ReadObject(&m.obj)
    75  	}
    76  	if hdr.Vers > 1 {
    77  		m.name = r.ReadString()
    78  	}
    79  
    80  	nobjs := int(r.ReadI32())
    81  	m.tbl = make(map[root.Object]root.Object, nobjs)
    82  	for range nobjs {
    83  		k := r.ReadObjectAny()
    84  		if r.Err() != nil {
    85  			return r.Err()
    86  		}
    87  		v := r.ReadObjectAny()
    88  		if r.Err() != nil {
    89  			return r.Err()
    90  		}
    91  		if k != nil {
    92  			m.tbl[k] = v
    93  		}
    94  	}
    95  
    96  	r.CheckHeader(hdr)
    97  	return r.Err()
    98  }
    99  
   100  func init() {
   101  	f := func() reflect.Value {
   102  		o := NewMap()
   103  		return reflect.ValueOf(o)
   104  	}
   105  	rtypes.Factory.Add("TMap", f)
   106  }
   107  
   108  var (
   109  	_ root.Object        = (*Map)(nil)
   110  	_ root.UIDer         = (*Map)(nil)
   111  	_ root.Named         = (*Map)(nil)
   112  	_ rbytes.Marshaler   = (*Map)(nil)
   113  	_ rbytes.Unmarshaler = (*Map)(nil)
   114  )