mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 23:25:53 +08:00
refs #784 Added helper class for using CMemoTable with containers of value objects.
This commit is contained in:
committed by
Klaus Basan
parent
13380aa85d
commit
f6f2d38821
@@ -46,6 +46,58 @@ namespace BlackMisc
|
||||
CSequence<T> m_list;
|
||||
CDictionary<T, int, QMap> m_dict;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Helper class for memoizing members of a value object.
|
||||
*/
|
||||
template <typename... Ts>
|
||||
struct CMemoHelper
|
||||
{
|
||||
//! Memoizer for Ts. Other types are passed through.
|
||||
class CMemoizer : private CMemoTable<Ts>...
|
||||
{
|
||||
public:
|
||||
//! If T is in Ts, return the index of member in the memo table.
|
||||
template <typename T, std::enable_if_t<TIsOneOf<T, Ts...>::value, int> = 0>
|
||||
int maybeMemoize(const T &member) { return this->CMemoTable<T>::getIndex(member); }
|
||||
|
||||
//! If T is not in Ts, return member.
|
||||
template <typename T, std::enable_if_t<! TIsOneOf<T, Ts...>::value, int> = 0>
|
||||
const T &maybeMemoize(const T &member) { return member; }
|
||||
|
||||
//! Return the values in the T table as a flat list.
|
||||
template <typename T>
|
||||
const CSequence<T> &getTable() const { return this->CMemoTable<T>::getTable(); }
|
||||
};
|
||||
|
||||
//! Unmemoizer for Ts. Other types are passed through.
|
||||
class CUnmemoizer : private CSequence<Ts>...
|
||||
{
|
||||
public:
|
||||
//! Return reference to the flat list T table.
|
||||
template <typename T>
|
||||
CSequence<T> &getTable() { return *this; }
|
||||
|
||||
//! If T is in Ts, return proxy that will assign to member through the value at the given index in the flat list.
|
||||
template <typename T, std::enable_if_t<TIsOneOf<T, Ts...>::value, int> = 0>
|
||||
auto maybeUnmemoize(T &member) const
|
||||
{
|
||||
struct Memo
|
||||
{
|
||||
int index;
|
||||
T &member;
|
||||
const CSequence<T> &list;
|
||||
int &get() { return index; }
|
||||
~Memo() { if (index >= 0) { member = list[index]; } }
|
||||
};
|
||||
return Memo { -1, member, static_cast<const CSequence<T> &>(*this) };
|
||||
}
|
||||
|
||||
//! If T is not in Ts, return member as std::reference_wrapper.
|
||||
template <typename T, std::enable_if_t<! TIsOneOf<T, Ts...>::value, int> = 0>
|
||||
auto maybeUnmemoize(T &member) const { return std::ref(member); }
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user