go-hep.org/x/hep@v0.38.1/groot/rmeta/cxx.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 rmeta
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"strings"
    11  
    12  	"go-hep.org/x/hep/groot/root"
    13  )
    14  
    15  var GoType2ROOTEnum = map[reflect.Type]Enum{
    16  	reflect.TypeOf(int8(0)):  Char,
    17  	reflect.TypeOf(int16(0)): Short,
    18  	reflect.TypeOf(int32(0)): Int,
    19  	reflect.TypeOf(int64(0)): Long,
    20  	//	reflect.TypeOf(int64(0)): Long64,
    21  	reflect.TypeOf(float32(0)): Float,
    22  	reflect.TypeOf(float64(0)): Double,
    23  	reflect.TypeOf(uint8(0)):   UChar,
    24  	//	reflect.TypeOf(uint8(0)): CharStar,
    25  	reflect.TypeOf(uint16(0)): UShort,
    26  	reflect.TypeOf(uint32(0)): UInt,
    27  	reflect.TypeOf(uint64(0)): ULong,
    28  	//	reflect.TypeOf(uint64(0)): ULong64,
    29  	reflect.TypeOf(false):            Bool,
    30  	reflect.TypeOf(root.Float16(0)):  Float16,
    31  	reflect.TypeOf(root.Double32(0)): Double32,
    32  	reflect.TypeOf(""):               TString,
    33  }
    34  
    35  var GoType2Cxx = map[string]string{
    36  	"bool":    "bool",
    37  	"byte":    "unsigned char",
    38  	"uint":    "unsigned int",
    39  	"uint8":   "unsigned char",
    40  	"uint16":  "unsigned short",
    41  	"uint32":  "unsigned int",
    42  	"uint64":  "unsigned long",
    43  	"int":     "int",
    44  	"int8":    "char",
    45  	"int16":   "short",
    46  	"int32":   "int",
    47  	"int64":   "long",
    48  	"float32": "float",
    49  	"float64": "double",
    50  }
    51  
    52  var CxxBuiltins = map[string]reflect.Type{
    53  	"bool": reflect.TypeOf(false),
    54  
    55  	/*
    56  		"uint":   reflect.TypeOf(uint(0)),
    57  		"uint8":  reflect.TypeOf(uint8(0)),
    58  		"uint16": reflect.TypeOf(uint16(0)),
    59  		"uint32": reflect.TypeOf(uint32(0)),
    60  		"uint64": reflect.TypeOf(uint64(0)),
    61  
    62  		"int":   reflect.TypeOf(int(0)),
    63  		"int8":  reflect.TypeOf(int8(0)),
    64  		"int16": reflect.TypeOf(int16(0)),
    65  		"int32": reflect.TypeOf(int32(0)),
    66  		"int64": reflect.TypeOf(int64(0)),
    67  
    68  		"float32": reflect.TypeOf(float32(0)),
    69  		"float64": reflect.TypeOf(float64(0)),
    70  	*/
    71  
    72  	// stdint
    73  	"int8_t":   reflect.TypeOf(int8(0)),
    74  	"int16_t":  reflect.TypeOf(int16(0)),
    75  	"int32_t":  reflect.TypeOf(int32(0)),
    76  	"int64_t":  reflect.TypeOf(int64(0)),
    77  	"uint8_t":  reflect.TypeOf(uint8(0)),
    78  	"uint16_t": reflect.TypeOf(uint16(0)),
    79  	"uint32_t": reflect.TypeOf(uint32(0)),
    80  	"uint64_t": reflect.TypeOf(uint64(0)),
    81  
    82  	// C/C++ builtins
    83  
    84  	"unsigned":       reflect.TypeOf(uint32(0)),
    85  	"unsigned char":  reflect.TypeOf(uint8(0)),
    86  	"unsigned short": reflect.TypeOf(uint16(0)),
    87  	"unsigned int":   reflect.TypeOf(uint32(0)),
    88  	"unsigned long":  reflect.TypeOf(uint64(0)),
    89  
    90  	//"int":   reflect.TypeOf(int(0)),
    91  	"char":  reflect.TypeOf(int8(0)),
    92  	"short": reflect.TypeOf(int16(0)),
    93  	"int":   reflect.TypeOf(int32(0)),
    94  	"long":  reflect.TypeOf(int64(0)),
    95  
    96  	"float":  reflect.TypeOf(float32(0)),
    97  	"double": reflect.TypeOf(float64(0)),
    98  
    99  	"string": reflect.TypeOf(""),
   100  
   101  	// ROOT builtins
   102  	"Bool_t": reflect.TypeOf(true),
   103  
   104  	"Byte_t": reflect.TypeOf(uint8(0)),
   105  
   106  	"Char_t":    reflect.TypeOf(int8(0)),
   107  	"UChar_t":   reflect.TypeOf(uint8(0)),
   108  	"Short_t":   reflect.TypeOf(int16(0)),
   109  	"UShort_t":  reflect.TypeOf(uint16(0)),
   110  	"Int_t":     reflect.TypeOf(int32(0)),
   111  	"UInt_t":    reflect.TypeOf(uint32(0)),
   112  	"Seek_t":    reflect.TypeOf(int64(0)),  // FIXME(sbinet): not portable
   113  	"Long_t":    reflect.TypeOf(int64(0)),  // FIXME(sbinet): not portable
   114  	"ULong_t":   reflect.TypeOf(uint64(0)), // FIXME(sbinet): not portable
   115  	"Long64_t":  reflect.TypeOf(int64(0)),
   116  	"ULong64_t": reflect.TypeOf(uint64(0)),
   117  
   118  	"Float_t":    reflect.TypeOf(float32(0)),
   119  	"Float16_t":  reflect.TypeOf(root.Float16(0)),
   120  	"Double_t":   reflect.TypeOf(float64(0)),
   121  	"Double32_t": reflect.TypeOf(root.Double32(0)),
   122  
   123  	"Version_t": reflect.TypeOf(int16(0)),
   124  	"Option_t":  reflect.TypeOf(""),
   125  	"Ssiz_t":    reflect.TypeOf(int(0)),
   126  	"Real_t":    reflect.TypeOf(float32(0)),
   127  
   128  	"Axis_t": reflect.TypeOf(float64(0)),
   129  	"Stat_t": reflect.TypeOf(float64(0)),
   130  
   131  	"Font_t":   reflect.TypeOf(int16(0)),
   132  	"Style_t":  reflect.TypeOf(int16(0)),
   133  	"Marker_t": reflect.TypeOf(int16(0)),
   134  	"Width_t":  reflect.TypeOf(int16(0)),
   135  	"Color_t":  reflect.TypeOf(int16(0)),
   136  	"SCoord_t": reflect.TypeOf(int16(0)),
   137  	"Coord_t":  reflect.TypeOf(float64(0)),
   138  	"Angle_t":  reflect.TypeOf(float32(0)),
   139  	"Size_t":   reflect.TypeOf(float32(0)),
   140  }
   141  
   142  func STLNameFrom(name string, vtype ESTLType, ctype Enum) string {
   143  	if ctype == Object {
   144  		return name
   145  	}
   146  	return STLNameFor(vtype, ctype)
   147  }
   148  
   149  // STLNameFor creates a regular C++ STL container name given a STL enum type
   150  // and a ROOT enum value for the contained element.
   151  func STLNameFor(vtype ESTLType, ctype Enum) string {
   152  	ename := rmeta2Name(ctype)
   153  	if strings.HasSuffix(ename, ">") {
   154  		ename += " "
   155  	}
   156  
   157  	typfmt := "%s"
   158  	switch vtype {
   159  	case STLvector:
   160  		typfmt = "vector<%s>"
   161  	case STLlist:
   162  		typfmt = "list<%s>"
   163  	case STLdeque:
   164  		typfmt = "deque<%s>"
   165  	case STLmap:
   166  		typfmt = "map<%s>"
   167  	case STLmultimap:
   168  		typfmt = "multimap<%s>"
   169  	case STLset:
   170  		typfmt = "set<%s>"
   171  	case STLmultiset:
   172  		typfmt = "multiset<%s>"
   173  	case STLbitset:
   174  		typfmt = "bitset<%s>"
   175  	case STLforwardlist:
   176  		typfmt = "forward_list<%s>"
   177  	case STLunorderedset:
   178  		typfmt = "unordered_set<%s>"
   179  	case STLunorderedmultiset:
   180  		typfmt = "unordered_multiset<%s>"
   181  	case STLunorderedmap:
   182  		typfmt = "unordered_map<%s>"
   183  	case STLunorderedmultimap:
   184  		typfmt = "unordered_multimap<%s>"
   185  	}
   186  
   187  	return fmt.Sprintf(typfmt, ename)
   188  }
   189  
   190  func rmeta2Name(t Enum) string {
   191  	switch t {
   192  	case Char:
   193  		return "char"
   194  	case Short:
   195  		return "short"
   196  	case Int:
   197  		return "int"
   198  	case Long:
   199  		return "long"
   200  	case Float:
   201  		return "float"
   202  	case Counter:
   203  		return "int"
   204  	case CharStar:
   205  		return "char*"
   206  	case Double:
   207  		return "double"
   208  	case Double32:
   209  		return "Double32_t"
   210  	case LegacyChar:
   211  		return "char" // FIXME(sbinet)
   212  	case UChar:
   213  		return "unsigned char"
   214  	case UShort:
   215  		return "unsigned short"
   216  	case UInt:
   217  		return "unsigned int"
   218  	case ULong:
   219  		return "unsigned long"
   220  	case Bits:
   221  		return "bits" // FIXME(sbinet)
   222  	case Long64:
   223  		return "Long64_t"
   224  	case ULong64:
   225  		return "ULong64_t"
   226  	case Bool:
   227  		return "bool"
   228  	case Float16:
   229  		return "Float16_t"
   230  	case Object:
   231  		// class derived from TObject
   232  		return "TObject" // FIXME(sbinet): better handling?
   233  	case TString:
   234  		return "TString"
   235  	case TObject:
   236  		return "TObject"
   237  	case TNamed:
   238  		return "TNamed"
   239  	case STLstring:
   240  		return "string"
   241  	}
   242  	panic(fmt.Errorf("not implemented: t=%v (%d)", t, t))
   243  }