interface.go 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Contains perverted wrappers to allow crossing over empty interfaces.

package geth

import (
	"errors"
	"math/big"

	"github.com/ethereum/go-ethereum/common"
)

// Interface represents a wrapped version of Go's interface{}, with the capacity
// to store arbitrary data types.
//
// Since it's impossible to get the arbitrary-ness converted between Go and mobile
// platforms, we're using explicit getters and setters for the conversions. There
// is of course no point in enumerating everything, just enough to support the
// contract bindins requiring client side generated code.
type Interface struct {
	object interface{}
}

// NewInterface creates a new empty interface that can be used to pass around
// generic types.
func NewInterface() *Interface {
	return new(Interface)
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
func (i *Interface) SetBool(b bool)                 { i.object = &b }
func (i *Interface) SetBools(bs *Bools)             { i.object = &bs.bools }
func (i *Interface) SetString(str string)           { i.object = &str }
func (i *Interface) SetStrings(strs *Strings)       { i.object = &strs.strs }
func (i *Interface) SetBinary(binary []byte)        { b := common.CopyBytes(binary); i.object = &b }
func (i *Interface) SetBinaries(binaries *Binaries) { i.object = &binaries.binaries }
func (i *Interface) SetAddress(address *Address)    { i.object = &address.address }
func (i *Interface) SetAddresses(addrs *Addresses)  { i.object = &addrs.addresses }
func (i *Interface) SetHash(hash *Hash)             { i.object = &hash.hash }
func (i *Interface) SetHashes(hashes *Hashes)       { i.object = &hashes.hashes }
func (i *Interface) SetInt8(n int8)                 { i.object = &n }
func (i *Interface) SetInt16(n int16)               { i.object = &n }
func (i *Interface) SetInt32(n int32)               { i.object = &n }
func (i *Interface) SetInt64(n int64)               { i.object = &n }
func (i *Interface) SetInt8s(bigints *BigInts) {
	ints := make([]int8, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, int8(bi.Int64()))
	}
	i.object = &ints
}
func (i *Interface) SetInt16s(bigints *BigInts) {
	ints := make([]int16, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, int16(bi.Int64()))
	}
	i.object = &ints
}
func (i *Interface) SetInt32s(bigints *BigInts) {
	ints := make([]int32, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, int32(bi.Int64()))
	}
	i.object = &ints
}
func (i *Interface) SetInt64s(bigints *BigInts) {
	ints := make([]int64, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, bi.Int64())
	}
	i.object = &ints
}
func (i *Interface) SetUint8(bigint *BigInt)  { n := uint8(bigint.bigint.Uint64()); i.object = &n }
func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n }
func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n }
func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n }
func (i *Interface) SetUint8s(bigints *BigInts) {
	ints := make([]uint8, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, uint8(bi.Uint64()))
	}
	i.object = &ints
}
func (i *Interface) SetUint16s(bigints *BigInts) {
	ints := make([]uint16, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, uint16(bi.Uint64()))
	}
	i.object = &ints
}
func (i *Interface) SetUint32s(bigints *BigInts) {
	ints := make([]uint32, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, uint32(bi.Uint64()))
	}
	i.object = &ints
}
func (i *Interface) SetUint64s(bigints *BigInts) {
	ints := make([]uint64, 0, bigints.Size())
	for _, bi := range bigints.bigints {
		ints = append(ints, bi.Uint64())
	}
	i.object = &ints
}
func (i *Interface) SetBigInt(bigint *BigInt)    { i.object = &bigint.bigint }
func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints }
121 122 123 124 125 126 127 128 129 130 131 132

func (i *Interface) SetDefaultBool()      { i.object = new(bool) }
func (i *Interface) SetDefaultBools()     { i.object = new([]bool) }
func (i *Interface) SetDefaultString()    { i.object = new(string) }
func (i *Interface) SetDefaultStrings()   { i.object = new([]string) }
func (i *Interface) SetDefaultBinary()    { i.object = new([]byte) }
func (i *Interface) SetDefaultBinaries()  { i.object = new([][]byte) }
func (i *Interface) SetDefaultAddress()   { i.object = new(common.Address) }
func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) }
func (i *Interface) SetDefaultHash()      { i.object = new(common.Hash) }
func (i *Interface) SetDefaultHashes()    { i.object = new([]common.Hash) }
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 149 150 151
func (i *Interface) SetDefaultBigInt()    { i.object = new(*big.Int) }
func (i *Interface) SetDefaultBigInts()   { i.object = new([]*big.Int) }

func (i *Interface) GetBool() bool            { return *i.object.(*bool) }
152
func (i *Interface) GetBools() *Bools         { return &Bools{*i.object.(*[]bool)} }
153 154 155
func (i *Interface) GetString() string        { return *i.object.(*string) }
func (i *Interface) GetStrings() *Strings     { return &Strings{*i.object.(*[]string)} }
func (i *Interface) GetBinary() []byte        { return *i.object.(*[]byte) }
156
func (i *Interface) GetBinaries() *Binaries   { return &Binaries{*i.object.(*[][]byte)} }
157 158 159 160 161 162 163 164
func (i *Interface) GetAddress() *Address     { return &Address{*i.object.(*common.Address)} }
func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} }
func (i *Interface) GetHash() *Hash           { return &Hash{*i.object.(*common.Hash)} }
func (i *Interface) GetHashes() *Hashes       { return &Hashes{*i.object.(*[]common.Hash)} }
func (i *Interface) GetInt8() int8            { return *i.object.(*int8) }
func (i *Interface) GetInt16() int16          { return *i.object.(*int16) }
func (i *Interface) GetInt32() int32          { return *i.object.(*int32) }
func (i *Interface) GetInt64() int64          { return *i.object.(*int64) }
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
func (i *Interface) GetInt8s() *BigInts {
	val := i.object.(*[]int8)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
	}
	return bigints
}
func (i *Interface) GetInt16s() *BigInts {
	val := i.object.(*[]int16)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
	}
	return bigints
}
func (i *Interface) GetInt32s() *BigInts {
	val := i.object.(*[]int32)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
	}
	return bigints
}
func (i *Interface) GetInt64s() *BigInts {
	val := i.object.(*[]int64)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetInt64(v)})
	}
	return bigints
}
197 198 199 200 201 202 203 204 205 206 207 208
func (i *Interface) GetUint8() *BigInt {
	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))}
}
func (i *Interface) GetUint16() *BigInt {
	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))}
}
func (i *Interface) GetUint32() *BigInt {
	return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))}
}
func (i *Interface) GetUint64() *BigInt {
	return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))}
}
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
func (i *Interface) GetUint8s() *BigInts {
	val := i.object.(*[]uint8)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
	}
	return bigints
}
func (i *Interface) GetUint16s() *BigInts {
	val := i.object.(*[]uint16)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
	}
	return bigints
}
func (i *Interface) GetUint32s() *BigInts {
	val := i.object.(*[]uint32)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
	}
	return bigints
}
func (i *Interface) GetUint64s() *BigInts {
	val := i.object.(*[]uint64)
	bigints := NewBigInts(len(*val))
	for i, v := range *val {
		bigints.Set(i, &BigInt{new(big.Int).SetUint64(v)})
	}
	return bigints
}
241 242 243 244 245 246 247 248 249 250
func (i *Interface) GetBigInt() *BigInt   { return &BigInt{*i.object.(**big.Int)} }
func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} }

// Interfaces is a slices of wrapped generic objects.
type Interfaces struct {
	objects []interface{}
}

// NewInterfaces creates a slice of uninitialized interfaces.
func NewInterfaces(size int) *Interfaces {
251
	return &Interfaces{objects: make([]interface{}, size)}
252 253 254 255 256 257 258 259
}

// Size returns the number of interfaces in the slice.
func (i *Interfaces) Size() int {
	return len(i.objects)
}

// Get returns the bigint at the given index from the slice.
260 261
// Notably the returned value can be changed without affecting the
// interfaces itself.
262
func (i *Interfaces) Get(index int) (iface *Interface, _ error) {
263 264 265
	if index < 0 || index >= len(i.objects) {
		return nil, errors.New("index out of bounds")
	}
266
	return &Interface{object: i.objects[index]}, nil
267 268 269 270 271 272 273 274 275 276
}

// Set sets the big int at the given index in the slice.
func (i *Interfaces) Set(index int, object *Interface) error {
	if index < 0 || index >= len(i.objects) {
		return errors.New("index out of bounds")
	}
	i.objects[index] = object.object
	return nil
}