├── README.md ├── c_error.cc ├── c_error.h ├── goobj.go ├── string_test.go ├── error_test.go ├── error.go ├── cgo.go ├── string.go ├── char.go ├── c_string.h ├── void_pointer.go ├── string_list.go ├── c_string.cc ├── file.go └── types.go /README.md: -------------------------------------------------------------------------------- 1 | - *Go语言QQ群: 102319854, 1055927514* 2 | - *凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa* 3 | 4 | ---- 5 | 6 | # Golang CGO helper 7 | -------------------------------------------------------------------------------- /c_error.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 | #include "c_error.h" 6 | 7 | #include 8 | 9 | struct chai2010_cgo_Error_T { 10 | int errCode; 11 | std::string errText; 12 | }; 13 | 14 | chai2010_cgo_Error_T* chai2010_cgo_Error_New(int errCode, const char* errText) { 15 | auto p = new chai2010_cgo_Error_T(); 16 | p->errCode = errCode; 17 | p->errText = errText? errText: ""; 18 | return p; 19 | } 20 | void chai2010_cgo_Error_Delete(chai2010_cgo_Error_T* p) { 21 | delete p; 22 | } 23 | 24 | void chai2010_cgo_Error_SetError(chai2010_cgo_Error_T* p, int errCode, const char* errText) { 25 | p->errCode = errCode; 26 | p->errText = errText? errText: ""; 27 | } 28 | 29 | int chai2010_cgo_Error_GetCode(chai2010_cgo_Error_T* p) { 30 | return p->errCode; 31 | } 32 | const char* chai2010_cgo_Error_GetText(chai2010_cgo_Error_T* p) { 33 | return p->errText.c_str(); 34 | } 35 | -------------------------------------------------------------------------------- /c_error.h: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 | // https://github.com/chai2010/cgo 6 | 7 | #pragma once 8 | 9 | #ifndef CHAI2010_CGO_C_ERROR_H_ 10 | #define CHAI2010_CGO_C_ERROR_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #if defined(__cplusplus) 18 | extern "C" { 19 | #endif 20 | 21 | typedef struct chai2010_cgo_Error_T chai2010_cgo_Error_T; 22 | 23 | chai2010_cgo_Error_T* chai2010_cgo_Error_New(int errCode, const char* errText); 24 | void chai2010_cgo_Error_Delete(chai2010_cgo_Error_T* p); 25 | 26 | void chai2010_cgo_Error_SetError(chai2010_cgo_Error_T* p, int errCode, const char* errText); 27 | 28 | int chai2010_cgo_Error_GetCode(chai2010_cgo_Error_T* p); 29 | const char* chai2010_cgo_Error_GetText(chai2010_cgo_Error_T* p); 30 | 31 | #if defined(__cplusplus) 32 | } 33 | #endif 34 | #endif // CHAI2010_CGO_C_ERROR_H_ 35 | -------------------------------------------------------------------------------- /goobj.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | import ( 8 | "sync" 9 | ) 10 | 11 | type ObjectId int32 12 | 13 | var refs struct { 14 | sync.Mutex 15 | objs map[ObjectId]interface{} 16 | next ObjectId 17 | } 18 | 19 | func init() { 20 | refs.Lock() 21 | defer refs.Unlock() 22 | 23 | refs.objs = make(map[ObjectId]interface{}) 24 | refs.next = 1000 25 | } 26 | 27 | func NewObjectId(obj interface{}) ObjectId { 28 | refs.Lock() 29 | defer refs.Unlock() 30 | 31 | id := refs.next 32 | refs.next++ 33 | 34 | refs.objs[id] = obj 35 | return id 36 | } 37 | 38 | func (id ObjectId) IsNil() bool { 39 | return id == 0 40 | } 41 | 42 | func (id ObjectId) Get() interface{} { 43 | refs.Lock() 44 | defer refs.Unlock() 45 | 46 | return refs.objs[id] 47 | } 48 | 49 | func (id *ObjectId) Free() interface{} { 50 | refs.Lock() 51 | defer refs.Unlock() 52 | 53 | obj := refs.objs[*id] 54 | delete(refs.objs, *id) 55 | *id = 0 56 | 57 | return obj 58 | } 59 | -------------------------------------------------------------------------------- /string_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | import ( 8 | "testing" 9 | ) 10 | 11 | func TestString(t *testing.T) { 12 | s := NewString("") 13 | defer s.Delete() 14 | 15 | tAssertStrEQ(t, s, "") 16 | 17 | s.AssignByGoString("abc") 18 | tAssertStrEQ(t, s, "abc") 19 | 20 | s.AssignByGoString("") 21 | tAssertStrEQ(t, s, "") 22 | 23 | s.AssignByGoBytes([]byte("123")) 24 | tAssertStrEQ(t, s, "123") 25 | 26 | s.AssignByGoBytes(nil) 27 | tAssertStrEQ(t, s, "") 28 | } 29 | 30 | func tAssertStrEQ(t *testing.T, s1 *String, s2 string) { 31 | if s1.Size() != len(s2) { 32 | t.Fatalf("%q: bad size", s2) 33 | } 34 | 35 | if s1.Data().Strlen() != len(s2) { 36 | t.Fatalf("%q: bad Strlen", s2) 37 | } 38 | if s1.CStr().Strlen() != len(s2) { 39 | t.Fatalf("%q: bad Strlen", s2) 40 | } 41 | 42 | if string(s1.GoSlice()) != s2 { 43 | t.Fatalf("%q: bad GoSlice", s2) 44 | } 45 | if s1.GoString() != s2 { 46 | t.Fatalf("%q: bad GoString", s2) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /error_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | import ( 8 | "testing" 9 | ) 10 | 11 | func TestError(t *testing.T) { 12 | p := NewErrorFrom(9527, "hi") 13 | defer p.Delete() 14 | 15 | code := p.GetCode() 16 | text := p.GetText().GoString() 17 | 18 | if code != 9527 { 19 | t.Fatalf("expect = %v, got = %v", 9527, code) 20 | } 21 | if text != "hi" { 22 | t.Fatalf("expect = %v, got = %v", "hi", text) 23 | } 24 | } 25 | 26 | func TestError_empty(t *testing.T) { 27 | p := NewErrorFrom(0, "") 28 | defer p.Delete() 29 | 30 | code := p.GetCode() 31 | text := p.GetText().GoString() 32 | 33 | if code != 0 { 34 | t.Fatalf("expect = %v, got = %v", 0, code) 35 | } 36 | if text != "" { 37 | t.Fatalf("expect = , got = %v", text) 38 | } 39 | } 40 | 41 | func TestError_init(t *testing.T) { 42 | p := NewErrorFrom(0, "") 43 | defer p.Delete() 44 | 45 | p.SetErrorFrom(9527, "hi") 46 | 47 | code := p.GetCode() 48 | text := p.GetText().GoString() 49 | 50 | if code != 9527 { 51 | t.Fatalf("expect = %v, got = %v", 9527, code) 52 | } 53 | if text != "hi" { 54 | t.Fatalf("expect = %v, got = %v", "hi", text) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /error.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | //#include "./c_error.h" 8 | import "C" 9 | 10 | type Error C.chai2010_cgo_Error_T 11 | 12 | func NewError(code int, desc *Char) *Error { 13 | return (*Error)(C.chai2010_cgo_Error_New(C.int(code), (*C.char)(desc))) 14 | } 15 | func NewErrorFrom(code int, desc string) *Error { 16 | cs := NewCharString(desc) 17 | defer cs.Free() 18 | 19 | return (*Error)(C.chai2010_cgo_Error_New(C.int(code), (*C.char)(cs))) 20 | } 21 | func (p *Error) Delete() { 22 | C.chai2010_cgo_Error_Delete((*C.chai2010_cgo_Error_T)(p)) 23 | } 24 | 25 | func (p *Error) SetError(code int, desc *Char) { 26 | C.chai2010_cgo_Error_SetError((*C.chai2010_cgo_Error_T)(p), C.int(code), (*C.char)(desc)) 27 | } 28 | func (p *Error) SetErrorFrom(code int, desc string) { 29 | cs := NewCharString(desc) 30 | defer cs.Free() 31 | 32 | C.chai2010_cgo_Error_SetError((*C.chai2010_cgo_Error_T)(p), C.int(code), (*C.char)(cs)) 33 | } 34 | 35 | func (p *Error) GetCode() int { 36 | return int(C.chai2010_cgo_Error_GetCode((*C.chai2010_cgo_Error_T)(p))) 37 | } 38 | 39 | func (p *Error) GetText() *Char { 40 | return (*Char)(C.chai2010_cgo_Error_GetText((*C.chai2010_cgo_Error_T)(p))) 41 | } 42 | -------------------------------------------------------------------------------- /cgo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 | // export cgo functions for go test 6 | 7 | package cgo 8 | 9 | /* 10 | #cgo CXXFLAGS: -std=c++11 11 | #cgo windows LDFLAGS: -Wl,--allow-multiple-definition 12 | 13 | */ 14 | import "C" 15 | import "unsafe" 16 | 17 | // Go string to C string 18 | // The C string is allocated in the C heap using malloc. 19 | // It is the caller's responsibility to arrange for it to be 20 | // freed, such as by calling C.free (be sure to include stdlib.h 21 | // if C.free is needed). 22 | func CString(s string) *Char { 23 | return (*Char)(C.CString(s)) 24 | } 25 | 26 | // Go []byte slice to C array 27 | // The C array is allocated in the C heap using malloc. 28 | // It is the caller's responsibility to arrange for it to be 29 | // freed, such as by calling C.free (be sure to include stdlib.h 30 | // if C.free is needed). 31 | func CBytes(s []byte) unsafe.Pointer { 32 | return C.CBytes(s) 33 | } 34 | 35 | // C string to Go string 36 | func GoString(s *Char) string { 37 | return C.GoString((*C.char)(s)) 38 | } 39 | 40 | // C data with explicit length to Go string 41 | func GoStringN(s *Char, n int) string { 42 | return C.GoStringN((*C.char)(s), C.int(n)) 43 | } 44 | 45 | // C data with explicit length to Go []byte 46 | func GoBytes(p unsafe.Pointer, n int) []byte { 47 | return C.GoBytes(p, C.int(n)) 48 | } 49 | -------------------------------------------------------------------------------- /string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | //#include "./c_string.h" 8 | import "C" 9 | 10 | import ( 11 | "fmt" 12 | "reflect" 13 | "unsafe" 14 | ) 15 | 16 | type String C.chai2010_cgo_String_T 17 | 18 | func NewString(s string) *String { 19 | p := (*String)(C.chai2010_cgo_String_New()) 20 | p.AssignByGoString(s) 21 | return p 22 | } 23 | func NewStringFormat(format string, args ...interface{}) *String { 24 | return NewString(fmt.Sprintf(format, args...)) 25 | } 26 | func NewStringFromStr(s *Char) *String { 27 | return (*String)(C.chai2010_cgo_String_NewFromStr((*C.char)(s))) 28 | } 29 | func NewStringFromStrN(s *Char, n int) *String { 30 | return (*String)(C.chai2010_cgo_String_NewFromStrN((*C.char)(s), C.int(n))) 31 | } 32 | func (s *String) Delete() { 33 | C.chai2010_cgo_String_Delete((*C.chai2010_cgo_String_T)(s)) 34 | } 35 | 36 | func (s *String) Size() int { 37 | return int(C.chai2010_cgo_String_Size((*C.chai2010_cgo_String_T)(s))) 38 | } 39 | func (s *String) Resize(newSize int) { 40 | C.chai2010_cgo_String_Resize((*C.chai2010_cgo_String_T)(s), C.int(newSize)) 41 | } 42 | func (s *String) Clear() { 43 | C.chai2010_cgo_String_Clear((*C.chai2010_cgo_String_T)(s)) 44 | } 45 | 46 | func (s *String) Data() *Char { 47 | return (*Char)(C.chai2010_cgo_String_Data((*C.chai2010_cgo_String_T)(s))) 48 | } 49 | func (s *String) CStr() *Char { 50 | return (*Char)(C.chai2010_cgo_String_CStr((*C.chai2010_cgo_String_T)(s))) 51 | } 52 | 53 | func (s *String) AssignStr(str *Char) { 54 | C.chai2010_cgo_String_AssignStr((*C.chai2010_cgo_String_T)(s), (*C.char)(str)) 55 | } 56 | func (s *String) AssignStrN(str *Char, n int) { 57 | C.chai2010_cgo_String_AssignStrN((*C.chai2010_cgo_String_T)(s), (*C.char)(str), C.int(n)) 58 | } 59 | 60 | func (s *String) AssignByGoString(str string) { 61 | C.chai2010_cgo_String_AssignStrN((*C.chai2010_cgo_String_T)(s), 62 | (*C.char)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&str)).Data)), 63 | C.int(len(str)), 64 | ) 65 | } 66 | func (s *String) AssignByGoBytes(str []byte) { 67 | C.chai2010_cgo_String_AssignStrN((*C.chai2010_cgo_String_T)(s), 68 | (*C.char)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&str)).Data)), 69 | C.int(len(str)), 70 | ) 71 | } 72 | 73 | func (s *String) GoString() string { 74 | return C.GoString((*C.char)(s.Data())) 75 | } 76 | 77 | func (s *String) GoSlice() []byte { 78 | if p, n := s.Data(), s.Size(); n > 0 { 79 | return (*[1 << 31]byte)(unsafe.Pointer(p))[0:n:n] 80 | } 81 | return nil 82 | } 83 | -------------------------------------------------------------------------------- /char.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | //#include 8 | //#include 9 | import "C" 10 | 11 | import ( 12 | "fmt" 13 | "unsafe" 14 | ) 15 | 16 | // Go string to C string 17 | // The C string is allocated in the C heap using malloc. 18 | // It is the caller's responsibility to arrange for it to be 19 | // freed, such as by calling C.free (be sure to include stdlib.h 20 | // if C.free is needed). 21 | func NewCharString(s string) *Char { 22 | return (*Char)(C.CString(s)) 23 | } 24 | 25 | func NewCharFormat(format string, args ...interface{}) *Char { 26 | s := fmt.Sprintf(format, args...) 27 | return (*Char)(C.CString(s)) 28 | } 29 | 30 | func (s *Char) IsEmpty() bool { 31 | return s == nil || *s == 0 32 | } 33 | 34 | // C string to Go string 35 | func (s *Char) GoString() string { 36 | return C.GoString((*C.char)(s)) 37 | } 38 | 39 | // C data with explicit length to Go string 40 | func (s *Char) GoStringN(n int) string { 41 | return C.GoStringN((*C.char)(s), C.int(n)) 42 | } 43 | 44 | func (s *Char) GoBytes(n int) []byte { 45 | return C.GoBytes(unsafe.Pointer(s), C.int(n)) 46 | } 47 | 48 | func (s *Char) Strlen() int { 49 | return int(C.strlen((*C.char)(s))) 50 | } 51 | 52 | func (s *Char) Strdup() *Char { 53 | d := (*Char)(C.malloc(C.strlen((*C.char)(s)) + 1)) 54 | if d == nil { 55 | return nil 56 | } 57 | C.strcpy((*C.char)(d), (*C.char)(s)) 58 | return d 59 | } 60 | 61 | func (dst *Char) Strcpy(src *Char) *Char { 62 | return (*Char)(C.strcpy((*C.char)(dst), (*C.char)(src))) 63 | } 64 | 65 | func (dst *Char) Strncpy(src *Char, num int) *Char { 66 | return (*Char)(C.strncpy((*C.char)(dst), (*C.char)(src), C.size_t(num))) 67 | } 68 | 69 | func (dst *Char) Strcat(src *Char) *Char { 70 | return (*Char)(C.strcat((*C.char)(dst), (*C.char)(src))) 71 | } 72 | 73 | func (dst *Char) Strncat(src *Char, num int) *Char { 74 | return (*Char)(C.strncat((*C.char)(dst), (*C.char)(src), C.size_t(num))) 75 | } 76 | 77 | func (s *Char) Strchr(ch int) *Char { 78 | return (*Char)(C.strchr((*C.char)(s), C.int(ch))) 79 | } 80 | 81 | func (s *Char) Strrchr(ch int) *Char { 82 | return (*Char)(C.strrchr((*C.char)(s), (C.int)(ch))) 83 | } 84 | 85 | func (s *Char) Strstr(s2 *Char) *Char { 86 | return (*Char)(C.strstr((*C.char)(s), (*C.char)(s2))) 87 | } 88 | 89 | func (s *Char) Strcspn(s2 *Char) int { 90 | return int(C.strcspn((*C.char)(s), (*C.char)(s2))) 91 | } 92 | 93 | func (s *Char) Strspn(s2 *Char) int { 94 | return (int)(C.strspn((*C.char)(s), (*C.char)(s2))) 95 | } 96 | 97 | func (s *Char) Strpbrk(s2 *Char) *Char { 98 | return (*Char)(C.strpbrk((*C.char)(s), (*C.char)(s2))) 99 | } 100 | 101 | func (s *Char) Strtok(delimiters *Char) *Char { 102 | return (*Char)(C.strtok((*C.char)(s), (*C.char)(delimiters))) 103 | } 104 | -------------------------------------------------------------------------------- /c_string.h: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 | // https://github.com/chai2010/cgo 6 | 7 | #pragma once 8 | 9 | #ifndef CHAI2010_CGO_C_STRING_H_ 10 | #define CHAI2010_CGO_C_STRING_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #if defined(__cplusplus) 18 | extern "C" { 19 | #endif 20 | 21 | // ---------------------------------------------------------------------------- 22 | // types 23 | // ---------------------------------------------------------------------------- 24 | 25 | typedef struct chai2010_cgo_String_T chai2010_cgo_String_T; 26 | typedef struct chai2010_cgo_StringList_T chai2010_cgo_StringList_T; 27 | 28 | // ---------------------------------------------------------------------------- 29 | // string 30 | // ---------------------------------------------------------------------------- 31 | 32 | chai2010_cgo_String_T* chai2010_cgo_String_New(); 33 | chai2010_cgo_String_T* chai2010_cgo_String_NewFromStr(const char* str); 34 | chai2010_cgo_String_T* chai2010_cgo_String_NewFromStrN(const char* str, int n); 35 | void chai2010_cgo_String_Delete(chai2010_cgo_String_T* p); 36 | 37 | int chai2010_cgo_String_Size(chai2010_cgo_String_T* p); 38 | void chai2010_cgo_String_Resize(chai2010_cgo_String_T* p, int newSize); 39 | void chai2010_cgo_String_Clear(chai2010_cgo_String_T* p); 40 | 41 | const char* chai2010_cgo_String_CStr(chai2010_cgo_String_T* p); 42 | const char* chai2010_cgo_String_Data(chai2010_cgo_String_T* p); 43 | 44 | void chai2010_cgo_String_AssignStr(chai2010_cgo_String_T* p, const char* str); 45 | void chai2010_cgo_String_AssignStrN(chai2010_cgo_String_T* p, const char* str, int size); 46 | 47 | // ---------------------------------------------------------------------------- 48 | // string list 49 | // ---------------------------------------------------------------------------- 50 | 51 | chai2010_cgo_StringList_T* chai2010_cgo_StringList_New(); 52 | void chai2010_cgo_StringList_Delete(chai2010_cgo_StringList_T* p); 53 | 54 | int chai2010_cgo_StringList_Size(chai2010_cgo_StringList_T* p); 55 | void chai2010_cgo_StringList_Resize(chai2010_cgo_StringList_T* p, int newSize); 56 | void chai2010_cgo_StringList_Clear(chai2010_cgo_StringList_T* p); 57 | 58 | int chai2010_cgo_StringList_StrSize(chai2010_cgo_StringList_T* p, int i); 59 | void chai2010_cgo_StringList_StrResize(chai2010_cgo_StringList_T* p, int i, int newSize); 60 | void chai2010_cgo_StringList_StrClear(chai2010_cgo_StringList_T* p, int i); 61 | 62 | const char* chai2010_cgo_StringList_StrData(chai2010_cgo_StringList_T* p, int i); 63 | const char* chai2010_cgo_StringList_StrCStr(chai2010_cgo_StringList_T* p, int i); 64 | 65 | void chai2010_cgo_StringList_AssignStr(chai2010_cgo_StringList_T* p, int i, const char* str); 66 | void chai2010_cgo_StringList_AssignStrN(chai2010_cgo_StringList_T* p, int i, const char* str, int size); 67 | 68 | void chai2010_cgo_StringList_AppendStr(chai2010_cgo_StringList_T* p, const char* str); 69 | void chai2010_cgo_StringList_AppendStrN(chai2010_cgo_StringList_T* p, const char* str, int size); 70 | 71 | // ---------------------------------------------------------------------------- 72 | // END 73 | // ---------------------------------------------------------------------------- 74 | 75 | #if defined(__cplusplus) 76 | } 77 | #endif 78 | #endif // CHAI2010_CGO_C_STRING_H_ 79 | -------------------------------------------------------------------------------- /void_pointer.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | //#include 8 | //#include 9 | import "C" 10 | 11 | import ( 12 | "unsafe" 13 | ) 14 | 15 | // Go []byte slice to C array 16 | // The C array is allocated in the C heap using malloc. 17 | // It is the caller's responsibility to arrange for it to be 18 | // freed, such as by calling C.free (be sure to include stdlib.h 19 | // if C.free is needed). 20 | func NewBytes(s []byte) VoidPointer { 21 | p := Malloc(len(s)) 22 | copy(p.ByteSlice(len(s)), s) 23 | return p 24 | } 25 | 26 | func Malloc(n int) VoidPointer { 27 | return VoidPointer(C.malloc(C.size_t(n))) 28 | } 29 | 30 | func Calloc(num, size int) VoidPointer { 31 | return VoidPointer(C.calloc(C.size_t(num), C.size_t(size))) 32 | } 33 | 34 | func (p *VoidPointer) Realloc(newSize int) VoidPointer { 35 | *p = VoidPointer(C.realloc(unsafe.Pointer(*p), C.size_t(newSize))) 36 | return *p 37 | } 38 | 39 | func (p VoidPointer) Free() { 40 | C.free(unsafe.Pointer(p)) 41 | } 42 | 43 | // C string to Go string 44 | func (p VoidPointer) GoString() string { 45 | return (*Char)(unsafe.Pointer(p)).GoString() 46 | } 47 | 48 | // C data with explicit length to Go string 49 | func (p VoidPointer) GoStringN(n int) string { 50 | return (*Char)(unsafe.Pointer(p)).GoStringN(n) 51 | } 52 | 53 | // C data with explicit length to Go []byte 54 | func (p VoidPointer) GoBytes(n int) []byte { 55 | return C.GoBytes(unsafe.Pointer(p), C.int(n)) 56 | } 57 | 58 | func (p VoidPointer) ByteSlice(n int) []byte { 59 | return ((*[1 << 31]byte)(unsafe.Pointer(p)))[0:n:n] 60 | } 61 | 62 | func (p VoidPointer) Int8Slice(n int) []int8 { 63 | return ((*[1 << 31]int8)(unsafe.Pointer(p)))[0:n:n] 64 | } 65 | func (p VoidPointer) Int16Slice(n int) []int16 { 66 | return ((*[1 << 30]int16)(unsafe.Pointer(p)))[0:n:n] 67 | } 68 | func (p VoidPointer) Int32Slice(n int) []int32 { 69 | return ((*[1 << 29]int32)(unsafe.Pointer(p)))[0:n:n] 70 | } 71 | func (p VoidPointer) Int64Slice(n int) []int64 { 72 | return ((*[1 << 28]int64)(unsafe.Pointer(p)))[0:n:n] 73 | } 74 | 75 | func (p VoidPointer) UInt8Slice(n int) []uint8 { 76 | return ((*[1 << 31]uint8)(unsafe.Pointer(p)))[0:n:n] 77 | } 78 | func (p VoidPointer) UInt16Slice(n int) []uint16 { 79 | return ((*[1 << 30]uint16)(unsafe.Pointer(p)))[0:n:n] 80 | } 81 | func (p VoidPointer) UInt32Slice(n int) []uint32 { 82 | return ((*[1 << 29]uint32)(unsafe.Pointer(p)))[0:n:n] 83 | } 84 | func (p VoidPointer) UInt64Slice(n int) []uint64 { 85 | return ((*[1 << 28]uint64)(unsafe.Pointer(p)))[0:n:n] 86 | } 87 | 88 | func (p VoidPointer) Float32Slice(n int) []float32 { 89 | return ((*[1 << 29]float32)(unsafe.Pointer(p)))[0:n:n] 90 | } 91 | func (p VoidPointer) Float64Slice(n int) []float64 { 92 | return ((*[1 << 28]float64)(unsafe.Pointer(p)))[0:n:n] 93 | } 94 | 95 | func (p VoidPointer) Memcpy(src VoidPointer, num int) VoidPointer { 96 | return VoidPointer(C.memcpy(unsafe.Pointer(p), unsafe.Pointer(src), C.size_t(num))) 97 | } 98 | 99 | func (p VoidPointer) Memset(value, num int) VoidPointer { 100 | return VoidPointer(C.memset(unsafe.Pointer(p), C.int(value), C.size_t(num))) 101 | } 102 | 103 | func (p VoidPointer) Memmove(src VoidPointer, num int) VoidPointer { 104 | return VoidPointer(C.memmove(unsafe.Pointer(p), unsafe.Pointer(src), C.size_t(num))) 105 | } 106 | 107 | func (p VoidPointer) Memchr(value, num int) VoidPointer { 108 | return VoidPointer(C.memchr(unsafe.Pointer(p), C.int(value), C.size_t(num))) 109 | } 110 | -------------------------------------------------------------------------------- /string_list.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | //#include "./c_string.h" 8 | import "C" 9 | 10 | import ( 11 | "reflect" 12 | "unsafe" 13 | ) 14 | 15 | type StringList C.chai2010_cgo_StringList_T 16 | 17 | func NewStringList(args ...string) *StringList { 18 | p := (*StringList)(C.chai2010_cgo_StringList_New()) 19 | p.Resize(len(args)) 20 | for i, s := range args { 21 | p.AssignByGoString(i, s) 22 | } 23 | return p 24 | } 25 | func (p *StringList) Delete() { 26 | C.chai2010_cgo_StringList_Delete((*C.chai2010_cgo_StringList_T)(p)) 27 | } 28 | 29 | func (p *StringList) Size() int { 30 | return int(C.chai2010_cgo_StringList_Size((*C.chai2010_cgo_StringList_T)(p))) 31 | } 32 | func (p *StringList) Resize(newSize int) { 33 | C.chai2010_cgo_StringList_Resize((*C.chai2010_cgo_StringList_T)(p), C.int(newSize)) 34 | } 35 | func (p *StringList) Clear() { 36 | C.chai2010_cgo_StringList_Clear((*C.chai2010_cgo_StringList_T)(p)) 37 | } 38 | 39 | func (p *StringList) StrSize(i int) int { 40 | return int(C.chai2010_cgo_StringList_StrSize((*C.chai2010_cgo_StringList_T)(p), C.int(i))) 41 | } 42 | func (p *StringList) StrResize(i, newSize int) { 43 | C.chai2010_cgo_StringList_StrResize((*C.chai2010_cgo_StringList_T)(p), C.int(i), C.int(newSize)) 44 | } 45 | func (p *StringList) StrClear(i int) { 46 | C.chai2010_cgo_StringList_StrClear((*C.chai2010_cgo_StringList_T)(p), C.int(i)) 47 | } 48 | 49 | func (p *StringList) StrData(i int) *Char { 50 | return (*Char)(C.chai2010_cgo_StringList_StrData((*C.chai2010_cgo_StringList_T)(p), C.int(i))) 51 | } 52 | func (p *StringList) StrCStr(i int) *Char { 53 | return (*Char)(C.chai2010_cgo_StringList_StrCStr((*C.chai2010_cgo_StringList_T)(p), C.int(i))) 54 | } 55 | 56 | func (p *StringList) StrGoString(i int) string { 57 | return C.GoString((*C.char)(p.StrData(i))) 58 | } 59 | func (p *StringList) StrGoData(i int) []byte { 60 | if p, n := p.StrData(i), p.StrSize(i); n > 0 { 61 | return (*[1 << 31]byte)(unsafe.Pointer(p))[0:n:n] 62 | } 63 | return nil 64 | } 65 | 66 | func (p *StringList) AssignStr(i int, s *Char) { 67 | C.chai2010_cgo_StringList_AssignStr((*C.chai2010_cgo_StringList_T)(p), C.int(i), (*C.char)(s)) 68 | } 69 | func (p *StringList) AssignStrN(i int, s *Char, n int) { 70 | C.chai2010_cgo_StringList_AssignStrN((*C.chai2010_cgo_StringList_T)(p), C.int(i), (*C.char)(s), C.int(n)) 71 | } 72 | 73 | func (p *StringList) AssignByGoString(i int, s string) { 74 | C.chai2010_cgo_StringList_AssignStrN((*C.chai2010_cgo_StringList_T)(p), C.int(i), 75 | (*C.char)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)), 76 | C.int(len(s)), 77 | ) 78 | } 79 | func (p *StringList) AssignByGoSlice(i int, s []byte) { 80 | C.chai2010_cgo_StringList_AssignStrN((*C.chai2010_cgo_StringList_T)(p), C.int(i), 81 | (*C.char)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s)).Data)), 82 | C.int(len(s)), 83 | ) 84 | } 85 | 86 | func (p *StringList) AppendStr(str *Char) { 87 | C.chai2010_cgo_StringList_AppendStr((*C.chai2010_cgo_StringList_T)(p), (*C.char)(str)) 88 | } 89 | func (p *StringList) AppendStrN(str *Char, n int) { 90 | C.chai2010_cgo_StringList_AppendStrN((*C.chai2010_cgo_StringList_T)(p), (*C.char)(str), C.int(n)) 91 | } 92 | 93 | func (p *StringList) AppendByGoString(s string) { 94 | C.chai2010_cgo_StringList_AppendStrN((*C.chai2010_cgo_StringList_T)(p), 95 | (*C.char)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)), 96 | C.int(len(s)), 97 | ) 98 | } 99 | func (p *StringList) AppendByGoSlice(s []byte) { 100 | C.chai2010_cgo_StringList_AppendStrN((*C.chai2010_cgo_StringList_T)(p), 101 | (*C.char)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s)).Data)), 102 | C.int(len(s)), 103 | ) 104 | } 105 | -------------------------------------------------------------------------------- /c_string.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 | #include "./c_string.h" 6 | 7 | #include 8 | #include 9 | 10 | // ---------------------------------------------------------------------------- 11 | // types 12 | // ---------------------------------------------------------------------------- 13 | 14 | struct chai2010_cgo_String_T { 15 | std::string v; 16 | }; 17 | 18 | struct chai2010_cgo_StringList_T { 19 | std::vector v; 20 | }; 21 | 22 | // ---------------------------------------------------------------------------- 23 | // string 24 | // ---------------------------------------------------------------------------- 25 | 26 | chai2010_cgo_String_T* chai2010_cgo_String_New() { 27 | return new chai2010_cgo_String_T(); 28 | } 29 | chai2010_cgo_String_T* chai2010_cgo_String_NewFromStr(const char* str) { 30 | auto p = new chai2010_cgo_String_T(); 31 | p->v.assign(str); 32 | return p; 33 | } 34 | chai2010_cgo_String_T* chai2010_cgo_String_NewFromStrN(const char* str, int n) { 35 | auto p = new chai2010_cgo_String_T(); 36 | p->v.assign(str, size_t(n)); 37 | return p; 38 | } 39 | void chai2010_cgo_String_Delete(chai2010_cgo_String_T* p) { 40 | delete p; 41 | } 42 | 43 | int chai2010_cgo_String_Size(chai2010_cgo_String_T* p) { 44 | return p->v.size(); 45 | } 46 | void chai2010_cgo_String_Resize(chai2010_cgo_String_T* p, int newSize) { 47 | p->v.resize(newSize); 48 | } 49 | void chai2010_cgo_String_Clear(chai2010_cgo_String_T* p) { 50 | p->v.clear(); 51 | } 52 | 53 | const char* chai2010_cgo_String_CStr(chai2010_cgo_String_T* p) { 54 | return p->v.data(); 55 | } 56 | const char* chai2010_cgo_String_Data(chai2010_cgo_String_T* p) { 57 | return p->v.c_str(); 58 | } 59 | 60 | void chai2010_cgo_String_AssignStr(chai2010_cgo_String_T* p, const char* str) { 61 | p->v.assign(str); 62 | } 63 | void chai2010_cgo_String_AssignStrN(chai2010_cgo_String_T* p, const char* str, int size) { 64 | p->v.assign(str, size_t(size)); 65 | } 66 | 67 | // ---------------------------------------------------------------------------- 68 | // string list 69 | // ---------------------------------------------------------------------------- 70 | 71 | chai2010_cgo_StringList_T* chai2010_cgo_StringList_New() { 72 | return new chai2010_cgo_StringList_T(); 73 | } 74 | void chai2010_cgo_StringList_Delete(chai2010_cgo_StringList_T* p) { 75 | delete p; 76 | } 77 | 78 | int chai2010_cgo_StringList_Size(chai2010_cgo_StringList_T* p) { 79 | return p->v.size(); 80 | } 81 | void chai2010_cgo_StringList_Resize(chai2010_cgo_StringList_T* p, int newSize) { 82 | p->v.resize(newSize); 83 | } 84 | void chai2010_cgo_StringList_Clear(chai2010_cgo_StringList_T* p) { 85 | p->v.clear(); 86 | } 87 | 88 | int chai2010_cgo_StringList_StrSize(chai2010_cgo_StringList_T* p, int i) { 89 | return p->v[i].size(); 90 | } 91 | void chai2010_cgo_StringList_StrResize(chai2010_cgo_StringList_T* p, int i, int newSize) { 92 | p->v[i].resize(newSize); 93 | } 94 | void chai2010_cgo_StringList_StrClear(chai2010_cgo_StringList_T* p, int i) { 95 | p->v[i].clear(); 96 | } 97 | 98 | const char* chai2010_cgo_StringList_StrData(chai2010_cgo_StringList_T* p, int i) { 99 | return p->v[i].data(); 100 | } 101 | const char* chai2010_cgo_StringList_StrCStr(chai2010_cgo_StringList_T* p, int i) { 102 | return p->v[i].c_str(); 103 | } 104 | 105 | void chai2010_cgo_StringList_AssignStr(chai2010_cgo_StringList_T* p, int i, const char* str) { 106 | p->v[i].assign(str); 107 | } 108 | void chai2010_cgo_StringList_AssignStrN(chai2010_cgo_StringList_T* p, int i, const char* str, int size) { 109 | p->v[i].assign(str, size_t(size)); 110 | } 111 | 112 | void chai2010_cgo_StringList_AppendStr(chai2010_cgo_StringList_T* p, const char* str) { 113 | p->v.push_back(std::string(str)); 114 | } 115 | void chai2010_cgo_StringList_AppendStrN(chai2010_cgo_StringList_T* p, const char* str, int size) { 116 | p->v.push_back(std::string(str, size_t(size))); 117 | } 118 | -------------------------------------------------------------------------------- /file.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | /* 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // on mingw, stderr and stdout are defined as &_iob[FILENO] 14 | // on netbsd, they are defined as &__sF[FILENO] 15 | // and cgo doesn't recognize them, so write a function to get them, 16 | // instead of depending on internals of libc implementation. 17 | 18 | static FILE *cgo_get_stdin(void) { return stdin; } 19 | static FILE *cgo_get_stdout(void) { return stdout; } 20 | static FILE *cgo_get_stderr(void) { return stderr; } 21 | 22 | */ 23 | import "C" 24 | 25 | import ( 26 | "errors" 27 | "io" 28 | "unsafe" 29 | ) 30 | 31 | type File C.FILE 32 | 33 | var ( 34 | _ io.Seeker = (*File)(nil) 35 | _ io.Reader = (*File)(nil) 36 | _ io.Writer = (*File)(nil) 37 | _ io.Closer = (*File)(nil) 38 | 39 | _ io.ByteReader = (*File)(nil) 40 | _ io.ByteWriter = (*File)(nil) 41 | ) 42 | 43 | const ( 44 | EOF = C.EOF 45 | 46 | SEEK_CUR = C.SEEK_CUR 47 | SEEK_SET = C.SEEK_SET 48 | SEEK_END = C.SEEK_END 49 | ) 50 | 51 | var ( 52 | Stdin = (*File)(C.cgo_get_stdin()) 53 | Stdout = (*File)(C.cgo_get_stdout()) 54 | Stderr = (*File)(C.cgo_get_stderr()) 55 | ) 56 | 57 | func OpenFile(filename, mode *Char) *File { 58 | return (*File)(C.fopen((*C.char)(filename), (*C.char)(mode))) 59 | } 60 | 61 | func TempFile() *File { 62 | return (*File)(C.tmpfile()) 63 | } 64 | 65 | func (f *File) Close() error { 66 | n := int(C.fclose((*C.FILE)(f))) 67 | if n != 0 { 68 | return io.EOF 69 | } 70 | return nil 71 | } 72 | 73 | func (f *File) Flush() { 74 | C.fflush((*C.FILE)(f)) 75 | } 76 | 77 | func (f *File) Read(buf []byte) (int, error) { 78 | n := int(C.fread(unsafe.Pointer(&buf[0]), C.size_t(1), C.size_t(len(buf)), (*C.FILE)(f))) 79 | if n == len(buf) { 80 | return n, nil 81 | } 82 | if f.Eof() != 0 { 83 | return 0, io.EOF 84 | } 85 | err := errors.New(C.GoString(C.strerror(C.int(f.Error())))) 86 | return 0, err 87 | } 88 | 89 | func (f *File) Write(buf []byte) (int, error) { 90 | n := int(C.fwrite(unsafe.Pointer(&buf[0]), C.size_t(1), C.size_t(len(buf)), (*C.FILE)(f))) 91 | if n == len(buf) { 92 | return n, nil 93 | } 94 | if f.Eof() != 0 { 95 | return 0, io.EOF 96 | } 97 | err := errors.New(C.GoString(C.strerror(C.int(f.Error())))) 98 | return 0, err 99 | } 100 | 101 | func (f *File) ReadByte() (c byte, err error) { 102 | if ch := f.Getc(); ch >= 0 { 103 | return byte(ch), nil 104 | } 105 | if f.Eof() != 0 { 106 | return 0, io.EOF 107 | } 108 | err = errors.New(C.GoString(C.strerror(C.int(f.Error())))) 109 | return 0, err 110 | } 111 | func (f *File) WriteByte(c byte) error { 112 | if n := f.Putc(int(c)); n != int(C.EOF) { 113 | return nil 114 | } 115 | if f.Eof() != 0 { 116 | return io.EOF 117 | } 118 | err := errors.New(C.GoString(C.strerror(C.int(f.Error())))) 119 | return err 120 | } 121 | 122 | func (f *File) Puts(s *Char) { 123 | C.fputs((*C.char)(s), (*C.FILE)(f)) 124 | } 125 | 126 | func (f *File) Getc() int { 127 | return int(C.fgetc((*C.FILE)(f))) 128 | } 129 | 130 | func (f *File) Gets(buf []byte) *Char { 131 | return (*Char)(C.fgets((*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)), (*C.FILE)(f))) 132 | } 133 | 134 | func (f *File) Putc(ch int) int { 135 | return int(C.fputc(C.int(ch), (*C.FILE)(f))) 136 | } 137 | 138 | func (f *File) Ungetc(ch int) int { 139 | return int(C.ungetc(C.int(ch), (*C.FILE)(f))) 140 | } 141 | 142 | func (f *File) Tell() int64 { 143 | return int64(C.ftell((*C.FILE)(f))) 144 | } 145 | func (f *File) Seek(off int64, origin int) (int64, error) { 146 | n := int(C.fseek((*C.FILE)(f), C.long(off), C.int(origin))) 147 | if n == 0 { 148 | return f.Tell(), nil 149 | } 150 | err := errors.New(C.GoString(C.strerror(C.int(f.Error())))) 151 | return 0, err 152 | } 153 | 154 | func (f *File) Rewind() { 155 | C.rewind((*C.FILE)(f)) 156 | } 157 | 158 | func (f *File) Eof() int { 159 | return int(C.feof((*C.FILE)(f))) 160 | } 161 | func (f *File) Error() int { 162 | return int(C.ferror((*C.FILE)(f))) 163 | } 164 | func (f *File) ClearErr() { 165 | C.clearerr((*C.FILE)(f)) 166 | } 167 | -------------------------------------------------------------------------------- /types.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 . 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 cgo 6 | 7 | //#include 8 | //#include 9 | import "C" 10 | import "unsafe" 11 | 12 | type ( 13 | Bool C.char 14 | Char C.char 15 | Int C.int 16 | UInt C.uint 17 | Float C.float 18 | Double C.double 19 | SizeT C.size_t 20 | 21 | Int8 C.int8_t 22 | Int16 C.int16_t 23 | Int32 C.int32_t 24 | Int64 C.int64_t 25 | IntPtr C.intptr_t 26 | 27 | UInt8 C.uint8_t 28 | UInt16 C.uint16_t 29 | UInt32 C.uint32_t 30 | UInt64 C.uint64_t 31 | UIntPtr C.uintptr_t 32 | 33 | VoidPointer uintptr 34 | ) 35 | 36 | // ----------------------------------------------------------------------------- 37 | 38 | func bool2Byte(b bool) byte { 39 | if b { 40 | return 1 41 | } 42 | return 0 43 | } 44 | 45 | func NewBool(firstValue bool, moreValues ...bool) *Bool { 46 | n := len(moreValues) + 1 47 | p := NewBoolN(n) 48 | s := p.Slice(n) 49 | 50 | s[0] = bool2Byte(firstValue) 51 | for i, v := range moreValues { 52 | s[i+1] = bool2Byte(v) 53 | } 54 | return p 55 | } 56 | 57 | func NewBoolN(n int) *Bool { 58 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Char(0)))) 59 | return (*Bool)(p) 60 | } 61 | 62 | func (s *Bool) First() bool { 63 | return *s != 0 64 | } 65 | 66 | func (s *Bool) Slice(n int) []byte { 67 | return ((*[1 << 31]byte)(unsafe.Pointer(s)))[0:n:n] 68 | } 69 | 70 | func (s *Bool) Free() { 71 | C.free(unsafe.Pointer(s)) 72 | } 73 | 74 | // ----------------------------------------------------------------------------- 75 | 76 | func NewChar(firstValue int, moreValues ...int) *Char { 77 | n := len(moreValues) + 1 78 | p := NewCharN(n) 79 | s := p.Slice(n) 80 | 81 | s[0] = byte(firstValue) 82 | for i, v := range moreValues { 83 | s[i+1] = byte(v) 84 | } 85 | return p 86 | } 87 | 88 | func NewCharN(n int) *Char { 89 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Char(0)))) 90 | return (*Char)(p) 91 | } 92 | 93 | func (s *Char) First() byte { 94 | return byte(*s) 95 | } 96 | 97 | func (s *Char) Slice(n int) []byte { 98 | if n == 0 { 99 | n = s.Strlen() 100 | } 101 | return ((*[1 << 31]byte)(unsafe.Pointer(s)))[0:n:n] 102 | } 103 | 104 | func (s *Char) Free() { 105 | C.free(unsafe.Pointer(s)) 106 | } 107 | 108 | // ----------------------------------------------------------------------------- 109 | 110 | func NewInt(firstValue int, moreValues ...int) *Int { 111 | n := len(moreValues) + 1 112 | p := NewIntN(n) 113 | s := p.Slice(n) 114 | 115 | s[0] = Int(firstValue) 116 | for i, v := range moreValues { 117 | s[i+1] = Int(v) 118 | } 119 | return p 120 | } 121 | 122 | func NewIntN(n int) *Int { 123 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int(0)))) 124 | return (*Int)(p) 125 | } 126 | 127 | func (p *Int) First() int { 128 | return int(*p) 129 | } 130 | 131 | func (p *Int) Slice(n int) []Int { 132 | return ((*[1 << 29]Int)(unsafe.Pointer(p)))[0:n:n] 133 | } 134 | 135 | func (p *Int) Free() { 136 | C.free(unsafe.Pointer(p)) 137 | } 138 | 139 | // ----------------------------------------------------------------------------- 140 | 141 | func NewUInt(firstValue uint, moreValues ...uint) *UInt { 142 | n := len(moreValues) + 1 143 | p := NewUIntN(n) 144 | s := p.Slice(n) 145 | 146 | s[0] = UInt(firstValue) 147 | for i, v := range moreValues { 148 | s[i+1] = UInt(v) 149 | } 150 | return p 151 | } 152 | 153 | func NewUIntN(n int) *UInt { 154 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt(0)))) 155 | return (*UInt)(p) 156 | } 157 | 158 | func (p *UInt) First() uint { 159 | return uint(*p) 160 | } 161 | 162 | func (p *UInt) Slice(n int) []UInt { 163 | return ((*[1 << 29]UInt)(unsafe.Pointer(p)))[0:n:n] 164 | } 165 | 166 | func (p *UInt) Free() { 167 | C.free(unsafe.Pointer(p)) 168 | } 169 | 170 | // ----------------------------------------------------------------------------- 171 | 172 | func NewFloat(firstValue float32, moreValues ...float32) *Float { 173 | n := len(moreValues) + 1 174 | p := NewFloatN(n) 175 | s := p.Slice(n) 176 | 177 | s[0] = firstValue 178 | for i, v := range moreValues { 179 | s[i+1] = v 180 | } 181 | return p 182 | } 183 | 184 | func NewFloatN(n int) *Float { 185 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Float(0)))) 186 | return (*Float)(p) 187 | } 188 | 189 | func (p *Float) First() float32 { 190 | return float32(*p) 191 | } 192 | 193 | func (p *Float) Slice(n int) []float32 { 194 | return ((*[1 << 29]float32)(unsafe.Pointer(p)))[0:n:n] 195 | } 196 | 197 | func (p *Float) Free() { 198 | C.free(unsafe.Pointer(p)) 199 | } 200 | 201 | // ----------------------------------------------------------------------------- 202 | 203 | func NewDouble(firstValue float64, moreValues ...float64) *Double { 204 | n := len(moreValues) + 1 205 | p := NewDoubleN(n) 206 | s := p.Slice(n) 207 | 208 | s[0] = firstValue 209 | for i, v := range moreValues { 210 | s[i+1] = v 211 | } 212 | return p 213 | } 214 | 215 | func NewDoubleN(n int) *Double { 216 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Double(0)))) 217 | return (*Double)(p) 218 | } 219 | 220 | func (p *Double) First() float64 { 221 | return float64(*p) 222 | } 223 | 224 | func (p *Double) Slice(n int) []float64 { 225 | return ((*[1 << 28]float64)(unsafe.Pointer(p)))[0:n:n] 226 | } 227 | 228 | func (p *Double) Free() { 229 | C.free(unsafe.Pointer(p)) 230 | } 231 | 232 | // ----------------------------------------------------------------------------- 233 | 234 | func NewSizeT(firstValue int, moreValues ...int) *SizeT { 235 | n := len(moreValues) + 1 236 | p := NewSizeTN(n) 237 | s := p.Slice(n) 238 | 239 | s[0] = SizeT(firstValue) 240 | for i, v := range moreValues { 241 | s[i+1] = SizeT(v) 242 | } 243 | return p 244 | } 245 | 246 | func NewSizeTN(n int) *SizeT { 247 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(SizeT(0)))) 248 | return (*SizeT)(p) 249 | } 250 | 251 | func (p *SizeT) First() int { 252 | return int(*p) 253 | } 254 | 255 | func (p *SizeT) Slice(n int) []SizeT { 256 | return ((*[1 << 28]SizeT)(unsafe.Pointer(p)))[0:n:n] 257 | } 258 | 259 | func (p *SizeT) Free() { 260 | C.free(unsafe.Pointer(p)) 261 | } 262 | 263 | // ----------------------------------------------------------------------------- 264 | 265 | func NewInt8(firstValue int8, moreValues ...int8) *Int8 { 266 | n := len(moreValues) + 1 267 | p := NewInt8N(n) 268 | s := p.Slice(n) 269 | 270 | s[0] = firstValue 271 | for i, v := range moreValues { 272 | s[i+1] = v 273 | } 274 | return p 275 | } 276 | 277 | func NewInt8N(n int) *Int8 { 278 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int8(0)))) 279 | return (*Int8)(p) 280 | } 281 | 282 | func (p *Int8) First() int8 { 283 | return int8(*p) 284 | } 285 | 286 | func (p *Int8) Slice(n int) []int8 { 287 | return ((*[1 << 31]int8)(unsafe.Pointer(p)))[0:n:n] 288 | } 289 | 290 | func (p *Int8) Free() { 291 | C.free(unsafe.Pointer(p)) 292 | } 293 | 294 | // ----------------------------------------------------------------------------- 295 | 296 | func NewUInt8(firstValue uint8, moreValues ...uint8) *UInt8 { 297 | n := len(moreValues) + 1 298 | p := NewUInt8N(n) 299 | s := p.Slice(n) 300 | 301 | s[0] = firstValue 302 | for i, v := range moreValues { 303 | s[i+1] = v 304 | } 305 | return p 306 | } 307 | 308 | func NewUInt8N(n int) *UInt8 { 309 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt8(0)))) 310 | return (*UInt8)(p) 311 | } 312 | 313 | func (p *UInt8) First() uint8 { 314 | return uint8(*p) 315 | } 316 | 317 | func (p *UInt8) Slice(n int) []uint8 { 318 | return ((*[1 << 31]uint8)(unsafe.Pointer(p)))[0:n:n] 319 | } 320 | 321 | func (p *UInt8) Free() { 322 | C.free(unsafe.Pointer(p)) 323 | } 324 | 325 | // ----------------------------------------------------------------------------- 326 | 327 | func NewInt16(firstValue int16, moreValues ...int16) *Int16 { 328 | n := len(moreValues) + 1 329 | p := NewInt16N(n) 330 | s := p.Slice(n) 331 | 332 | s[0] = firstValue 333 | for i, v := range moreValues { 334 | s[i+1] = v 335 | } 336 | return p 337 | } 338 | 339 | func NewInt16N(n int) *Int16 { 340 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int16(0)))) 341 | return (*Int16)(p) 342 | } 343 | 344 | func (p *Int16) First() int16 { 345 | return int16(*p) 346 | } 347 | 348 | func (p *Int16) Slice(n int) []int16 { 349 | return ((*[1 << 30]int16)(unsafe.Pointer(p)))[0:n:n] 350 | } 351 | 352 | func (p *Int16) Free() { 353 | C.free(unsafe.Pointer(p)) 354 | } 355 | 356 | // ----------------------------------------------------------------------------- 357 | 358 | func NewUInt16(firstValue uint16, moreValues ...uint16) *UInt16 { 359 | n := len(moreValues) + 1 360 | p := NewUInt16N(n) 361 | s := p.Slice(n) 362 | 363 | s[0] = firstValue 364 | for i, v := range moreValues { 365 | s[i+1] = v 366 | } 367 | return p 368 | } 369 | 370 | func NewUInt16N(n int) *UInt16 { 371 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt16(0)))) 372 | return (*UInt16)(p) 373 | } 374 | 375 | func (p *UInt16) First() uint16 { 376 | return uint16(*p) 377 | } 378 | 379 | func (p *UInt16) Slice(n int) []uint16 { 380 | return ((*[1 << 30]uint16)(unsafe.Pointer(p)))[0:n:n] 381 | } 382 | 383 | func (p *UInt16) Free() { 384 | C.free(unsafe.Pointer(p)) 385 | } 386 | 387 | // ----------------------------------------------------------------------------- 388 | 389 | func NewInt32(firstValue int32, moreValues ...int32) *Int32 { 390 | n := len(moreValues) + 1 391 | p := NewInt32N(n) 392 | s := p.Slice(n) 393 | 394 | s[0] = firstValue 395 | for i, v := range moreValues { 396 | s[i+1] = v 397 | } 398 | return p 399 | } 400 | 401 | func NewInt32N(n int) *Int32 { 402 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int32(0)))) 403 | return (*Int32)(p) 404 | } 405 | 406 | func (p *Int32) First() int32 { 407 | return int32(*p) 408 | } 409 | 410 | func (p *Int32) Slice(n int) []int32 { 411 | return ((*[1 << 29]int32)(unsafe.Pointer(p)))[0:n:n] 412 | } 413 | 414 | func (p *Int32) Free() { 415 | C.free(unsafe.Pointer(p)) 416 | } 417 | 418 | // ----------------------------------------------------------------------------- 419 | 420 | func NewUInt32(firstValue uint32, moreValues ...uint32) *UInt32 { 421 | n := len(moreValues) + 1 422 | p := NewUInt32N(n) 423 | s := p.Slice(n) 424 | 425 | s[0] = firstValue 426 | for i, v := range moreValues { 427 | s[i+1] = v 428 | } 429 | return p 430 | } 431 | 432 | func NewUInt32N(n int) *UInt32 { 433 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt32(0)))) 434 | return (*UInt32)(p) 435 | } 436 | 437 | func (p *UInt32) First() uint32 { 438 | return uint32(*p) 439 | } 440 | 441 | func (p *UInt32) Slice(n int) []uint32 { 442 | return ((*[1 << 29]uint32)(unsafe.Pointer(p)))[0:n:n] 443 | } 444 | 445 | func (p *UInt32) Free() { 446 | C.free(unsafe.Pointer(p)) 447 | } 448 | 449 | // ----------------------------------------------------------------------------- 450 | 451 | func NewInt64(firstValue int64, moreValues ...int64) *Int64 { 452 | n := len(moreValues) + 1 453 | p := NewInt64N(n) 454 | s := p.Slice(n) 455 | 456 | s[0] = firstValue 457 | for i, v := range moreValues { 458 | s[i+1] = v 459 | } 460 | return p 461 | } 462 | 463 | func NewInt64N(n int) *Int64 { 464 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(Int64(0)))) 465 | return (*Int64)(p) 466 | } 467 | 468 | func (p *Int64) First() int64 { 469 | return int64(*p) 470 | } 471 | 472 | func (p *Int64) Slice(n int) []int64 { 473 | return ((*[1 << 28]int64)(unsafe.Pointer(p)))[0:n:n] 474 | } 475 | 476 | func (p *Int64) Free() { 477 | C.free(unsafe.Pointer(p)) 478 | } 479 | 480 | // ----------------------------------------------------------------------------- 481 | 482 | func NewUInt64(firstValue uint64, moreValues ...uint64) *UInt64 { 483 | n := len(moreValues) + 1 484 | p := NewUInt64N(n) 485 | s := p.Slice(n) 486 | 487 | s[0] = firstValue 488 | for i, v := range moreValues { 489 | s[i+1] = v 490 | } 491 | return p 492 | } 493 | 494 | func NewUInt64N(n int) *UInt64 { 495 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UInt64(0)))) 496 | return (*UInt64)(p) 497 | } 498 | 499 | func (p *UInt64) First() uint64 { 500 | return uint64(*p) 501 | } 502 | 503 | func (p *UInt64) Slice(n int) []uint64 { 504 | return ((*[1 << 28]uint64)(unsafe.Pointer(p)))[0:n:n] 505 | } 506 | 507 | func (p *UInt64) Free() { 508 | C.free(unsafe.Pointer(p)) 509 | } 510 | 511 | // ----------------------------------------------------------------------------- 512 | 513 | func NewIntPtr(firstValue uintptr, moreValues ...uintptr) *IntPtr { 514 | n := len(moreValues) + 1 515 | p := NewIntPtrN(n) 516 | s := p.Slice(n) 517 | 518 | s[0] = IntPtr(firstValue) 519 | for i, v := range moreValues { 520 | s[i+1] = IntPtr(v) 521 | } 522 | return p 523 | } 524 | 525 | func NewIntPtrN(n int) *IntPtr { 526 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(IntPtr(0)))) 527 | return (*IntPtr)(p) 528 | } 529 | 530 | func (p *IntPtr) First() uintptr { 531 | return uintptr(*p) 532 | } 533 | 534 | func (p *IntPtr) Slice(n int) []IntPtr { 535 | return ((*[1 << 28]IntPtr)(unsafe.Pointer(p)))[0:n:n] 536 | } 537 | 538 | func (p *IntPtr) Free() { 539 | C.free(unsafe.Pointer(p)) 540 | } 541 | 542 | // ----------------------------------------------------------------------------- 543 | 544 | func NewUIntPtr(firstValue uintptr, moreValues ...uintptr) *UIntPtr { 545 | n := len(moreValues) + 1 546 | p := NewUIntPtrN(n) 547 | s := p.Slice(n) 548 | 549 | s[0] = UIntPtr(firstValue) 550 | for i, v := range moreValues { 551 | s[i+1] = UIntPtr(v) 552 | } 553 | return p 554 | } 555 | 556 | func NewUIntPtrN(n int) *UIntPtr { 557 | p := C.calloc(C.size_t(n), C.size_t(unsafe.Sizeof(UIntPtr(0)))) 558 | return (*UIntPtr)(p) 559 | } 560 | 561 | func (p *UIntPtr) First() uintptr { 562 | return uintptr(*p) 563 | } 564 | 565 | func (p *UIntPtr) Slice(n int) []UIntPtr { 566 | return ((*[1 << 28]UIntPtr)(unsafe.Pointer(p)))[0:n:n] 567 | } 568 | 569 | func (p *UIntPtr) Free() { 570 | C.free(unsafe.Pointer(p)) 571 | } 572 | 573 | // ----------------------------------------------------------------------------- 574 | --------------------------------------------------------------------------------