github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/mobile/interface.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:43</date>
    10  //</624342654179610624>
    11  
    12  
    13  //包含扭曲的包装,以允许跨越空接口。
    14  
    15  package geth
    16  
    17  import (
    18  	"errors"
    19  	"math/big"
    20  
    21  	"github.com/ethereum/go-ethereum/common"
    22  )
    23  
    24  //接口表示Go接口的包装版本,具有
    25  //存储任意数据类型。
    26  //
    27  //因为在Go和Mobile之间无法转换任意性
    28  //平台,我们使用显式getter和setter进行转换。那里
    29  //当然,没有必要列举所有的东西,只是足以支持
    30  //合同绑定需要客户端生成的代码。
    31  type Interface struct {
    32  	object interface{}
    33  }
    34  
    35  //NewInterface创建一个新的空接口,可用于传递
    36  //泛型类型。
    37  func NewInterface() *Interface {
    38  	return new(Interface)
    39  }
    40  
    41  func (i *Interface) SetBool(b bool)                { i.object = &b }
    42  func (i *Interface) SetBools(bs []bool)            { i.object = &bs }
    43  func (i *Interface) SetString(str string)          { i.object = &str }
    44  func (i *Interface) SetStrings(strs *Strings)      { i.object = &strs.strs }
    45  func (i *Interface) SetBinary(binary []byte)       { b := common.CopyBytes(binary); i.object = &b }
    46  func (i *Interface) SetBinaries(binaries [][]byte) { i.object = &binaries }
    47  func (i *Interface) SetAddress(address *Address)   { i.object = &address.address }
    48  func (i *Interface) SetAddresses(addrs *Addresses) { i.object = &addrs.addresses }
    49  func (i *Interface) SetHash(hash *Hash)            { i.object = &hash.hash }
    50  func (i *Interface) SetHashes(hashes *Hashes)      { i.object = &hashes.hashes }
    51  func (i *Interface) SetInt8(n int8)                { i.object = &n }
    52  func (i *Interface) SetInt16(n int16)              { i.object = &n }
    53  func (i *Interface) SetInt32(n int32)              { i.object = &n }
    54  func (i *Interface) SetInt64(n int64)              { i.object = &n }
    55  func (i *Interface) SetUint8(bigint *BigInt)       { n := uint8(bigint.bigint.Uint64()); i.object = &n }
    56  func (i *Interface) SetUint16(bigint *BigInt)      { n := uint16(bigint.bigint.Uint64()); i.object = &n }
    57  func (i *Interface) SetUint32(bigint *BigInt)      { n := uint32(bigint.bigint.Uint64()); i.object = &n }
    58  func (i *Interface) SetUint64(bigint *BigInt)      { n := bigint.bigint.Uint64(); i.object = &n }
    59  func (i *Interface) SetBigInt(bigint *BigInt)      { i.object = &bigint.bigint }
    60  func (i *Interface) SetBigInts(bigints *BigInts)   { i.object = &bigints.bigints }
    61  
    62  func (i *Interface) SetDefaultBool()      { i.object = new(bool) }
    63  func (i *Interface) SetDefaultBools()     { i.object = new([]bool) }
    64  func (i *Interface) SetDefaultString()    { i.object = new(string) }
    65  func (i *Interface) SetDefaultStrings()   { i.object = new([]string) }
    66  func (i *Interface) SetDefaultBinary()    { i.object = new([]byte) }
    67  func (i *Interface) SetDefaultBinaries()  { i.object = new([][]byte) }
    68  func (i *Interface) SetDefaultAddress()   { i.object = new(common.Address) }
    69  func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) }
    70  func (i *Interface) SetDefaultHash()      { i.object = new(common.Hash) }
    71  func (i *Interface) SetDefaultHashes()    { i.object = new([]common.Hash) }
    72  func (i *Interface) SetDefaultInt8()      { i.object = new(int8) }
    73  func (i *Interface) SetDefaultInt16()     { i.object = new(int16) }
    74  func (i *Interface) SetDefaultInt32()     { i.object = new(int32) }
    75  func (i *Interface) SetDefaultInt64()     { i.object = new(int64) }
    76  func (i *Interface) SetDefaultUint8()     { i.object = new(uint8) }
    77  func (i *Interface) SetDefaultUint16()    { i.object = new(uint16) }
    78  func (i *Interface) SetDefaultUint32()    { i.object = new(uint32) }
    79  func (i *Interface) SetDefaultUint64()    { i.object = new(uint64) }
    80  func (i *Interface) SetDefaultBigInt()    { i.object = new(*big.Int) }
    81  func (i *Interface) SetDefaultBigInts()   { i.object = new([]*big.Int) }
    82  
    83  func (i *Interface) GetBool() bool            { return *i.object.(*bool) }
    84  func (i *Interface) GetBools() []bool         { return *i.object.(*[]bool) }
    85  func (i *Interface) GetString() string        { return *i.object.(*string) }
    86  func (i *Interface) GetStrings() *Strings     { return &Strings{*i.object.(*[]string)} }
    87  func (i *Interface) GetBinary() []byte        { return *i.object.(*[]byte) }
    88  func (i *Interface) GetBinaries() [][]byte    { return *i.object.(*[][]byte) }
    89  func (i *Interface) GetAddress() *Address     { return &Address{*i.object.(*common.Address)} }
    90  func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} }
    91  func (i *Interface) GetHash() *Hash           { return &Hash{*i.object.(*common.Hash)} }
    92  func (i *Interface) GetHashes() *Hashes       { return &Hashes{*i.object.(*[]common.Hash)} }
    93  func (i *Interface) GetInt8() int8            { return *i.object.(*int8) }
    94  func (i *Interface) GetInt16() int16          { return *i.object.(*int16) }
    95  func (i *Interface) GetInt32() int32          { return *i.object.(*int32) }
    96  func (i *Interface) GetInt64() int64          { return *i.object.(*int64) }
    97  func (i *Interface) GetUint8() *BigInt {
    98  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))}
    99  }
   100  func (i *Interface) GetUint16() *BigInt {
   101  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))}
   102  }
   103  func (i *Interface) GetUint32() *BigInt {
   104  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))}
   105  }
   106  func (i *Interface) GetUint64() *BigInt {
   107  	return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))}
   108  }
   109  func (i *Interface) GetBigInt() *BigInt   { return &BigInt{*i.object.(**big.Int)} }
   110  func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} }
   111  
   112  //接口是被包装的一般对象的切片。
   113  type Interfaces struct {
   114  	objects []interface{}
   115  }
   116  
   117  //NewInterfaces创建一个未初始化的接口切片。
   118  func NewInterfaces(size int) *Interfaces {
   119  	return &Interfaces{
   120  		objects: make([]interface{}, size),
   121  	}
   122  }
   123  
   124  //SIZE返回切片中的接口数。
   125  func (i *Interfaces) Size() int {
   126  	return len(i.objects)
   127  }
   128  
   129  //get从切片返回给定索引处的bigint。
   130  func (i *Interfaces) Get(index int) (iface *Interface, _ error) {
   131  	if index < 0 || index >= len(i.objects) {
   132  		return nil, errors.New("index out of bounds")
   133  	}
   134  	return &Interface{i.objects[index]}, nil
   135  }
   136  
   137  //set在切片中的给定索引处设置big int。
   138  func (i *Interfaces) Set(index int, object *Interface) error {
   139  	if index < 0 || index >= len(i.objects) {
   140  		return errors.New("index out of bounds")
   141  	}
   142  	i.objects[index] = object.object
   143  	return nil
   144  }
   145