go-hep.org/x/hep@v0.38.1/groot/rdict/descr.go (about)

     1  // Copyright 2020 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 rdict
     6  
     7  import (
     8  	"reflect"
     9  
    10  	"go-hep.org/x/hep/groot/rbytes"
    11  	"go-hep.org/x/hep/groot/rmeta"
    12  )
    13  
    14  // type arrayMode int32
    15  
    16  type elemDescr struct {
    17  	otype  rmeta.Enum
    18  	ntype  rmeta.Enum
    19  	offset int // actually an index to the struct's field or to array's element
    20  	length int
    21  	elem   rbytes.StreamerElement
    22  	method []int
    23  	oclass string
    24  	nclass string
    25  	mbr    any // member streamer
    26  }
    27  
    28  type streamerConfig struct {
    29  	si     *StreamerInfo
    30  	eid    int // element ID
    31  	descr  *elemDescr
    32  	offset int // offset/index within object. negative if no offset to be applied.
    33  	length int // number of elements for fixed-length arrays
    34  
    35  	count func() int // optional func to give the length of ROOT's C var-len arrays.
    36  }
    37  
    38  func (cfg *streamerConfig) counter(recv any) int {
    39  	if cfg.count != nil {
    40  		return cfg.count()
    41  	}
    42  	return int(reflect.ValueOf(recv).Elem().FieldByIndex(cfg.descr.method).Int())
    43  }
    44  
    45  func (cfg *streamerConfig) adjust(recv any) any {
    46  	if cfg == nil || cfg.offset < 0 {
    47  		return recv
    48  	}
    49  	rv := reflect.ValueOf(recv).Elem()
    50  	switch rv.Kind() {
    51  	case reflect.Struct:
    52  		return rv.Field(cfg.offset).Addr().Interface()
    53  	case reflect.Array, reflect.Slice:
    54  		return rv.Index(cfg.offset).Addr().Interface()
    55  	default:
    56  		return recv
    57  	}
    58  }