github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/mobile/interface.go (about) 1 // Contains perverted wrappers to allow crossing over empty interfaces. 2 3 package geth 4 5 import ( 6 "errors" 7 "math/big" 8 9 "github.com/quickchainproject/quickchain/common" 10 ) 11 12 // Interface represents a wrapped version of Go's interface{}, with the capacity 13 // to store arbitrary data types. 14 // 15 // Since it's impossible to get the arbitrary-ness converted between Go and mobile 16 // platforms, we're using explicit getters and setters for the conversions. There 17 // is of course no point in enumerating everything, just enough to support the 18 // contract bindins requiring client side generated code. 19 type Interface struct { 20 object interface{} 21 } 22 23 // NewInterface creates a new empty interface that can be used to pass around 24 // generic types. 25 func NewInterface() *Interface { 26 return new(Interface) 27 } 28 29 func (i *Interface) SetBool(b bool) { i.object = &b } 30 func (i *Interface) SetBools(bs []bool) { i.object = &bs } 31 func (i *Interface) SetString(str string) { i.object = &str } 32 func (i *Interface) SetStrings(strs *Strings) { i.object = &strs.strs } 33 func (i *Interface) SetBinary(binary []byte) { b := common.CopyBytes(binary); i.object = &b } 34 func (i *Interface) SetBinaries(binaries [][]byte) { i.object = &binaries } 35 func (i *Interface) SetAddress(address *Address) { i.object = &address.address } 36 func (i *Interface) SetAddresses(addrs *Addresses) { i.object = &addrs.addresses } 37 func (i *Interface) SetHash(hash *Hash) { i.object = &hash.hash } 38 func (i *Interface) SetHashes(hashes *Hashes) { i.object = &hashes.hashes } 39 func (i *Interface) SetInt8(n int8) { i.object = &n } 40 func (i *Interface) SetInt16(n int16) { i.object = &n } 41 func (i *Interface) SetInt32(n int32) { i.object = &n } 42 func (i *Interface) SetInt64(n int64) { i.object = &n } 43 func (i *Interface) SetUint8(bigint *BigInt) { n := uint8(bigint.bigint.Uint64()); i.object = &n } 44 func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n } 45 func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n } 46 func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n } 47 func (i *Interface) SetBigInt(bigint *BigInt) { i.object = &bigint.bigint } 48 func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints } 49 50 func (i *Interface) SetDefaultBool() { i.object = new(bool) } 51 func (i *Interface) SetDefaultBools() { i.object = new([]bool) } 52 func (i *Interface) SetDefaultString() { i.object = new(string) } 53 func (i *Interface) SetDefaultStrings() { i.object = new([]string) } 54 func (i *Interface) SetDefaultBinary() { i.object = new([]byte) } 55 func (i *Interface) SetDefaultBinaries() { i.object = new([][]byte) } 56 func (i *Interface) SetDefaultAddress() { i.object = new(common.Address) } 57 func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) } 58 func (i *Interface) SetDefaultHash() { i.object = new(common.Hash) } 59 func (i *Interface) SetDefaultHashes() { i.object = new([]common.Hash) } 60 func (i *Interface) SetDefaultInt8() { i.object = new(int8) } 61 func (i *Interface) SetDefaultInt16() { i.object = new(int16) } 62 func (i *Interface) SetDefaultInt32() { i.object = new(int32) } 63 func (i *Interface) SetDefaultInt64() { i.object = new(int64) } 64 func (i *Interface) SetDefaultUint8() { i.object = new(uint8) } 65 func (i *Interface) SetDefaultUint16() { i.object = new(uint16) } 66 func (i *Interface) SetDefaultUint32() { i.object = new(uint32) } 67 func (i *Interface) SetDefaultUint64() { i.object = new(uint64) } 68 func (i *Interface) SetDefaultBigInt() { i.object = new(*big.Int) } 69 func (i *Interface) SetDefaultBigInts() { i.object = new([]*big.Int) } 70 71 func (i *Interface) GetBool() bool { return *i.object.(*bool) } 72 func (i *Interface) GetBools() []bool { return *i.object.(*[]bool) } 73 func (i *Interface) GetString() string { return *i.object.(*string) } 74 func (i *Interface) GetStrings() *Strings { return &Strings{*i.object.(*[]string)} } 75 func (i *Interface) GetBinary() []byte { return *i.object.(*[]byte) } 76 func (i *Interface) GetBinaries() [][]byte { return *i.object.(*[][]byte) } 77 func (i *Interface) GetAddress() *Address { return &Address{*i.object.(*common.Address)} } 78 func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} } 79 func (i *Interface) GetHash() *Hash { return &Hash{*i.object.(*common.Hash)} } 80 func (i *Interface) GetHashes() *Hashes { return &Hashes{*i.object.(*[]common.Hash)} } 81 func (i *Interface) GetInt8() int8 { return *i.object.(*int8) } 82 func (i *Interface) GetInt16() int16 { return *i.object.(*int16) } 83 func (i *Interface) GetInt32() int32 { return *i.object.(*int32) } 84 func (i *Interface) GetInt64() int64 { return *i.object.(*int64) } 85 func (i *Interface) GetUint8() *BigInt { 86 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))} 87 } 88 func (i *Interface) GetUint16() *BigInt { 89 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))} 90 } 91 func (i *Interface) GetUint32() *BigInt { 92 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))} 93 } 94 func (i *Interface) GetUint64() *BigInt { 95 return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))} 96 } 97 func (i *Interface) GetBigInt() *BigInt { return &BigInt{*i.object.(**big.Int)} } 98 func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} } 99 100 // Interfaces is a slices of wrapped generic objects. 101 type Interfaces struct { 102 objects []interface{} 103 } 104 105 // NewInterfaces creates a slice of uninitialized interfaces. 106 func NewInterfaces(size int) *Interfaces { 107 return &Interfaces{ 108 objects: make([]interface{}, size), 109 } 110 } 111 112 // Size returns the number of interfaces in the slice. 113 func (i *Interfaces) Size() int { 114 return len(i.objects) 115 } 116 117 // Get returns the bigint at the given index from the slice. 118 func (i *Interfaces) Get(index int) (iface *Interface, _ error) { 119 if index < 0 || index >= len(i.objects) { 120 return nil, errors.New("index out of bounds") 121 } 122 return &Interface{i.objects[index]}, nil 123 } 124 125 // Set sets the big int at the given index in the slice. 126 func (i *Interfaces) Set(index int, object *Interface) error { 127 if index < 0 || index >= len(i.objects) { 128 return errors.New("index out of bounds") 129 } 130 i.objects[index] = object.object 131 return nil 132 }