Simplify code using if constexpr:

Also simplify msig construction
This commit is contained in:
seelabs
2019-08-15 11:40:28 -07:00
parent 1eb3753f26
commit c2d2ba9f45
9 changed files with 29 additions and 105 deletions

View File

@@ -129,15 +129,12 @@ class FeatureBitset
{
using base = std::bitset<detail::FeatureCollections::numFeatures()>;
void initFromFeatures()
{
}
template<class... Fs>
void initFromFeatures(uint256 const& f, Fs&&... fs)
{
set(f);
initFromFeatures(std::forward<Fs>(fs)...);
if constexpr (sizeof...(fs) > 0)
initFromFeatures(std::forward<Fs>(fs)...);
}
public:

View File

@@ -83,25 +83,14 @@ stpath_append_one (STPath& st,
boost::none, book.currency, book.account }));
}
inline
void
stpath_append (STPath& st)
{
}
template <class T, class... Args>
void
stpath_append (STPath& st,
T const& t, Args const&... args)
{
stpath_append_one(st, t);
stpath_append(st, args...);
}
inline
void
stpathset_append (STPathSet& st)
{
if constexpr (sizeof...(args) > 0)
stpath_append(st, args...);
}
template <class... Args>
@@ -110,7 +99,8 @@ stpathset_append(STPathSet& st,
STPath const& p, Args const&... args)
{
st.push_back(p);
stpathset_append(st, args...);
if constexpr (sizeof...(args) > 0)
stpathset_append(st, args...);
}
} // detail

View File

@@ -548,14 +548,6 @@ private:
STAmount const& amount,
Account const& account);
// If you get an error here it means
// you're calling fund with no accounts
inline
void
fund (STAmount const&)
{
}
void
fund_arg (STAmount const& amount,
Account const& account)
@@ -605,7 +597,8 @@ public:
Arg const& arg, Args const&... args)
{
fund_arg (amount, arg);
fund (amount, args...);
if constexpr (sizeof...(args) > 0)
fund(amount, args...);
}
/** Establish trust lines.

View File

@@ -69,9 +69,8 @@ public:
Path& push_back (STPathElement const& pe);
Json::Value json () const;
private:
Path& addHelper (){return *this;}
template <class First, class... Rest>
Path& addHelper (First&& first, Rest&&... rest);
void addHelper (First&& first, Rest&&... rest);
};
inline Path& Path::push_back (STPathElement const& pe)
@@ -95,10 +94,11 @@ Path& Path::push_back (jtx::Account const& account)
}
template <class First, class... Rest>
Path& Path::addHelper (First&& first, Rest&&... rest)
void Path::addHelper (First&& first, Rest&&... rest)
{
push_back (std::forward<First> (first));
return addHelper (std::forward<Rest> (rest)...);
if constexpr (sizeof...(rest) > 0)
addHelper(std::forward<Rest>(rest)...);
}
inline
@@ -130,15 +130,12 @@ public:
return v;
}
private:
PathSet& addHelper ()
{
return *this;
}
template <class First, class... Rest>
PathSet& addHelper (First first, Rest... rest)
void addHelper (First first, Rest... rest)
{
paths.emplace_back (std::move (first.path));
return addHelper (std::move (rest)...);
if constexpr (sizeof...(rest) > 0)
addHelper(std::move(rest)...);
}
};

View File

@@ -53,12 +53,6 @@ protected:
std::uint32_t mask_;
private:
inline
void
set_args()
{
}
void
set_args (std::uint32_t flag)
{
@@ -85,7 +79,9 @@ private:
set_args (std::uint32_t flag,
Args... args)
{
set_args(flag, args...);
set_args(flag);
if constexpr (sizeof...(args))
set_args(args...);
}
protected:

View File

@@ -96,48 +96,14 @@ public:
msig (std::vector<Reg> signers_);
template <class AccountType, class... Accounts>
msig (AccountType&& a0, Accounts&&... aN)
: msig(make_vector(
std::forward<AccountType>(a0),
std::forward<Accounts>(aN)...))
explicit msig(AccountType&& a0, Accounts&&... aN)
: msig{std::vector<Reg>{std::forward<AccountType>(a0),
std::forward<Accounts>(aN)...}}
{
}
void
operator()(Env&, JTx& jt) const;
private:
template <class AccountType>
static
void
helper (std::vector<Reg>& v,
AccountType&& account)
{
v.emplace_back(std::forward<
Reg>(account));
}
template <class AccountType, class... Accounts>
static
void
helper (std::vector<Reg>& v,
AccountType&& a0, Accounts&&... aN)
{
helper(v, std::forward<AccountType>(a0));
helper(v, std::forward<Accounts>(aN)...);
}
template <class... Accounts>
static
std::vector<Reg>
make_vector(Accounts&&... signers)
{
std::vector<Reg> v;
v.reserve(sizeof...(signers));
helper(v, std::forward<
Accounts>(signers)...);
return v;
}
};
//------------------------------------------------------------------------------

View File

@@ -91,12 +91,6 @@ private:
void
append_one(BookSpec const& book);
inline
void
append()
{
}
template <class T, class... Args>
void
append (T const& t, Args const&... args);
@@ -114,7 +108,8 @@ void
path::append (T const& t, Args const&... args)
{
append_one(t);
append(args...);
if constexpr (sizeof...(args) > 0)
append(args...);
}
} // jtx

View File

@@ -30,12 +30,6 @@ namespace jtx {
namespace detail {
inline
void
require_args (requires_t& vec)
{
}
template <class Cond, class... Args>
inline
void
@@ -43,7 +37,8 @@ require_args (requires_t& vec,
Cond const& cond, Args const&... args)
{
vec.push_back(cond);
require_args(vec, args...);
if constexpr (sizeof...(args) > 0)
require_args(vec, args...);
}
} // detail

View File

@@ -64,13 +64,6 @@ create_arg (boost::optional<Account>&,
opt = value;
}
inline
void
create_args (boost::optional<Account>&,
boost::optional<std::uint32_t>&)
{
}
template<class Arg, class... Args>
void
create_args(boost::optional<Account>& account_opt,
@@ -78,7 +71,8 @@ create_args(boost::optional<Account>& account_opt,
Arg const& arg, Args const&... args)
{
create_arg(account_opt, expire_opt, arg);
create_args(account_opt, expire_opt, args...);
if constexpr (sizeof...(args))
create_args(account_opt, expire_opt, args...);
}
} // detail
@@ -91,7 +85,8 @@ create (Account const& account,
{
boost::optional<Account> target;
boost::optional<std::uint32_t> expire;
detail::create_args(target, expire, args...);
if constexpr (sizeof...(args) > 0)
detail::create_args(target, expire, args...);
return detail::create(
account, target, expire);
}