This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from eosnetworkfoundation/backport-support-std…
…array Backport support stdarray
- Loading branch information
Showing
7 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"____comment": "This file was generated with eosio-abigen. DO NOT EDIT ", | ||
"version": "eosio::abi/1.2", | ||
"types": [], | ||
"structs": [ | ||
{ | ||
"name": "greeting", | ||
"base": "", | ||
"fields": [ | ||
{ | ||
"name": "id", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"name": "t", | ||
"type": "int32[32]" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "hi", | ||
"base": "", | ||
"fields": [ | ||
{ | ||
"name": "user", | ||
"type": "name" | ||
} | ||
] | ||
} | ||
], | ||
"actions": [ | ||
{ | ||
"name": "hi", | ||
"type": "hi", | ||
"ricardian_contract": "" | ||
} | ||
], | ||
"tables": [ | ||
{ | ||
"name": "greeting", | ||
"type": "greeting", | ||
"index_type": "i64", | ||
"key_names": [], | ||
"key_types": [] | ||
} | ||
], | ||
"ricardian_clauses": [], | ||
"variants": [], | ||
"action_results": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <eosio/eosio.hpp> | ||
#include <eosio/print.hpp> | ||
#include <array> | ||
|
||
using std::array; | ||
using namespace eosio; | ||
|
||
class[[eosio::contract("using_std_array")]] using_std_array : public contract | ||
{ | ||
public: | ||
using contract::contract; | ||
|
||
[[eosio::action]] void hi(name user) { | ||
require_auth(user); | ||
print("Hello, ", user); | ||
} | ||
|
||
struct [[eosio::table]] greeting { | ||
uint64_t id; | ||
array<int, 32> t; | ||
uint64_t primary_key() const { return id; } | ||
}; | ||
typedef multi_index<"greeting"_n, greeting> greeting_index; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"tests" : [ | ||
{ | ||
"expected" : { | ||
"abi-file" : "using_std_array.abi" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#include <eosio/eosio.hpp> | ||
#include <eosio/transaction.hpp> | ||
#include <eosio/name.hpp> | ||
|
||
using namespace eosio; | ||
|
||
class [[eosio::contract]] array_tests : public contract { | ||
public: | ||
using contract::contract; | ||
|
||
TABLE tests { | ||
uint64_t id; | ||
std::array<uint8_t,32> str; | ||
uint64_t primary_key() const { return id; } | ||
}; | ||
|
||
struct info { | ||
int age; | ||
std::string name; | ||
}; | ||
|
||
typedef multi_index<name("tests"), tests> tests_table; | ||
typedef std::array<std::string,4> array_string_4; | ||
struct my_struct { | ||
uint32_t id; | ||
std::array<array_string_4,2> aastr; | ||
|
||
bool operator==(const my_struct& b) const { | ||
return id == b.id && | ||
aastr == b.aastr; | ||
} | ||
}; | ||
|
||
// test inside using std::array | ||
[[eosio::action]] | ||
void testin(std::string message) { | ||
tests_table _tests(get_self(), get_self().value); | ||
|
||
std::array<uint8_t, 32> str = {'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a' }; | ||
int len = message.length() < 32 ? message.length() : 32; | ||
for(int i = 0; i < len ; ++i){ | ||
str[i] = (uint8_t)message[i]; | ||
} | ||
|
||
std::array<uint8_t, 32> str2 = str; | ||
eosio::cout << "size of std::array str is : " << str.size() << "\n"; | ||
for(int i = 0; i < 32; ++i){ | ||
eosio::cout << str[i] << " "; | ||
} | ||
eosio::cout << "\n"; | ||
for(int i = 0; i < 32; ++i){ | ||
eosio::cout << str2[i] << " "; | ||
} | ||
eosio::cout << "\n"; | ||
std::array<info, 2> info_arr; | ||
info_arr[0].age = 20; | ||
info_arr[0].name = "abc"; | ||
info_arr[1].age = 21; | ||
info_arr[1].name = "cde"; | ||
for(int i = 0; i < 2; ++i){ | ||
eosio::cout << info_arr[i].age << " " << info_arr[i].name << "\n"; | ||
} | ||
} | ||
|
||
// test parameter using std::array | ||
// not supported so far | ||
[[eosio::action]] | ||
void testpa(std::array<int,4> input){ | ||
std::array<int,4> arr = input; | ||
for(int i = 0; i < 4; ++i){ | ||
eosio::cout << arr[i] << " "; | ||
} | ||
eosio::cout << "\n"; | ||
} | ||
|
||
// test return using std::array, not supported so far | ||
[[eosio::action]] | ||
// cleos -v push action eosio testre '[[1,2,3,4]]' -p eosio@active | ||
std::array<int,4> testre(std::array<int,4> input){ | ||
std::array<int,4> arr = input; | ||
for(auto & v : arr) v += 1; | ||
return arr; | ||
} | ||
|
||
// test return using std::vector | ||
[[eosio::action]] | ||
// cleos -v push action eosio testrev '[[1,2,3,4]]' -p eosio@active | ||
std::vector<int> testrev(std::vector<int> input){ | ||
std::vector<int> vec = input; | ||
for(auto & v : vec) v += 1; | ||
return vec; | ||
} | ||
|
||
// test nested array | ||
[[eosio::action]] | ||
void testne() { | ||
std::array<tests,2> nest; | ||
std::array<uint8_t, 32> str = {'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a' }; | ||
|
||
nest[0].id = 1; | ||
nest[0].str = str; | ||
nest[1].id = 2; | ||
nest[1].str = str; | ||
for(int i = 0; i < nest.size(); ++i){ | ||
eosio::cout << nest[i].id << " " ; | ||
for(int j = 0; j < nest[i].str.size(); ++j) { | ||
eosio::cout << nest[i].str[j] + i << " "; | ||
} | ||
eosio::cout << "\n"; | ||
} | ||
std::array<std::array<std::string, 5>, 3> nest2; | ||
for(int i = 0; i < nest2.size(); ++i){ | ||
for(int j = 0; j < nest2[i].size(); ++j) { | ||
nest2[i][j] = "test nested "; | ||
eosio::cout << nest2[i][j] << " "; | ||
} | ||
eosio::cout << "\n"; | ||
} | ||
} | ||
|
||
// test complex data | ||
[[eosio::action]] | ||
void testcom(name user) { | ||
require_auth(user); | ||
tests_table _tests(get_self(), get_self().value); | ||
|
||
std::array<uint8_t, 32> str = {'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a', | ||
'a','a','a','a','a','a','a','a' }; | ||
_tests.emplace(user, [&](auto& t) { | ||
t.id = user.value + std::time(0); // primary key can't be same | ||
t.str = str; | ||
}); | ||
auto it = _tests.begin(); | ||
auto ite = _tests.end(); | ||
while(it != ite){ | ||
eosio::cout << "id = " << it->id << "\n"; | ||
for(int i = 0; i < it->str.size(); ++i) { | ||
eosio::cout << it->str[i] << " "; | ||
} | ||
eosio::cout << "\n"; | ||
++it; | ||
} | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters