github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/mobile/interface.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  //包含扭曲的包装,以允许跨越空接口。
    26  
    27  package geth
    28  
    29  import (
    30  	"errors"
    31  	"math/big"
    32  
    33  	"github.com/ethereum/go-ethereum/common"
    34  )
    35  
    36  //接口表示Go接口的包装版本,具有
    37  //存储任意数据类型。
    38  //
    39  //因为在Go和Mobile之间无法转换任意性
    40  //平台,我们使用显式getter和setter进行转换。那里
    41  //当然,没有必要列举所有的东西,只是足以支持
    42  //合同绑定需要客户端生成的代码。
    43  type Interface struct {
    44  	object interface{}
    45  }
    46  
    47  //NewInterface创建一个新的空接口,可用于传递
    48  //泛型类型。
    49  func NewInterface() *Interface {
    50  	return new(Interface)
    51  }
    52  
    53  func (i *Interface) SetBool(b bool)                { i.object = &b }
    54  func (i *Interface) SetBools(bs []bool)            { i.object = &bs }
    55  func (i *Interface) SetString(str string)          { i.object = &str }
    56  func (i *Interface) SetStrings(strs *Strings)      { i.object = &strs.strs }
    57  func (i *Interface) SetBinary(binary []byte)       { b := common.CopyBytes(binary); i.object = &b }
    58  func (i *Interface) SetBinaries(binaries [][]byte) { i.object = &binaries }
    59  func (i *Interface) SetAddress(address *Address)   { i.object = &address.address }
    60  func (i *Interface) SetAddresses(addrs *Addresses) { i.object = &addrs.addresses }
    61  func (i *Interface) SetHash(hash *Hash)            { i.object = &hash.hash }
    62  func (i *Interface) SetHashes(hashes *Hashes)      { i.object = &hashes.hashes }
    63  func (i *Interface) SetInt8(n int8)                { i.object = &n }
    64  func (i *Interface) SetInt16(n int16)              { i.object = &n }
    65  func (i *Interface) SetInt32(n int32)              { i.object = &n }
    66  func (i *Interface) SetInt64(n int64)              { i.object = &n }
    67  func (i *Interface) SetUint8(bigint *BigInt)       { n := uint8(bigint.bigint.Uint64()); i.object = &n }
    68  func (i *Interface) SetUint16(bigint *BigInt)      { n := uint16(bigint.bigint.Uint64()); i.object = &n }
    69  func (i *Interface) SetUint32(bigint *BigInt)      { n := uint32(bigint.bigint.Uint64()); i.object = &n }
    70  func (i *Interface) SetUint64(bigint *BigInt)      { n := bigint.bigint.Uint64(); i.object = &n }
    71  func (i *Interface) SetBigInt(bigint *BigInt)      { i.object = &bigint.bigint }
    72  func (i *Interface) SetBigInts(bigints *BigInts)   { i.object = &bigints.bigints }
    73  
    74  func (i *Interface) SetDefaultBool()      { i.object = new(bool) }
    75  func (i *Interface) SetDefaultBools()     { i.object = new([]bool) }
    76  func (i *Interface) SetDefaultString()    { i.object = new(string) }
    77  func (i *Interface) SetDefaultStrings()   { i.object = new([]string) }
    78  func (i *Interface) SetDefaultBinary()    { i.object = new([]byte) }
    79  func (i *Interface) SetDefaultBinaries()  { i.object = new([][]byte) }
    80  func (i *Interface) SetDefaultAddress()   { i.object = new(common.Address) }
    81  func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) }
    82  func (i *Interface) SetDefaultHash()      { i.object = new(common.Hash) }
    83  func (i *Interface) SetDefaultHashes()    { i.object = new([]common.Hash) }
    84  func (i *Interface) SetDefaultInt8()      { i.object = new(int8) }
    85  func (i *Interface) SetDefaultInt16()     { i.object = new(int16) }
    86  func (i *Interface) SetDefaultInt32()     { i.object = new(int32) }
    87  func (i *Interface) SetDefaultInt64()     { i.object = new(int64) }
    88  func (i *Interface) SetDefaultUint8()     { i.object = new(uint8) }
    89  func (i *Interface) SetDefaultUint16()    { i.object = new(uint16) }
    90  func (i *Interface) SetDefaultUint32()    { i.object = new(uint32) }
    91  func (i *Interface) SetDefaultUint64()    { i.object = new(uint64) }
    92  func (i *Interface) SetDefaultBigInt()    { i.object = new(*big.Int) }
    93  func (i *Interface) SetDefaultBigInts()   { i.object = new([]*big.Int) }
    94  
    95  func (i *Interface) GetBool() bool            { return *i.object.(*bool) }
    96  func (i *Interface) GetBools() []bool         { return *i.object.(*[]bool) }
    97  func (i *Interface) GetString() string        { return *i.object.(*string) }
    98  func (i *Interface) GetStrings() *Strings     { return &Strings{*i.object.(*[]string)} }
    99  func (i *Interface) GetBinary() []byte        { return *i.object.(*[]byte) }
   100  func (i *Interface) GetBinaries() [][]byte    { return *i.object.(*[][]byte) }
   101  func (i *Interface) GetAddress() *Address     { return &Address{*i.object.(*common.Address)} }
   102  func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} }
   103  func (i *Interface) GetHash() *Hash           { return &Hash{*i.object.(*common.Hash)} }
   104  func (i *Interface) GetHashes() *Hashes       { return &Hashes{*i.object.(*[]common.Hash)} }
   105  func (i *Interface) GetInt8() int8            { return *i.object.(*int8) }
   106  func (i *Interface) GetInt16() int16          { return *i.object.(*int16) }
   107  func (i *Interface) GetInt32() int32          { return *i.object.(*int32) }
   108  func (i *Interface) GetInt64() int64          { return *i.object.(*int64) }
   109  func (i *Interface) GetUint8() *BigInt {
   110  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))}
   111  }
   112  func (i *Interface) GetUint16() *BigInt {
   113  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))}
   114  }
   115  func (i *Interface) GetUint32() *BigInt {
   116  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))}
   117  }
   118  func (i *Interface) GetUint64() *BigInt {
   119  	return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))}
   120  }
   121  func (i *Interface) GetBigInt() *BigInt   { return &BigInt{*i.object.(**big.Int)} }
   122  func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} }
   123  
   124  //接口是被包装的一般对象的切片。
   125  type Interfaces struct {
   126  	objects []interface{}
   127  }
   128  
   129  //NewInterfaces创建一个未初始化的接口切片。
   130  func NewInterfaces(size int) *Interfaces {
   131  	return &Interfaces{
   132  		objects: make([]interface{}, size),
   133  	}
   134  }
   135  
   136  //SIZE返回切片中的接口数。
   137  func (i *Interfaces) Size() int {
   138  	return len(i.objects)
   139  }
   140  
   141  //get从切片返回给定索引处的bigint。
   142  func (i *Interfaces) Get(index int) (iface *Interface, _ error) {
   143  	if index < 0 || index >= len(i.objects) {
   144  		return nil, errors.New("index out of bounds")
   145  	}
   146  	return &Interface{i.objects[index]}, nil
   147  }
   148  
   149  //set在切片中的给定索引处设置big int。
   150  func (i *Interfaces) Set(index int, object *Interface) error {
   151  	if index < 0 || index >= len(i.objects) {
   152  		return errors.New("index out of bounds")
   153  	}
   154  	i.objects[index] = object.object
   155  	return nil
   156  }