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()>; using base = std::bitset<detail::FeatureCollections::numFeatures()>;
void initFromFeatures()
{
}
template<class... Fs> template<class... Fs>
void initFromFeatures(uint256 const& f, Fs&&... fs) void initFromFeatures(uint256 const& f, Fs&&... fs)
{ {
set(f); set(f);
initFromFeatures(std::forward<Fs>(fs)...); if constexpr (sizeof...(fs) > 0)
initFromFeatures(std::forward<Fs>(fs)...);
} }
public: public:

View File

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

View File

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

View File

@@ -69,9 +69,8 @@ public:
Path& push_back (STPathElement const& pe); Path& push_back (STPathElement const& pe);
Json::Value json () const; Json::Value json () const;
private: private:
Path& addHelper (){return *this;}
template <class First, class... Rest> 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) 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> 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)); push_back (std::forward<First> (first));
return addHelper (std::forward<Rest> (rest)...); if constexpr (sizeof...(rest) > 0)
addHelper(std::forward<Rest>(rest)...);
} }
inline inline
@@ -130,15 +130,12 @@ public:
return v; return v;
} }
private: private:
PathSet& addHelper ()
{
return *this;
}
template <class First, class... Rest> template <class First, class... Rest>
PathSet& addHelper (First first, Rest... rest) void addHelper (First first, Rest... rest)
{ {
paths.emplace_back (std::move (first.path)); 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_; std::uint32_t mask_;
private: private:
inline
void
set_args()
{
}
void void
set_args (std::uint32_t flag) set_args (std::uint32_t flag)
{ {
@@ -85,7 +79,9 @@ private:
set_args (std::uint32_t flag, set_args (std::uint32_t flag,
Args... args) Args... args)
{ {
set_args(flag, args...); set_args(flag);
if constexpr (sizeof...(args))
set_args(args...);
} }
protected: protected:

View File

@@ -96,48 +96,14 @@ public:
msig (std::vector<Reg> signers_); msig (std::vector<Reg> signers_);
template <class AccountType, class... Accounts> template <class AccountType, class... Accounts>
msig (AccountType&& a0, Accounts&&... aN) explicit msig(AccountType&& a0, Accounts&&... aN)
: msig(make_vector( : msig{std::vector<Reg>{std::forward<AccountType>(a0),
std::forward<AccountType>(a0), std::forward<Accounts>(aN)...}}
std::forward<Accounts>(aN)...))
{ {
} }
void void
operator()(Env&, JTx& jt) const; 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 void
append_one(BookSpec const& book); append_one(BookSpec const& book);
inline
void
append()
{
}
template <class T, class... Args> template <class T, class... Args>
void void
append (T const& t, Args const&... args); append (T const& t, Args const&... args);
@@ -114,7 +108,8 @@ void
path::append (T const& t, Args const&... args) path::append (T const& t, Args const&... args)
{ {
append_one(t); append_one(t);
append(args...); if constexpr (sizeof...(args) > 0)
append(args...);
} }
} // jtx } // jtx

View File

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

View File

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