go-hep.org/x/hep@v0.38.1/groot/rbase/uuid.go (about)

     1  // Copyright ©2018 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 UUID [16]byte
    17  
    18  func (*UUID) Class() string {
    19  	return "TUUID"
    20  }
    21  
    22  func (*UUID) RVersion() int16 {
    23  	return rvers.UUID
    24  }
    25  
    26  func (*UUID) Sizeof() int32 {
    27  	// UUID-version (u16)
    28  	// UUID-payload (16-bytes)
    29  	return 18
    30  }
    31  
    32  func (uuid *UUID) MarshalROOT(w *rbytes.WBuffer) (int, error) {
    33  	if w.Err() != nil {
    34  		return 0, w.Err()
    35  	}
    36  	pos := w.Pos()
    37  	w.WriteU16(uint16(uuid.RVersion()))
    38  	_, _ = w.Write((*uuid)[:])
    39  	end := w.Pos()
    40  
    41  	return int(end - pos), w.Err()
    42  }
    43  
    44  func (uuid *UUID) UnmarshalROOTv1(r *rbytes.RBuffer) error {
    45  	if r.Err() != nil {
    46  		return r.Err()
    47  	}
    48  
    49  	_, _ = r.Read((*uuid)[:])
    50  
    51  	return r.Err()
    52  }
    53  
    54  func (uuid *UUID) UnmarshalROOT(r *rbytes.RBuffer) error {
    55  	if r.Err() != nil {
    56  		return r.Err()
    57  	}
    58  
    59  	_ = r.ReadU16() // version
    60  	_, _ = r.Read((*uuid)[:])
    61  
    62  	return r.Err()
    63  }
    64  
    65  func init() {
    66  	f := func() reflect.Value {
    67  		o := &UUID{}
    68  		return reflect.ValueOf(o)
    69  	}
    70  	rtypes.Factory.Add("TUUID", f)
    71  }
    72  
    73  var (
    74  	_ root.Object        = (*UUID)(nil)
    75  	_ rbytes.Marshaler   = (*UUID)(nil)
    76  	_ rbytes.Unmarshaler = (*UUID)(nil)
    77  )