interface.go 7.1 KB

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