Ejemplo n.º 1
0
// NewCompoundType creates a new CompoundType.
// size is the size in bytes of the compound datatype.
func NewCompoundType(size int) (*CompoundType, error) {
	id := C.H5Tcreate(C.H5T_class_t(T_COMPOUND), C.size_t(size))
	if err := checkID(id); err != nil {
		return nil, err
	}
	t := &CompoundType{Datatype{Location{Identifier{id}}}}
	runtime.SetFinalizer(t, (*CompoundType).finalizer)
	return t, nil
}
Ejemplo n.º 2
0
Archivo: put.go Proyecto: postfix/hdf5
func initializeStructToPut(object *object, value reflect.Value) error {
	typo := value.Type()
	size := C.size_t(typo.Size())

	object.data = C.malloc(size)
	if object.data == nil {
		return errors.New("cannot allocate memory")
	}
	object.flag |= flagOwnedMemory

	object.tid = C.H5Tcreate(C.H5T_COMPOUND, size)
	if object.tid < 0 {
		return errors.New("cannot create a compound datatype")
	}

	count := typo.NumField()

	for i := 0; i < count; i++ {
		field := typo.Field(i)
		if len(field.PkgPath) > 0 {
			continue
		}

		o := object.new()
		if err := initializeToPut(o, value.Field(i)); err != nil {
			return err
		}

		address := unsafe.Pointer(uintptr(object.data) + uintptr(field.Offset))

		if o.flag&flagVariableLength != 0 {
			tid := C.H5Tvlen_create(o.tid)
			if tid < 0 {
				return errors.New("cannnot create a variable-length datatype")
			}

			// NOTE: It is assumed here that sizeof(hvl_t) <= v.Type().Size().
			h := (*C.hvl_t)(address)
			h.len, h.p = 1, o.data

			o = object.new()
			o.tid = tid
		} else {
			C.memcpy(address, o.data, C.size_t(value.Field(i).Type().Size()))
		}

		cname := C.CString(field.Name)
		defer C.free(unsafe.Pointer(cname))

		if C.H5Tinsert(object.tid, cname, C.size_t(field.Offset), o.tid) < 0 {
			return errors.New("cannot construct a compound datatype")
		}
	}

	return nil
}
Ejemplo n.º 3
0
// Creates a new datatype.
// hid_t H5Tcreate( H5T_class_t class, size_tsize )
func CreateDataType(class TypeClass, size int) (t *DataType, err error) {
	t = nil
	err = nil

	hid := C.H5Tcreate(C.H5T_class_t(class), C.size_t(size))
	err = togo_err(C.herr_t(int(hid)))
	if err != nil {
		return
	}
	t = new_dtype(hid, _type_cls_to_go_type[class])
	return
}
Ejemplo n.º 4
0
// CreateDatatype creates a new datatype.
// class must be T_COMPUND, T_OPAQUE, T_ENUM or T_STRING.
// size is the size of the new datatype in bytes.
func CreateDatatype(class TypeClass, size int) (*Datatype, error) {
	_, ok := parametricTypes[class]
	if !ok {
		return nil, fmt.Errorf(
			"invalid TypeClass, want %v, %v, %v or %v, got %v",
			T_COMPOUND, T_OPAQUE, T_STRING, T_ENUM)
	}

	hid := C.H5Tcreate(C.H5T_class_t(class), C.size_t(size))
	err := h5err(C.herr_t(int(hid)))
	if err != nil {
		return nil, err
	}
	return NewDatatype(hid), nil
}