Concepts.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/Optional.h>
  8. #include <AK/Variant.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/SharedCircularQueue.h>
  11. #pragma once
  12. // These concepts are used to help the compiler distinguish between specializations that would be
  13. // ambiguous otherwise. For example, if the specializations for int and Vector<T> were declared as
  14. // follows:
  15. //
  16. // template<> ErrorOr<int> decode(Decoder& decoder);
  17. // template<typename T> ErrorOr<Vector<T>> decode(Decoder& decoder);
  18. //
  19. // Then decode<int>() would be ambiguous because either declaration could work (the compiler would
  20. // not be able to distinguish if you wanted to decode an int or a Vector of int).
  21. namespace IPC::Concepts {
  22. namespace Detail {
  23. template<typename T>
  24. constexpr inline bool IsHashMap = false;
  25. template<typename K, typename V, typename KeyTraits, typename ValueTraits, bool IsOrdered>
  26. constexpr inline bool IsHashMap<HashMap<K, V, KeyTraits, ValueTraits, IsOrdered>> = true;
  27. template<typename T>
  28. constexpr inline bool IsOptional = false;
  29. template<typename T>
  30. constexpr inline bool IsOptional<Optional<T>> = true;
  31. template<typename T>
  32. constexpr inline bool IsSharedSingleProducerCircularQueue = false;
  33. template<typename T, size_t Size>
  34. constexpr inline bool IsSharedSingleProducerCircularQueue<Core::SharedSingleProducerCircularQueue<T, Size>> = true;
  35. template<typename T>
  36. constexpr inline bool IsVariant = false;
  37. template<typename... Ts>
  38. constexpr inline bool IsVariant<Variant<Ts...>> = true;
  39. template<typename T>
  40. constexpr inline bool IsVector = false;
  41. template<typename T>
  42. constexpr inline bool IsVector<Vector<T>> = true;
  43. }
  44. template<typename T>
  45. concept HashMap = Detail::IsHashMap<T>;
  46. template<typename T>
  47. concept Optional = Detail::IsOptional<T>;
  48. template<typename T>
  49. concept SharedSingleProducerCircularQueue = Detail::IsSharedSingleProducerCircularQueue<T>;
  50. template<typename T>
  51. concept Variant = Detail::IsVariant<T>;
  52. template<typename T>
  53. concept Vector = Detail::IsVector<T>;
  54. }