param_traits_macros.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2012 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 IPC_PARAM_TRAITS_MACROS_H_
  5. #define IPC_PARAM_TRAITS_MACROS_H_
  6. #include <string>
  7. #include <type_traits>
  8. // Traits generation for structs.
  9. #define IPC_STRUCT_TRAITS_BEGIN(struct_name) \
  10. namespace IPC { \
  11. template <> \
  12. struct IPC_MESSAGE_EXPORT ParamTraits<struct_name> { \
  13. typedef struct_name param_type; \
  14. static void Write(base::Pickle* m, const param_type& p); \
  15. static bool Read(const base::Pickle* m, \
  16. base::PickleIterator* iter, \
  17. param_type* p); \
  18. static void Log(const param_type& p, std::string* l); \
  19. }; \
  20. }
  21. #define IPC_STRUCT_TRAITS_MEMBER(name)
  22. #define IPC_STRUCT_TRAITS_PARENT(type)
  23. #define IPC_STRUCT_TRAITS_END()
  24. // Convenience macro for defining enumerated type traits for types which are
  25. // not range-checked by the IPC system. The author of the message handlers
  26. // is responsible for all validation. This macro should not need to be
  27. // subsequently redefined.
  28. #define IPC_ENUM_TRAITS(type) \
  29. IPC_ENUM_TRAITS_VALIDATE(type, true)
  30. // Convenience macro for defining enumerated type traits for types which are
  31. // range-checked by the IPC system to be in the range of 0..maxvalue inclusive.
  32. // This macro should not need to be subsequently redefined.
  33. #define IPC_ENUM_TRAITS_MAX_VALUE(type, maxvalue) \
  34. IPC_ENUM_TRAITS_MIN_MAX_VALUE(type, 0, maxvalue)
  35. // Convenience macro for defining enumerated type traits for types which are
  36. // range-checked by the IPC system to be in the range of minvalue..maxvalue
  37. // inclusive. This macro should not need to be subsequently redefined.
  38. #define IPC_ENUM_TRAITS_MIN_MAX_VALUE(typ, minvalue, maxvalue) \
  39. IPC_ENUM_TRAITS_VALIDATE( \
  40. typ, (static_cast<std::underlying_type<typ>::type>(value) >= \
  41. static_cast<std::underlying_type<typ>::type>(minvalue) && \
  42. static_cast<std::underlying_type<typ>::type>(value) <= \
  43. static_cast<std::underlying_type<typ>::type>(maxvalue)))
  44. // Traits generation for enums. This macro may be redefined later.
  45. #define IPC_ENUM_TRAITS_VALIDATE(enum_name, validation_expression) \
  46. namespace IPC { \
  47. template <> \
  48. struct IPC_MESSAGE_EXPORT ParamTraits<enum_name> { \
  49. typedef enum_name param_type; \
  50. static void Write(base::Pickle* m, const param_type& p); \
  51. static bool Read(const base::Pickle* m, \
  52. base::PickleIterator* iter, \
  53. param_type* p); \
  54. static void Log(const param_type& p, std::string* l); \
  55. }; \
  56. }
  57. #endif // IPC_PARAM_TRAITS_MACROS_H_