提交 270d4c70 编写于 作者: V venediktov

make token accept smaller/larger unsigned integral types for NumberType

上级 ea09b974
......@@ -42,7 +42,7 @@ extern "C" {
account owned_account;
//Initialize currency account only if it does not exist
if ( !accounts::get( owned_account, N(currency) )) {
store_account( N(currency), account( currency_tokens(1000ll*1000ll*1000ll) ) );
store_account( N(currency), account( currency_tokens(1000ull*1000ull*1000ull) ) );
}
}
......
......@@ -87,7 +87,8 @@ namespace TOKEN_NAME {
/**
Assert statement to verify structure packing for account
**/
static_assert( sizeof(account) == sizeof(uint64_t)+sizeof(currency_tokens), "unexpected packing" );
//This structure is *not* packed the check will not work for different sizes of currency_tockens
//static_assert( sizeof(account) == sizeof(uint64_t)+sizeof(currency_tokens), "unexpected packing" );
/**
Defines the database table for account information
......
......@@ -58,6 +58,8 @@ namespace eosio {
*/
template<typename NumberType, uint64_t currency = N(eos) >
struct token {
using number_type = NumberType;
static_assert(eosio::is_unsigned<NumberType>::value, "NumberType can only be unsigned number");
/**
* Type of the currency (e.g. eos) represented as an unsigned 64 bit integer
* @brief Type of the currency
......@@ -68,20 +70,22 @@ namespace eosio {
* Default constructor
* @brief Default constructor
*/
token(){}
token() : quantity{} {}
/**
* Constructor for token given quantity of tokens available
* @brief Constructor for token given quantity of tokens available
* @param v - quantity of tokens available
*/
explicit token( NumberType v ):quantity(v){};
// template<typename T, typename = typename eosio::enable_if<eosio::is_unsigned<T>::value && eosio::is_same<T,NumberType>::value>::type>
template<typename T, typename = typename eosio::enable_if<eosio::is_unsigned<T>::value && !(sizeof(T)>sizeof(NumberType))>::type>
explicit token( T v ):quantity(v){};
/**
* Quantity of tokens available
* @brief Quantity of tokens available
*/
NumberType quantity = 0;
NumberType quantity;
/**
* Subtracts quantity of token from this object
......
......@@ -89,4 +89,71 @@ namespace eosio {
template<typename T> struct remove_reference<const T&> { typedef T type; };
///@}
/**
* @ingroup types
* needed for universal references since we build with --nostdlib and thus std::forward<T> is not available
* with forward<Args...>(args...) we always guarantee correctness of the calling code
*/
template<typename T, typename U>
constexpr decltype(auto) forward(U && u) noexcept
{
return static_cast<T &&>(u);
}
template< class T >
constexpr typename remove_reference<T>::type&& move( T&& t ) noexcept {
return static_cast<typename remove_reference<decltype(t)>::type&&>(t);
}
template<class T, T v>
struct integral_constant {
static constexpr T value = v;
typedef T value_type;
typedef integral_constant type; // using injected-class-name
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; } //since c++14
};
using true_type = integral_constant<bool,true>;
using false_type = integral_constant<bool,false>;
template<class T, class U>
struct is_same : false_type {};
template<class T>
struct is_same<T, T> : true_type {};
template<class...> struct voidify { using type = void; };
template<class... Ts> using void_t = typename voidify<Ts...>::type;
template<class T, class = void>
struct supports_arithmetic_operations : false_type {};
//for no default ctor we'll need decltype(eosio::declval<T>() +-/* eosio::declval<T>())
template<class T>
struct supports_arithmetic_operations<T,
void_t<decltype(T() + T()),
decltype(T() - T()),
decltype(T() * T()),
decltype(T() / T())>>
: true_type {};
namespace detail {
template<typename T,bool = supports_arithmetic_operations<T>::value>
struct is_unsigned : integral_constant<bool, T(0) < T(-1)> {};
template<typename T>
struct is_unsigned<T,false> : false_type {};
} // namespace detail
template<typename T>
struct is_unsigned : detail::is_unsigned<T>::type {};
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
} // namespace eos
......@@ -132,7 +132,7 @@ void apply_exchange_buy( buy_order order ) {
bid& exchange_bid = order;
require_auth( exchange_bid.buyer.name );
assert( exchange_bid.quantity > eosio::tokens(0), "invalid quantity" );
assert( exchange_bid.quantity > eosio::tokens(0u), "invalid quantity" );
assert( exchange_bid.expiration > now(), "order expired" );
print( name(exchange_bid.buyer.name), " created bid for ", order.quantity, " currency at price: ", order.at_price, "\n" );
......@@ -163,7 +163,7 @@ void apply_exchange_buy( buy_order order ) {
print( "lowest ask <= exchange_bid.at_price\n" );
match( exchange_bid, buyer_account, lowest_ask, seller_account );
if( lowest_ask.quantity == currency_tokens(0) ) {
if( lowest_ask.quantity == currency_tokens(0u) ) {
seller_account.open_orders--;
save( seller_account );
save( buyer_account );
......@@ -195,7 +195,7 @@ void apply_exchange_sell( sell_order order ) {
ask& exchange_ask = order;
require_auth( exchange_ask.seller.name );
assert( exchange_ask.quantity > currency_tokens(0), "invalid quantity" );
assert( exchange_ask.quantity > currency_tokens(0u), "invalid quantity" );
assert( exchange_ask.expiration > now(), "order expired" );
print( "\n\n", name(exchange_ask.seller.name), " created sell for ", order.quantity,
......@@ -224,7 +224,7 @@ void apply_exchange_sell( sell_order order ) {
while( highest_bid.at_price >= exchange_ask.at_price ) {
match( highest_bid, buyer_account, exchange_ask, seller_account );
if( highest_bid.quantity == eos_tokens(0) ) {
if( highest_bid.quantity == eos_tokens(0u) ) {
buyer_account.open_orders--;
save( seller_account );
save( buyer_account );
......
......@@ -28,7 +28,7 @@ namespace exchange {
eosio::print( "{ quantity: ", quantity, ", price: ", at_price, " }" );
}
};
static_assert( sizeof(bid) == 32+12, "unexpected padding" );
static_assert( sizeof(bid) == sizeof(order_id)+sizeof(price)+sizeof(eosio::tokens)+sizeof(time), "unexpected padding" );
//@abi table
struct PACKED( ask ) {
......@@ -41,7 +41,8 @@ namespace exchange {
eosio::print( "{ quantity: ", quantity, ", price: ", at_price, " }" );
}
};
static_assert( sizeof(ask) == 32+12, "unexpected padding" );
static_assert( sizeof(ask) == sizeof(order_id)+sizeof(price)+sizeof(currency_tokens)+sizeof(time), "unexpected padding" );
//@abi table i64
struct PACKED( account ) {
......
......@@ -44,7 +44,7 @@ extern "C" {
account owned_account;
//Initialize currency account only if it does not exist
if ( !accounts::get( owned_account, N(currency) )) {
store_account( N(currency), account( currency_tokens(1000ll*1000ll*1000ll) ) );
store_account( N(currency), account( currency_tokens(1000ull*1000ull*1000ull) ) );
}
}
......
......@@ -105,7 +105,7 @@ extern "C" {
//Initialize storage account only if it does not exist
if ( !accounts::get( owned_account, N(storage) )) {
// How do we initialize the storage capacity? By how much here?
accounts::store( account( storage_tokens(1000ll*1000ll*1000ll) ), N(storage) );
accounts::store( account( storage_tokens(1000ull*1000ull*1000ull) ), N(storage) );
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册