[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6476c72c | 2011-02-11 18:46:19 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef IPC_PARAM_TRAITS_MACROS_H_ |
| 6 | #define IPC_PARAM_TRAITS_MACROS_H_ |
| 7 | |
| 8 | #include <string> |
Henrique Ferreiro | f15449d9 | 2021-10-15 20:40:59 | [diff] [blame] | 9 | #include <type_traits> |
[email protected] | 6476c72c | 2011-02-11 18:46:19 | [diff] [blame] | 10 | |
| 11 | // Traits generation for structs. |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 12 | #define IPC_STRUCT_TRAITS_BEGIN(struct_name) \ |
| 13 | namespace IPC { \ |
| 14 | template <> \ |
| 15 | struct IPC_MESSAGE_EXPORT ParamTraits<struct_name> { \ |
| 16 | typedef struct_name param_type; \ |
| 17 | static void Write(base::Pickle* m, const param_type& p); \ |
| 18 | static bool Read(const base::Pickle* m, \ |
| 19 | base::PickleIterator* iter, \ |
| 20 | param_type* p); \ |
| 21 | static void Log(const param_type& p, std::string* l); \ |
| 22 | }; \ |
[email protected] | 6476c72c | 2011-02-11 18:46:19 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #define IPC_STRUCT_TRAITS_MEMBER(name) |
[email protected] | 94dc971d | 2011-03-05 19:08:32 | [diff] [blame] | 26 | #define IPC_STRUCT_TRAITS_PARENT(type) |
[email protected] | 6476c72c | 2011-02-11 18:46:19 | [diff] [blame] | 27 | #define IPC_STRUCT_TRAITS_END() |
| 28 | |
[email protected] | dedd0a9 | 2013-06-04 07:20:32 | [diff] [blame] | 29 | // Convenience macro for defining enumerated type traits for types which are |
| 30 | // not range-checked by the IPC system. The author of the message handlers |
| 31 | // is responsible for all validation. This macro should not need to be |
| 32 | // subsequently redefined. |
| 33 | #define IPC_ENUM_TRAITS(type) \ |
| 34 | IPC_ENUM_TRAITS_VALIDATE(type, true) |
| 35 | |
| 36 | // Convenience macro for defining enumerated type traits for types which are |
| 37 | // range-checked by the IPC system to be in the range of 0..maxvalue inclusive. |
| 38 | // This macro should not need to be subsequently redefined. |
| 39 | #define IPC_ENUM_TRAITS_MAX_VALUE(type, maxvalue) \ |
| 40 | IPC_ENUM_TRAITS_MIN_MAX_VALUE(type, 0, maxvalue) |
| 41 | |
| 42 | // Convenience macro for defining enumerated type traits for types which are |
| 43 | // range-checked by the IPC system to be in the range of minvalue..maxvalue |
| 44 | // inclusive. This macro should not need to be subsequently redefined. |
Henrique Ferreiro | f15449d9 | 2021-10-15 20:40:59 | [diff] [blame] | 45 | #define IPC_ENUM_TRAITS_MIN_MAX_VALUE(typ, minvalue, maxvalue) \ |
| 46 | IPC_ENUM_TRAITS_VALIDATE( \ |
| 47 | typ, (static_cast<std::underlying_type<typ>::type>(value) >= \ |
| 48 | static_cast<std::underlying_type<typ>::type>(minvalue) && \ |
| 49 | static_cast<std::underlying_type<typ>::type>(value) <= \ |
| 50 | static_cast<std::underlying_type<typ>::type>(maxvalue))) |
[email protected] | dedd0a9 | 2013-06-04 07:20:32 | [diff] [blame] | 51 | |
| 52 | // Traits generation for enums. This macro may be redefined later. |
| 53 | #define IPC_ENUM_TRAITS_VALIDATE(enum_name, validation_expression) \ |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 54 | namespace IPC { \ |
| 55 | template <> \ |
| 56 | struct IPC_MESSAGE_EXPORT ParamTraits<enum_name> { \ |
| 57 | typedef enum_name param_type; \ |
| 58 | static void Write(base::Pickle* m, const param_type& p); \ |
| 59 | static bool Read(const base::Pickle* m, \ |
| 60 | base::PickleIterator* iter, \ |
| 61 | param_type* p); \ |
| 62 | static void Log(const param_type& p, std::string* l); \ |
| 63 | }; \ |
[email protected] | 6476c72c | 2011-02-11 18:46:19 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #endif // IPC_PARAM_TRAITS_MACROS_H_ |
| 67 | |