1
0

Refactoring and added common utils

This commit is contained in:
Scott E. Graves
2017-02-17 18:58:06 -06:00
parent 6b425fc5ca
commit 9b44d435f8
9 changed files with 101 additions and 30 deletions

View File

@@ -20,7 +20,8 @@ using json = nlohmann::json;
NS_BEGIN(Sia)
NS_BEGIN(Api)
#define String std::wstring
typedef std::wstring String;
#define DEFAULT_CONFIG_FILE_PATH L".\\Config\\SiaDriveConfig.json"
#define Property(type, name, get_access, set_access) \
private:\
@@ -43,10 +44,12 @@ struct SiaHostConfig
String RequiredVersion;
};
#define API_SUCCESS(t, x) (x == t::Success)
#define DEFAULT_CONFIG_FILE_PATH L".\\Config\\SiaDriveConfig.json"
template<typename T>
inline bool ApiSuccess(const T& t) {
return t == T::Success;
}
typedef ttmath::UInt<256> Hastings;
typedef ttmath::Big<1, 30> SiaCurrency;
/*
BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
@@ -72,4 +75,41 @@ static inline String SiaCurrencyToString(const SiaCurrency& value)
return value.ToWString(conv);
}
class IHost
{
public:
IHost() {}
virtual ~IHost() {}
public:
virtual Hastings GetStoragePrice() const = 0;
virtual Hastings GetDownloadPrice() const = 0;
virtual Hastings GetUploadPrice() const = 0;
};
template<typename T, typename R>
R CalculateAveragePrice(const std::vector<T>& v, std::function<R(const T& t)> PriceGetter)
{
R result = v.size() ? std::accumulate(std::next(v.begin()), v.end(), PriceGetter(v[0]), [&](const R& a, const T& b) {
return a + PriceGetter(b);
}).Div(v.size()) : 0;
return result;
}
inline Hastings CalculateAverageHostPrice(const std::vector<IHost>& hosts)
{
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetStoragePrice(); });
}
inline Hastings CalculateAverageDownloadPrice(const std::vector<IHost>& hosts)
{
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetDownloadPrice(); });
}
inline Hastings CalculateAverageUploadPrice(const std::vector<IHost>& hosts)
{
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetUploadPrice(); });
}
NS_END(2)