github.com/openethereum/go-ethereum@v1.9.7/mobile/interface.go (about)

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