gitee.com/haifengat/gotap_dipper@v0.0.4-0.20231212021028-041a6fa876e5/generate/trade_lnx.go.tpl (about)

     1  package trade
     2  
     3  /*
     4  #cgo CPPFLAGS: -fPIC -I./
     5  #cgo linux LDFLAGS: -fPIC -L${SRCDIR} -Wl,-rpath ${SRCDIR} -ltaptrade -lstdc++
     6  
     7  #include "struct_cgo.h"
     8  
     9  void* CreateITapTradeAPINotify();
    10  void* CreateTradeAPI(struct TapAPIApplicationInfo *appInfo);
    11  void FreeITapTradeAPI(void* api);
    12  void* GetAPIVersion();
    13  void* GetErrorDescribe(int errorCode);
    14  
    15  // ********************** 调用函数 ******************
    16  [[ range .Fn ]]int [[ .FuncName ]](void* api[[ range .Params ]],[[ CHeaderBaseType .Type .Name]] [[ .Name ]][[end]]);
    17  [[ end ]]
    18  
    19  
    20  // ********************** 响应函数 ******************
    21  [[ range .On ]]void Set[[ .FuncName ]](void*, void*);
    22  [[ end ]]
    23  
    24  // Onxxx 为go 的 export所用
    25  [[ range .On ]]void ex[[ .FuncName ]]([[ range $i,$v := .Params ]][[ if gt $i 0 ]], [[ end ]] [[ CHeaderBaseType .Type .Name ]] [[ .Name ]][[end]]);
    26  [[ end ]]
    27  
    28  #include <stdlib.h>
    29  #include <stdint.h>
    30  */
    31  import "C"
    32  
    33  import (
    34  	"fmt"
    35  	"io"
    36  	"strings"
    37  	"unsafe"
    38  
    39  	"golang.org/x/text/encoding/simplifiedchinese"
    40  	"golang.org/x/text/transform"
    41  )
    42  
    43  func GBK(chars *C.char) string {
    44  	rd := transform.NewReader(strings.NewReader(C.GoString(chars)), simplifiedchinese.GBK.NewDecoder())
    45  	bs, _ := io.ReadAll(rd)
    46  	return string(bs)
    47  }
    48  
    49  var t *Trade
    50  func NewTrade()*Trade{
    51  	if t==nil {
    52  		t = &Trade{}
    53  	}
    54  	return t
    55  }
    56  
    57  // Trade 交易接口
    58  type Trade struct {
    59  	api, spi unsafe.Pointer
    60  	// 定义响应函数变量
    61  	[[ range .On ]][[ .FuncName ]] func([[ range $i,$v := .Params ]][[ if gt $i 0 ]], [[ end ]] [[ DefineVar .Type .Name ]][[end]])
    62  	[[ end ]]
    63  }
    64  
    65  func (t *Trade) CreateITapTradeAPINotify() {
    66  	t.spi = C.CreateITapTradeAPINotify()
    67  }
    68  func (t *Trade) CreateITapTradeAPI(appInfo *TapAPIApplicationInfo) {
    69  	t.api = C.CreateTradeAPI((*C.struct_TapAPIApplicationInfo)(unsafe.Pointer(appInfo)))
    70  }
    71  func (t *Trade)FreeITapTradeAPI(api unsafe.Pointer)  {
    72  	C.FreeITapTradeAPI(api)
    73  }
    74  func (t *Trade)GetITapTradeAPIVersion() string {
    75  	return C.GoString((*C.char)(C.GetAPIVersion()))
    76  }
    77  func (t *Trade) GetITapErrorDescribe(errorCode TAPIINT32) string {
    78  	return GBK((*C.char)(C.GetErrorDescribe(C.int(errorCode))))
    79  }
    80  
    81  // 替代 SetAPINotify
    82  func (t *Trade) SetSpi() { C.SetAPINotify(t.api, t.spi) }
    83  
    84  // ********************** 调用函数 ******************
    85  [[ range .Fn ]]
    86  func (t *Trade) [[ .FuncName ]]([[ range $i,$v := .Params ]][[ if gt $i 0 ]], [[ end ]][[ C2Go .Type .Name]][[ end ]]) C.int {return C.[[ .FuncName ]](t.api[[ range .Params ]], [[ Go2C .Type .Name ]][[ end ]])}
    87  [[ end ]]
    88  
    89  // ********************** 响应函数 ******************
    90  func (t *Trade)RegCallBack(){
    91  	[[ range .On ]]C.Set[[ .FuncName ]](t.spi, C.ex[[ .FuncName ]])
    92  	[[ end ]]
    93  }
    94  
    95  [[ range .On ]]
    96  //export ex[[ .FuncName ]]
    97  func ex[[ .FuncName ]]([[ range $i,$v := .Params ]][[ if gt $i 0 ]], [[ end ]] [[ CCB .Type .Name ]][[end]]){
    98  	if t.[[ .FuncName ]] != nil {
    99  		t.[[ .FuncName ]]([[ range $i,$v := .Params ]][[ if gt $i 0 ]], [[ end ]] [[ cgo .Type .Name ]][[end]])
   100  	} else {
   101  		fmt.Println("[[ .FuncName ]]")
   102  	}
   103  }
   104  [[ end ]]