test_task_traits_extension.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_TEST_TASK_TRAITS_EXTENSION_H_
  5. #define BASE_TASK_TEST_TASK_TRAITS_EXTENSION_H_
  6. #include <utility>
  7. #include "base/task/task_traits.h"
  8. namespace base {
  9. enum class TestExtensionEnumTrait { kA, kB, kC };
  10. struct TestExtensionBoolTrait {};
  11. // Example TaskTraits extension for use in tests.
  12. class TestTaskTraitsExtension {
  13. public:
  14. static constexpr uint8_t kExtensionId =
  15. TaskTraitsExtensionStorage::kFirstEmbedderExtensionId;
  16. struct ValidTrait : public TaskTraits::ValidTrait {
  17. using TaskTraits::ValidTrait::ValidTrait;
  18. ValidTrait(TestExtensionEnumTrait);
  19. ValidTrait(TestExtensionBoolTrait);
  20. };
  21. template <class... ArgTypes,
  22. class CheckArgumentsAreValid = std::enable_if_t<
  23. trait_helpers::AreValidTraits<ValidTrait, ArgTypes...>::value>>
  24. constexpr TestTaskTraitsExtension(ArgTypes... args)
  25. : enum_trait_(
  26. trait_helpers::GetEnum<TestExtensionEnumTrait,
  27. TestExtensionEnumTrait::kA>(args...)),
  28. bool_trait_(
  29. trait_helpers::HasTrait<TestExtensionBoolTrait, ArgTypes...>()) {}
  30. constexpr TaskTraitsExtensionStorage Serialize() const {
  31. return {kExtensionId, {{static_cast<uint8_t>(enum_trait_), bool_trait_}}};
  32. }
  33. static const TestTaskTraitsExtension Parse(
  34. const TaskTraitsExtensionStorage& extension) {
  35. if (extension.data[1]) {
  36. return TestTaskTraitsExtension(
  37. static_cast<TestExtensionEnumTrait>(extension.data[0]),
  38. TestExtensionBoolTrait());
  39. } else {
  40. return TestTaskTraitsExtension(
  41. static_cast<TestExtensionEnumTrait>(extension.data[0]));
  42. }
  43. }
  44. constexpr TestExtensionEnumTrait enum_trait() const { return enum_trait_; }
  45. constexpr bool bool_trait() const { return bool_trait_; }
  46. private:
  47. TestExtensionEnumTrait enum_trait_;
  48. bool bool_trait_;
  49. };
  50. template <class... ArgTypes,
  51. class = std::enable_if_t<
  52. trait_helpers::AreValidTraits<TestTaskTraitsExtension::ValidTrait,
  53. ArgTypes...>::value>>
  54. constexpr TaskTraitsExtensionStorage MakeTaskTraitsExtension(ArgTypes... args) {
  55. return TestTaskTraitsExtension(std::forward<ArgTypes>(args)...).Serialize();
  56. }
  57. } // namespace base
  58. #endif // BASE_TASK_TEST_TASK_TRAITS_EXTENSION_H_