mpl.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include "fwd.h"
  3. #include <tuple>
  4. #include <type_traits>
  5. namespace NYT {
  6. /// @cond Doxygen_Suppress
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <class TBase, class TDerived>
  9. struct TIsBaseOf
  10. {
  11. static constexpr bool Value = std::is_base_of_v<TBase, TDerived> && !std::is_same_v<TBase, TDerived>;
  12. };
  13. ////////////////////////////////////////////////////////////////////////////////
  14. namespace NDetail {
  15. template <class T, class Tuple>
  16. struct TIndexInTuple;
  17. template <class T, class... Types>
  18. struct TIndexInTuple<T, std::tuple<T, Types...>>
  19. {
  20. static constexpr int Value = 0;
  21. };
  22. template <class T>
  23. struct TIndexInTuple<T, std::tuple<>>
  24. {
  25. static constexpr int Value = 0;
  26. };
  27. template <class T, class U, class... Types>
  28. struct TIndexInTuple<T, std::tuple<U, Types...>>
  29. {
  30. static constexpr int Value = 1 + TIndexInTuple<T, std::tuple<Types...>>::Value;
  31. };
  32. template <class T, class TTuple>
  33. constexpr bool DoesTupleContainType = (TIndexInTuple<T, TTuple>::Value < std::tuple_size<TTuple>{});
  34. template <class TOut, class TIn = std::tuple<>>
  35. struct TUniqueTypes;
  36. template <class... TOut, class TInCar, class... TInCdr>
  37. struct TUniqueTypes<std::tuple<TOut...>, std::tuple<TInCar, TInCdr...>>
  38. {
  39. using TType = std::conditional_t<
  40. DoesTupleContainType<TInCar, std::tuple<TOut...>>,
  41. typename TUniqueTypes<std::tuple<TOut...>, std::tuple<TInCdr...>>::TType,
  42. typename TUniqueTypes<std::tuple<TOut..., TInCar>, std::tuple<TInCdr...>>::TType
  43. >;
  44. };
  45. template <class TOut>
  46. struct TUniqueTypes<TOut, std::tuple<>>
  47. {
  48. using TType = TOut;
  49. };
  50. } // namespace NDetail
  51. /// @endcond Doxygen_Suppress
  52. ////////////////////////////////////////////////////////////////////////////////
  53. }