Concepts.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //
  22. // They also serve to work around the inability to do partial function specialization in C++.
  23. namespace IPC::Concepts {
  24. namespace Detail {
  25. template<typename T>
  26. constexpr inline bool IsHashMap = false;
  27. template<typename K, typename V, typename KeyTraits, typename ValueTraits, bool IsOrdered>
  28. constexpr inline bool IsHashMap<HashMap<K, V, KeyTraits, ValueTraits, IsOrdered>> = true;
  29. template<typename T>
  30. constexpr inline bool IsOptional = false;
  31. template<typename T>
  32. constexpr inline bool IsOptional<Optional<T>> = true;
  33. template<typename T>
  34. constexpr inline bool IsSharedSingleProducerCircularQueue = false;
  35. template<typename T, size_t Size>
  36. constexpr inline bool IsSharedSingleProducerCircularQueue<Core::SharedSingleProducerCircularQueue<T, Size>> = true;
  37. template<typename T>
  38. constexpr inline bool IsVariant = false;
  39. template<typename... Ts>
  40. constexpr inline bool IsVariant<Variant<Ts...>> = true;
  41. template<typename T>
  42. constexpr inline bool IsVector = false;
  43. template<typename T>
  44. constexpr inline bool IsVector<Vector<T>> = true;
  45. template<typename T>
  46. constexpr inline bool IsArray = false;
  47. template<typename T, size_t N>
  48. constexpr inline bool IsArray<Array<T, N>> = true;
  49. }
  50. template<typename T>
  51. concept HashMap = Detail::IsHashMap<T>;
  52. template<typename T>
  53. concept Optional = Detail::IsOptional<T>;
  54. template<typename T>
  55. concept SharedSingleProducerCircularQueue = Detail::IsSharedSingleProducerCircularQueue<T>;
  56. template<typename T>
  57. concept Variant = Detail::IsVariant<T>;
  58. template<typename T>
  59. concept Vector = Detail::IsVector<T>;
  60. template<typename T>
  61. concept Array = Detail::IsArray<T>;
  62. }