mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-14 08:45:36 +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;
|
CSequence<T> m_list;
|
||||||
CDictionary<T, int, QMap> m_dict;
|
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
|
#endif
|
||||||
|
|||||||
@@ -162,6 +162,18 @@ namespace BlackMisc
|
|||||||
struct TIsEqualityComparable<T, U, void_t<decltype(std::declval<T>() == std::declval<U>())>> : public std::true_type {};
|
struct TIsEqualityComparable<T, U, void_t<decltype(std::declval<T>() == std::declval<U>())>> : public std::true_type {};
|
||||||
//! \endcond
|
//! \endcond
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Trait that detects if a type is a member of a parameter pack.
|
||||||
|
*/
|
||||||
|
template <typename T, typename... Ts>
|
||||||
|
struct TIsOneOf : public std::false_type {};
|
||||||
|
//! \cond
|
||||||
|
template <typename T, typename... Ts>
|
||||||
|
struct TIsOneOf<T, T, Ts...> : public std::true_type {};
|
||||||
|
template <typename T, typename T2, typename... Ts>
|
||||||
|
struct TIsOneOf<T, T2, Ts...> : public TIsOneOf<T, Ts...> {};
|
||||||
|
//! \endcond
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Trait that detects if a type is QPrivateSignal.
|
* Trait that detects if a type is QPrivateSignal.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user