github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/mobile/interface.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Contains perverted wrappers to allow crossing over empty interfaces.
    19  
    20  package geth
    21  
    22  import (
    23  	"errors"
    24  	"math/big"
    25  
    26  	"github.com/AigarNetwork/aigar/common"
    27  )
    28  
    29  // Interface represents a wrapped version of Go's interface{}, with the capacity
    30  // to store arbitrary data types.
    31  //
    32  // Since it's impossible to get the arbitrary-ness converted between Go and mobile
    33  // platforms, we're using explicit getters and setters for the conversions. There
    34  // is of course no point in enumerating everything, just enough to support the
    35  // contract bindins requiring client side generated code.
    36  type Interface struct {
    37  	object interface{}
    38  }
    39  
    40  // NewInterface creates a new empty interface that can be used to pass around
    41  // generic types.
    42  func NewInterface() *Interface {
    43  	return new(Interface)
    44  }
    45  
    46  func (i *Interface) SetBool(b bool)                 { i.object = &b }
    47  func (i *Interface) SetBools(bs *Bools)             { i.object = &bs.bools }
    48  func (i *Interface) SetString(str string)           { i.object = &str }
    49  func (i *Interface) SetStrings(strs *Strings)       { i.object = &strs.strs }
    50  func (i *Interface) SetBinary(binary []byte)        { b := common.CopyBytes(binary); i.object = &b }
    51  func (i *Interface) SetBinaries(binaries *Binaries) { i.object = &binaries.binaries }
    52  func (i *Interface) SetAddress(address *Address)    { i.object = &address.address }
    53  func (i *Interface) SetAddresses(addrs *Addresses)  { i.object = &addrs.addresses }
    54  func (i *Interface) SetHash(hash *Hash)             { i.object = &hash.hash }
    55  func (i *Interface) SetHashes(hashes *Hashes)       { i.object = &hashes.hashes }
    56  func (i *Interface) SetInt8(n int8)                 { i.object = &n }
    57  func (i *Interface) SetInt16(n int16)               { i.object = &n }
    58  func (i *Interface) SetInt32(n int32)               { i.object = &n }
    59  func (i *Interface) SetInt64(n int64)               { i.object = &n }
    60  func (i *Interface) SetInt8s(bigints *BigInts) {
    61  	ints := make([]int8, 0, bigints.Size())
    62  	for _, bi := range bigints.bigints {
    63  		ints = append(ints, int8(bi.Int64()))
    64  	}
    65  	i.object = &ints
    66  }
    67  func (i *Interface) SetInt16s(bigints *BigInts) {
    68  	ints := make([]int16, 0, bigints.Size())
    69  	for _, bi := range bigints.bigints {
    70  		ints = append(ints, int16(bi.Int64()))
    71  	}
    72  	i.object = &ints
    73  }
    74  func (i *Interface) SetInt32s(bigints *BigInts) {
    75  	ints := make([]int32, 0, bigints.Size())
    76  	for _, bi := range bigints.bigints {
    77  		ints = append(ints, int32(bi.Int64()))
    78  	}
    79  	i.object = &ints
    80  }
    81  func (i *Interface) SetInt64s(bigints *BigInts) {
    82  	ints := make([]int64, 0, bigints.Size())
    83  	for _, bi := range bigints.bigints {
    84  		ints = append(ints, bi.Int64())
    85  	}
    86  	i.object = &ints
    87  }
    88  func (i *Interface) SetUint8(bigint *BigInt)  { n := uint8(bigint.bigint.Uint64()); i.object = &n }
    89  func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n }
    90  func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n }
    91  func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n }
    92  func (i *Interface) SetUint8s(bigints *BigInts) {
    93  	ints := make([]uint8, 0, bigints.Size())
    94  	for _, bi := range bigints.bigints {
    95  		ints = append(ints, uint8(bi.Uint64()))
    96  	}
    97  	i.object = &ints
    98  }
    99  func (i *Interface) SetUint16s(bigints *BigInts) {
   100  	ints := make([]uint16, 0, bigints.Size())
   101  	for _, bi := range bigints.bigints {
   102  		ints = append(ints, uint16(bi.Uint64()))
   103  	}
   104  	i.object = &ints
   105  }
   106  func (i *Interface) SetUint32s(bigints *BigInts) {
   107  	ints := make([]uint32, 0, bigints.Size())
   108  	for _, bi := range bigints.bigints {
   109  		ints = append(ints, uint32(bi.Uint64()))
   110  	}
   111  	i.object = &ints
   112  }
   113  func (i *Interface) SetUint64s(bigints *BigInts) {
   114  	ints := make([]uint64, 0, bigints.Size())
   115  	for _, bi := range bigints.bigints {
   116  		ints = append(ints, bi.Uint64())
   117  	}
   118  	i.object = &ints
   119  }
   120  func (i *Interface) SetBigInt(bigint *BigInt)    { i.object = &bigint.bigint }
   121  func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints }
   122  
   123  func (i *Interface) SetDefaultBool()      { i.object = new(bool) }
   124  func (i *Interface) SetDefaultBools()     { i.object = new([]bool) }
   125  func (i *Interface) SetDefaultString()    { i.object = new(string) }
   126  func (i *Interface) SetDefaultStrings()   { i.object = new([]string) }
   127  func (i *Interface) SetDefaultBinary()    { i.object = new([]byte) }
   128  func (i *Interface) SetDefaultBinaries()  { i.object = new([][]byte) }
   129  func (i *Interface) SetDefaultAddress()   { i.object = new(common.Address) }
   130  func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) }
   131  func (i *Interface) SetDefaultHash()      { i.object = new(common.Hash) }
   132  func (i *Interface) SetDefaultHashes()    { i.object = new([]common.Hash) }
   133  func (i *Interface) SetDefaultInt8()      { i.object = new(int8) }
   134  func (i *Interface) SetDefaultInt8s()     { i.object = new([]int8) }
   135  func (i *Interface) SetDefaultInt16()     { i.object = new(int16) }
   136  func (i *Interface) SetDefaultInt16s()    { i.object = new([]int16) }
   137  func (i *Interface) SetDefaultInt32()     { i.object = new(int32) }
   138  func (i *Interface) SetDefaultInt32s()    { i.object = new([]int32) }
   139  func (i *Interface) SetDefaultInt64()     { i.object = new(int64) }
   140  func (i *Interface) SetDefaultInt64s()    { i.object = new([]int64) }
   141  func (i *Interface) SetDefaultUint8()     { i.object = new(uint8) }
   142  func (i *Interface) SetDefaultUint8s()    { i.object = new([]uint8) }
   143  func (i *Interface) SetDefaultUint16()    { i.object = new(uint16) }
   144  func (i *Interface) SetDefaultUint16s()   { i.object = new([]uint16) }
   145  func (i *Interface) SetDefaultUint32()    { i.object = new(uint32) }
   146  func (i *Interface) SetDefaultUint32s()   { i.object = new([]uint32) }
   147  func (i *Interface) SetDefaultUint64()    { i.object = new(uint64) }
   148  func (i *Interface) SetDefaultUint64s()   { i.object = new([]uint64) }
   149  func (i *Interface) SetDefaultBigInt()    { i.object = new(*big.Int) }
   150  func (i *Interface) SetDefaultBigInts()   { i.object = new([]*big.Int) }
   151  
   152  func (i *Interface) GetBool() bool            { return *i.object.(*bool) }
   153  func (i *Interface) GetBools() *Bools         { return &Bools{*i.object.(*[]bool)} }
   154  func (i *Interface) GetString() string        { return *i.object.(*string) }
   155  func (i *Interface) GetStrings() *Strings     { return &Strings{*i.object.(*[]string)} }
   156  func (i *Interface) GetBinary() []byte        { return *i.object.(*[]byte) }
   157  func (i *Interface) GetBinaries() *Binaries   { return &Binaries{*i.object.(*[][]byte)} }
   158  func (i *Interface) GetAddress() *Address     { return &Address{*i.object.(*common.Address)} }
   159  func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} }
   160  func (i *Interface) GetHash() *Hash           { return &Hash{*i.object.(*common.Hash)} }
   161  func (i *Interface) GetHashes() *Hashes       { return &Hashes{*i.object.(*[]common.Hash)} }
   162  func (i *Interface) GetInt8() int8            { return *i.object.(*int8) }
   163  func (i *Interface) GetInt16() int16          { return *i.object.(*int16) }
   164  func (i *Interface) GetInt32() int32          { return *i.object.(*int32) }
   165  func (i *Interface) GetInt64() int64          { return *i.object.(*int64) }
   166  func (i *Interface) GetInt8s() *BigInts {
   167  	val := i.object.(*[]int8)
   168  	bigints := NewBigInts(len(*val))
   169  	for i, v := range *val {
   170  		bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
   171  	}
   172  	return bigints
   173  }
   174  func (i *Interface) GetInt16s() *BigInts {
   175  	val := i.object.(*[]int16)
   176  	bigints := NewBigInts(len(*val))
   177  	for i, v := range *val {
   178  		bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
   179  	}
   180  	return bigints
   181  }
   182  func (i *Interface) GetInt32s() *BigInts {
   183  	val := i.object.(*[]int32)
   184  	bigints := NewBigInts(len(*val))
   185  	for i, v := range *val {
   186  		bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
   187  	}
   188  	return bigints
   189  }
   190  func (i *Interface) GetInt64s() *BigInts {
   191  	val := i.object.(*[]int64)
   192  	bigints := NewBigInts(len(*val))
   193  	for i, v := range *val {
   194  		bigints.Set(i, &BigInt{new(big.Int).SetInt64(v)})
   195  	}
   196  	return bigints
   197  }
   198  func (i *Interface) GetUint8() *BigInt {
   199  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))}
   200  }
   201  func (i *Interface) GetUint16() *BigInt {
   202  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))}
   203  }
   204  func (i *Interface) GetUint32() *BigInt {
   205  	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))}
   206  }
   207  func (i *Interface) GetUint64() *BigInt {
   208  	return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))}
   209  }
   210  func (i *Interface) GetUint8s() *BigInts {
   211  	val := i.object.(*[]uint8)
   212  	bigints := NewBigInts(len(*val))
   213  	for i, v := range *val {
   214  		bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
   215  	}
   216  	return bigints
   217  }
   218  func (i *Interface) GetUint16s() *BigInts {
   219  	val := i.object.(*[]uint16)
   220  	bigints := NewBigInts(len(*val))
   221  	for i, v := range *val {
   222  		bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
   223  	}
   224  	return bigints
   225  }
   226  func (i *Interface) GetUint32s() *BigInts {
   227  	val := i.object.(*[]uint32)
   228  	bigints := NewBigInts(len(*val))
   229  	for i, v := range *val {
   230  		bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
   231  	}
   232  	return bigints
   233  }
   234  func (i *Interface) GetUint64s() *BigInts {
   235  	val := i.object.(*[]uint64)
   236  	bigints := NewBigInts(len(*val))
   237  	for i, v := range *val {
   238  		bigints.Set(i, &BigInt{new(big.Int).SetUint64(v)})
   239  	}
   240  	return bigints
   241  }
   242  func (i *Interface) GetBigInt() *BigInt   { return &BigInt{*i.object.(**big.Int)} }
   243  func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} }
   244  
   245  // Interfaces is a slices of wrapped generic objects.
   246  type Interfaces struct {
   247  	objects []interface{}
   248  }
   249  
   250  // NewInterfaces creates a slice of uninitialized interfaces.
   251  func NewInterfaces(size int) *Interfaces {
   252  	return &Interfaces{objects: make([]interface{}, size)}
   253  }
   254  
   255  // Size returns the number of interfaces in the slice.
   256  func (i *Interfaces) Size() int {
   257  	return len(i.objects)
   258  }
   259  
   260  // Get returns the bigint at the given index from the slice.
   261  // Notably the returned value can be changed without affecting the
   262  // interfaces itself.
   263  func (i *Interfaces) Get(index int) (iface *Interface, _ error) {
   264  	if index < 0 || index >= len(i.objects) {
   265  		return nil, errors.New("index out of bounds")
   266  	}
   267  	return &Interface{object: i.objects[index]}, nil
   268  }
   269  
   270  // Set sets the big int at the given index in the slice.
   271  func (i *Interfaces) Set(index int, object *Interface) error {
   272  	if index < 0 || index >= len(i.objects) {
   273  		return errors.New("index out of bounds")
   274  	}
   275  	i.objects[index] = object.object
   276  	return nil
   277  }