go-hep.org/x/hep@v0.40.0/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.TypeFor[int8](): Char, 17 reflect.TypeFor[int16](): Short, 18 reflect.TypeFor[int32](): Int, 19 reflect.TypeFor[int64](): Long, 20 // reflect.TypeOf(int64(0)): Long64, 21 reflect.TypeFor[float32](): Float, 22 reflect.TypeFor[float64](): Double, 23 reflect.TypeFor[uint8](): UChar, 24 // reflect.TypeOf(uint8(0)): CharStar, 25 reflect.TypeFor[uint16](): UShort, 26 reflect.TypeFor[uint32](): UInt, 27 reflect.TypeFor[uint64](): ULong, 28 // reflect.TypeOf(uint64(0)): ULong64, 29 reflect.TypeFor[bool](): Bool, 30 reflect.TypeFor[root.Float16](): Float16, 31 reflect.TypeFor[root.Double32](): Double32, 32 reflect.TypeFor[string](): 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.TypeFor[bool](), 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.TypeFor[int8](), 74 "int16_t": reflect.TypeFor[int16](), 75 "int32_t": reflect.TypeFor[int32](), 76 "int64_t": reflect.TypeFor[int64](), 77 "uint8_t": reflect.TypeFor[uint8](), 78 "uint16_t": reflect.TypeFor[uint16](), 79 "uint32_t": reflect.TypeFor[uint32](), 80 "uint64_t": reflect.TypeFor[uint64](), 81 82 // C/C++ builtins 83 84 "unsigned": reflect.TypeFor[uint32](), 85 "unsigned char": reflect.TypeFor[uint8](), 86 "unsigned short": reflect.TypeFor[uint16](), 87 "unsigned int": reflect.TypeFor[uint32](), 88 "unsigned long": reflect.TypeFor[uint64](), 89 90 //"int": reflect.TypeOf(int(0)), 91 "char": reflect.TypeFor[int8](), 92 "short": reflect.TypeFor[int16](), 93 "int": reflect.TypeFor[int32](), 94 "long": reflect.TypeFor[int64](), 95 96 "float": reflect.TypeFor[float32](), 97 "double": reflect.TypeFor[float64](), 98 99 "string": reflect.TypeFor[string](), 100 101 // ROOT builtins 102 "Bool_t": reflect.TypeFor[bool](), 103 104 "Byte_t": reflect.TypeFor[uint8](), 105 106 "Char_t": reflect.TypeFor[int8](), 107 "UChar_t": reflect.TypeFor[uint8](), 108 "Short_t": reflect.TypeFor[int16](), 109 "UShort_t": reflect.TypeFor[uint16](), 110 "Int_t": reflect.TypeFor[int32](), 111 "UInt_t": reflect.TypeFor[uint32](), 112 "Seek_t": reflect.TypeFor[int64](), // FIXME(sbinet): not portable 113 "Long_t": reflect.TypeFor[int64](), // FIXME(sbinet): not portable 114 "ULong_t": reflect.TypeFor[uint64](), // FIXME(sbinet): not portable 115 "Long64_t": reflect.TypeFor[int64](), 116 "ULong64_t": reflect.TypeFor[uint64](), 117 118 "Float_t": reflect.TypeFor[float32](), 119 "Float16_t": reflect.TypeFor[root.Float16](), 120 "Double_t": reflect.TypeFor[float64](), 121 "Double32_t": reflect.TypeFor[root.Double32](), 122 123 "Version_t": reflect.TypeFor[int16](), 124 "Option_t": reflect.TypeFor[string](), 125 "Ssiz_t": reflect.TypeFor[int](), 126 "Real_t": reflect.TypeFor[float32](), 127 128 "Axis_t": reflect.TypeFor[float64](), 129 "Stat_t": reflect.TypeFor[float64](), 130 131 "Font_t": reflect.TypeFor[int16](), 132 "Style_t": reflect.TypeFor[int16](), 133 "Marker_t": reflect.TypeFor[int16](), 134 "Width_t": reflect.TypeFor[int16](), 135 "Color_t": reflect.TypeFor[int16](), 136 "SCoord_t": reflect.TypeFor[int16](), 137 "Coord_t": reflect.TypeFor[float64](), 138 "Angle_t": reflect.TypeFor[float32](), 139 "Size_t": reflect.TypeFor[float32](), 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 }