github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/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 []bool) { i.object = &bs } 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 [][]byte) { i.object = &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) SetUint8(bigint *BigInt) { n := uint8(bigint.bigint.Uint64()); i.object = &n } 60 func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n } 61 func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n } 62 func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n } 63 func (i *Interface) SetBigInt(bigint *BigInt) { i.object = &bigint.bigint } 64 func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints } 65 66 func (i *Interface) SetDefaultBool() { i.object = new(bool) } 67 func (i *Interface) SetDefaultBools() { i.object = new([]bool) } 68 func (i *Interface) SetDefaultString() { i.object = new(string) } 69 func (i *Interface) SetDefaultStrings() { i.object = new([]string) } 70 func (i *Interface) SetDefaultBinary() { i.object = new([]byte) } 71 func (i *Interface) SetDefaultBinaries() { i.object = new([][]byte) } 72 func (i *Interface) SetDefaultAddress() { i.object = new(common.Address) } 73 func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) } 74 func (i *Interface) SetDefaultHash() { i.object = new(common.Hash) } 75 func (i *Interface) SetDefaultHashes() { i.object = new([]common.Hash) } 76 func (i *Interface) SetDefaultInt8() { i.object = new(int8) } 77 func (i *Interface) SetDefaultInt16() { i.object = new(int16) } 78 func (i *Interface) SetDefaultInt32() { i.object = new(int32) } 79 func (i *Interface) SetDefaultInt64() { i.object = new(int64) } 80 func (i *Interface) SetDefaultUint8() { i.object = new(uint8) } 81 func (i *Interface) SetDefaultUint16() { i.object = new(uint16) } 82 func (i *Interface) SetDefaultUint32() { i.object = new(uint32) } 83 func (i *Interface) SetDefaultUint64() { i.object = new(uint64) } 84 func (i *Interface) SetDefaultBigInt() { i.object = new(*big.Int) } 85 func (i *Interface) SetDefaultBigInts() { i.object = new([]*big.Int) } 86 87 func (i *Interface) GetBool() bool { return *i.object.(*bool) } 88 func (i *Interface) GetBools() []bool { return *i.object.(*[]bool) } 89 func (i *Interface) GetString() string { return *i.object.(*string) } 90 func (i *Interface) GetStrings() *Strings { return &Strings{*i.object.(*[]string)} } 91 func (i *Interface) GetBinary() []byte { return *i.object.(*[]byte) } 92 func (i *Interface) GetBinaries() [][]byte { return *i.object.(*[][]byte) } 93 func (i *Interface) GetAddress() *Address { return &Address{*i.object.(*common.Address)} } 94 func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} } 95 func (i *Interface) GetHash() *Hash { return &Hash{*i.object.(*common.Hash)} } 96 func (i *Interface) GetHashes() *Hashes { return &Hashes{*i.object.(*[]common.Hash)} } 97 func (i *Interface) GetInt8() int8 { return *i.object.(*int8) } 98 func (i *Interface) GetInt16() int16 { return *i.object.(*int16) } 99 func (i *Interface) GetInt32() int32 { return *i.object.(*int32) } 100 func (i *Interface) GetInt64() int64 { return *i.object.(*int64) } 101 func (i *Interface) GetUint8() *BigInt { 102 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))} 103 } 104 func (i *Interface) GetUint16() *BigInt { 105 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))} 106 } 107 func (i *Interface) GetUint32() *BigInt { 108 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))} 109 } 110 func (i *Interface) GetUint64() *BigInt { 111 return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))} 112 } 113 func (i *Interface) GetBigInt() *BigInt { return &BigInt{*i.object.(**big.Int)} } 114 func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} } 115 116 // Interfaces is a slices of wrapped generic objects. 117 type Interfaces struct { 118 objects []interface{} 119 } 120 121 // NewInterfaces creates a slice of uninitialized interfaces. 122 func NewInterfaces(size int) *Interfaces { 123 return &Interfaces{ 124 objects: make([]interface{}, size), 125 } 126 } 127 128 // Size returns the number of interfaces in the slice. 129 func (i *Interfaces) Size() int { 130 return len(i.objects) 131 } 132 133 // Get returns the bigint at the given index from the slice. 134 func (i *Interfaces) Get(index int) (iface *Interface, _ error) { 135 if index < 0 || index >= len(i.objects) { 136 return nil, errors.New("index out of bounds") 137 } 138 return &Interface{i.objects[index]}, nil 139 } 140 141 // Set sets the big int at the given index in the slice. 142 func (i *Interfaces) Set(index int, object *Interface) error { 143 if index < 0 || index >= len(i.objects) { 144 return errors.New("index out of bounds") 145 } 146 i.objects[index] = object.object 147 return nil 148 }