当前位置: 首页>>代码示例>>Golang>>正文


Golang Type.Size方法代码示例

本文整理汇总了Golang中reflect.Type.Size方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Size方法的具体用法?Golang Type.Size怎么用?Golang Type.Size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Type的用法示例。


在下文中一共展示了Type.Size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: sizeof

func sizeof(v reflect.Type) int {
	switch t := v.(type) {
	case *reflect.ArrayType:
		n := sizeof(t.Elem())
		if n < 0 {
			return -1
		}
		return t.Len() * n

	case *reflect.StructType:
		sum := 0
		for i, n := 0, t.NumField(); i < n; i++ {
			s := sizeof(t.Field(i).Type)
			if s < 0 {
				return -1
			}
			sum += s
		}
		return sum

	case *reflect.UintType, *reflect.IntType, *reflect.FloatType, *reflect.ComplexType:
		switch t := t.Kind(); t {
		case reflect.Int, reflect.Uint, reflect.Uintptr:
			return -1
		}
		return int(v.Size())
	}
	return -1
}
开发者ID:richlowe,项目名称:gcc,代码行数:29,代码来源:binary.go

示例2: defaultType

func defaultType(t reflect.Type) TypeDesc {
	switch t.Kind() {
	case reflect.Bool:
		return BooleanType
	case reflect.String:
		return UTF8Type
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return LongType
	case reflect.Float32:
		return FloatType
	case reflect.Float64:
		return DoubleType
	case reflect.Array:
		if t.Name() == "UUID" && t.Size() == 16 {
			return UUIDType
		}
		return UnknownType
	case reflect.Struct:
		if t.Name() == "Time" && t.PkgPath() == "time" {
			return DateType
		}
		return UnknownType
	case reflect.Slice:
		if et := t.Elem(); et.Kind() == reflect.Uint8 {
			return BytesType
		}
		return UnknownType
	}
	return UnknownType
}
开发者ID:ngmoco,项目名称:gossie,代码行数:30,代码来源:types.go

示例3: indexer

func indexer(t reflect.Type) indexFunc {
	size := t.Size()
	return func(v handle, i int) handle {
		header := (*reflect.SliceHeader)(unsafe.Pointer(&v))
		return handle(header.Data + uintptr(i)*size)
	}
}
开发者ID:rainycape,项目名称:gondola,代码行数:7,代码来源:funcs_unsafe.go

示例4: sizeof

func sizeof(t reflect.Type) (int, error) {
	switch t.Kind() {
	case reflect.Array:
		n, err := sizeof(t.Elem())
		if err != nil {
			return 0, err
		}
		return t.Len() * n, nil

	case reflect.Struct:
		sum := 0
		for i, n := 0, t.NumField(); i < n; i++ {
			s, err := sizeof(t.Field(i).Type)
			if err != nil {
				return 0, err
			}
			sum += s
		}
		return sum, nil

	case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
		return int(t.Size()), nil
	}
	return 0, errors.New("invalid type " + t.String())
}
开发者ID:h8liu,项目名称:golang,代码行数:27,代码来源:binary.go

示例5: integerPromote

// integerPromote determines if we can promote v to dType, and if so, return the promoted value.
// This is needed because msgpack always encodes values as the minimum sized int that can hold them.
func integerPromote(dType reflect.Type, v reflect.Value) (reflect.Value, bool) {

	vt := v.Type()
	dsz := dType.Size()
	vtsz := vt.Size()

	if isIntType(dType) && isIntType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetInt(v.Int())
		return pv, true
	}

	if isUintType(dType) && isUintType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetUint(v.Uint())
		return pv, true
	}

	if isIntType(dType) && isUintType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetInt(int64(v.Uint()))
		return pv, true
	}

	if isUintType(dType) && isIntType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetUint(uint64(v.Int()))
		return pv, true
	}

	return v, false
}
开发者ID:andrebq,项目名称:skynet,代码行数:34,代码来源:rpc.go

示例6: sizeof

// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
func sizeof(t reflect.Type) int {
	switch t.Kind() {
	case reflect.Array:
		if s := sizeof(t.Elem()); s >= 0 {
			return s * t.Len()
		}

	case reflect.Struct:
		sum := 0
		for i, n := 0, t.NumField(); i < n; i++ {
			s := sizeof(t.Field(i).Type)
			if s < 0 {
				return -1
			}
			sum += s
		}
		return sum

	case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Ptr:
		return int(t.Size())
	}

	return -1
}
开发者ID:freedomkk-qfeng,项目名称:agent,代码行数:27,代码来源:binary.go

示例7: benchmarkWriteSlice

func benchmarkWriteSlice(b *testing.B, typ reflect.Type, count int) {
	slice := reflect.MakeSlice(reflect.SliceOf(typ), count, count).Interface()
	b.SetBytes(int64(count * int(typ.Size())))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Write(ioutil.Discard, BigEndian, slice)
	}
}
开发者ID:rainycape,项目名称:gondola,代码行数:8,代码来源:binary_test.go

示例8: benchmarkReadSlice

func benchmarkReadSlice(b *testing.B, typ reflect.Type, count int) {
	fr := &fakeReader{}
	slice := reflect.MakeSlice(reflect.SliceOf(typ), count, count).Interface()
	b.SetBytes(int64(count * int(typ.Size())))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Read(fr, BigEndian, slice)
	}
}
开发者ID:rainycape,项目名称:gondola,代码行数:9,代码来源:binary_test.go

示例9: structMatchGeneric

func structMatchGeneric(t *testing.T, ta, tb reflect.Type, sza, szb uintptr) {
	if sza != szb {
		t.Errorf("type size mismatch: %s(%d) != %s(%d)",
			ta.Name(), ta.Size(), tb.Name(), tb.Size())
		t.Fail()
	}

	if ta.Kind() != tb.Kind() {
		t.Errorf("type kind mismatch: %s=%s != %s=%s",
			ta.Name(), ta.Kind(), tb.Name(), tb.Kind())
		t.Fail()
	}
}
开发者ID:veandco,项目名称:go-sdl2,代码行数:13,代码来源:struct_test.go

示例10: sse2Reflect

func sse2Reflect(t reflect.Type) sse2type {
	elemSize := uint(0)
	if t.Kind() == reflect.Array {
		elemSize = uint(t.Elem().Size())
	}
	return sse2type{
		name:     t.Name(),
		size:     uint(t.Size()),
		elemSize: elemSize,
		align:    uint(t.Size()),
		t:        t,
	}
}
开发者ID:bjwbell,项目名称:gensimd,代码行数:13,代码来源:sizeinfo.go

示例11: indexSetter

func indexSetter(t reflect.Type) indexSetFunc {
	size := t.Size()
	return func(v handle, i int, val handle) {
		header := (*reflect.SliceHeader)(unsafe.Pointer(&v))
		var dst []byte
		dh := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
		dh.Len = int(size)
		dh.Data = header.Data + uintptr(i)*size
		var src []byte
		sh := (*reflect.SliceHeader)(unsafe.Pointer(&src))
		sh.Len = int(size)
		sh.Data = uintptr(val)
		copy(dst, src)
	}
}
开发者ID:rainycape,项目名称:gondola,代码行数:15,代码来源:funcs_unsafe.go

示例12: MatchHeaders

func MatchHeaders(b reflect.Type, slice interface{}, header reflect.SliceHeader) (e error) {
	h, s, al := SliceHeader(slice)
	switch {
	case s != int(b.Size()):
		e = HeaderMismatch{"%v: slice element size should be %v not %v", b.Name(), b.Size(), s}
	case al != b.Align():
		e = HeaderMismatch{"%v: slice element alignment should be %v not %v", b.Name(), b.Align(), al}
	case h.Data != header.Data:
		e = HeaderMismatch{"%v: slice headers should point to the same memory: %v - %v", b.Name(), h.Data, header.Data}
	case h.Len != header.Len:
		e = HeaderMismatch{"%v: slice header lengths should be the same: %v - %v", b.Name(), h.Len, header.Len}
	case h.Cap != header.Cap:
		e = HeaderMismatch{"%v: slice header capacities should be the same: %v - %v", b.Name(), h.Cap, header.Cap}
	}
	return
}
开发者ID:feyeleanor,项目名称:raw,代码行数:16,代码来源:reslice_test.go

示例13: swapper

func swapper(t reflect.Type) swapFunc {
	size := t.Size()
	tmp := make([]byte, size)
	return func(v handle, i, j int) {
		header := (*reflect.SliceHeader)(unsafe.Pointer(&v))
		var si []byte
		hi := (*reflect.SliceHeader)(unsafe.Pointer(&si))
		hi.Len = int(size)
		hi.Data = header.Data + uintptr(i)*size
		var sj []byte
		hj := (*reflect.SliceHeader)(unsafe.Pointer(&sj))
		hj.Len = int(size)
		hj.Data = header.Data + uintptr(j)*size
		copy(tmp, sj)
		copy(sj, si)
		copy(si, tmp)
	}
}
开发者ID:rainycape,项目名称:gondola,代码行数:18,代码来源:funcs_unsafe.go

示例14: NewMesh

func NewMesh(attrs interface{}, indicies []uint32) *Mesh {
	mesh := new(Mesh)
	mesh.Attrs = attrs
	mesh.Indicies = indicies
	gl33.GenVertexArrays(1, &mesh.vao)
	gl33.BindVertexArray(mesh.vao)
	attrsValue := reflect.ValueOf(attrs)
	mesh.vertexBO = NewBuffer(gl33.ARRAY_BUFFER, gl33.STATIC_DRAW, attrs)
	vertexSpec := attrsValue.Type().Elem()
	if vertexSpec.Kind() != reflect.Struct && vertexSpec.Kind() != reflect.Array {
		panic("attrs is not a slice of structs or arrays")
	}
	var num int
	if vertexSpec.Kind() == reflect.Struct {
		num = vertexSpec.NumField()
	} else {
		num = vertexSpec.Len()
	}
	for i, offset := 0, uintptr(0); i < num; i++ {
		var field reflect.Type
		var type_ gl33.Enum
		var dimensions int
		if vertexSpec.Kind() == reflect.Struct {
			field = vertexSpec.Field(i).Type
		} else {
			field = vertexSpec.Elem()
		}
		if field.Kind() == reflect.Array {
			type_ = glType(field.Elem().Kind())
			dimensions = field.Len()
		} else {
			type_ = glType(field.Kind())
			dimensions = 1
		}
		setupAttrib(gl33.Uint(i), type_, dimensions, offset, int(vertexSpec.Size()))
		offset += field.Size()
	}
	mesh.indexBO = NewBuffer(gl33.ELEMENT_ARRAY_BUFFER, gl33.STATIC_DRAW, indicies)
	return mesh
}
开发者ID:klkblake,项目名称:sge,代码行数:40,代码来源:mesh.go

示例15: structMatch

func structMatch(t *testing.T, ta, tb reflect.Type) {
	structMatchGeneric(t, ta, tb, ta.Size(), tb.Size())
}
开发者ID:veandco,项目名称:go-sdl2,代码行数:3,代码来源:struct_test.go


注:本文中的reflect.Type.Size方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。