| OLD | NEW |
| 1 {% from "enum_macros.tmpl" import enum_decl -%} | 1 {% from "enum_macros.tmpl" import enum_decl -%} |
| 2 class {{struct.name}} { | 2 class {{struct.name}} { |
| 3 public: | 3 public: |
| 4 using Data_ = internal::{{struct.name}}_Data; | 4 using Data_ = internal::{{struct.name}}_Data; |
| 5 | 5 |
| 6 {#--- Enums #} | 6 {#--- Enums #} |
| 7 {%- for enum in struct.enums -%} | 7 {%- for enum in struct.enums -%} |
| 8 {{enum_decl(enum, is_static=true)|indent(2)}} | 8 {{enum_decl(enum, is_static=true)|indent(2)}} |
| 9 {%- endfor %} | 9 {%- endfor %} |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 U To() const { | 28 U To() const { |
| 29 return mojo::TypeConverter<U, {{struct.name}}>::Convert(*this); | 29 return mojo::TypeConverter<U, {{struct.name}}>::Convert(*this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 {{struct.name}}(); | 32 {{struct.name}}(); |
| 33 ~{{struct.name}}(); | 33 ~{{struct.name}}(); |
| 34 | 34 |
| 35 // Returns the number of bytes it would take to serialize this struct's data. | 35 // Returns the number of bytes it would take to serialize this struct's data. |
| 36 size_t GetSerializedSize() const; | 36 size_t GetSerializedSize() const; |
| 37 | 37 |
| 38 // Returns true on successful serialization. On failure, part of the data may | 38 // Returns true on successful serialization. On failure, part of the data may |
| 39 // be serialized, until the point of failure. This API does not support | 39 // be serialized, until the point of failure. This API does not support |
| 40 // serializing handles. | 40 // serializing handles. If not null, |bytes_written| is set to the number of |
| 41 // bytes written to |buf|, even if this function return false. |
| 41 // | 42 // |
| 42 // TODO(vardhan): For now, we return true for success. Should we define a | 43 // TODO(vardhan): For now, we return true for success. Should we define a |
| 43 // public error type for serialization? (we shouldn't reuse | 44 // public error type for serialization? Should we open up |
| 44 // internal::ValidationError). | 45 // internal::ValidationError? |
| 45 bool Serialize(void* buf, size_t buf_size); | 46 bool Serialize(void* buf, size_t buf_size, size_t* bytes_written = nullptr); |
| 47 |
| 48 // Deserializes the given |buf| of size |buf_size| representing a serialized |
| 49 // version of this struct. The buffer is validated before it is deserialized. |
| 50 // Returns true on successful deserialization. |
| 51 // TODO(vardhan): Recover the validation error if there is one? |
| 52 bool Deserialize(void* buf, size_t buf_size); |
| 46 | 53 |
| 47 // Deserializes the given |buf| representing a serialized version of this | 54 // Deserializes the given |buf| representing a serialized version of this |
| 48 // struct. Assumes that the serialized |buf| is valid. | 55 // struct. The buffer is NOT validated before it is deserialized, so the user |
| 49 // | 56 // must be confident of its validity and that |buf| points to enough data to |
| 50 // TODO(vardhan): | 57 // finish deserializing. |
| 51 // - Should we pass in how big |buf| is and validate that it is | 58 void DeserializeWithoutValidation(void* buf); |
| 52 // <= GetSerializedSize()? If so, should this validation happen all the | |
| 53 // time? | |
| 54 // - Deserialize() will CHECK-fail if you try to deserialize something with | |
| 55 // a bad offset, etc. For IPC, we |Validate()| before running | |
| 56 // deserialization if we want safety, but we probably want a recoverable | |
| 57 // error for this API. | |
| 58 // - What's the validation story? | |
| 59 void Deserialize(void* buf); | |
| 60 | 59 |
| 61 {% if struct|is_cloneable_kind %} | 60 {% if struct|is_cloneable_kind %} |
| 62 {{struct.name}}Ptr Clone() const; | 61 {{struct.name}}Ptr Clone() const; |
| 63 {%- endif %} | 62 {%- endif %} |
| 64 bool Equals(const {{struct.name}}& other) const; | 63 bool Equals(const {{struct.name}}& other) const; |
| 65 | 64 |
| 66 {#--- Struct members #} | 65 {#--- Struct members #} |
| 67 {% for field in struct.fields %} | 66 {% for field in struct.fields %} |
| 68 {%- set type = field.kind|cpp_wrapper_type %} | 67 {%- set type = field.kind|cpp_wrapper_type %} |
| 69 {%- set name = field.name %} | 68 {%- set name = field.name %} |
| 70 {{type}} {{name}}; | 69 {{type}} {{name}}; |
| 71 {%- endfor %} | 70 {%- endfor %} |
| 72 }; | 71 }; |
| 73 | 72 |
| 74 {#--- Enum Operators #} | 73 {#--- Enum Operators #} |
| 75 {% from "enum_macros.tmpl" import global_enum_operators_decl -%} | 74 {% from "enum_macros.tmpl" import global_enum_operators_decl -%} |
| 76 {%- for enum in struct.enums %} | 75 {%- for enum in struct.enums %} |
| 77 {{global_enum_operators_decl(enum, class_name=struct.name)}} | 76 {{global_enum_operators_decl(enum, class_name=struct.name)}} |
| 78 {%- endfor %} | 77 {%- endfor %} |
| OLD | NEW |