Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/union_definition.tmpl

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 {%- import "validation_macros.tmpl" as validation_macros %} 1 {%- import "validation_macros.tmpl" as validation_macros %}
2 {%- set class_name = union.name ~ "_Data" %} 2 {%- set class_name = union.name ~ "_Data" %}
3 {%- set enum_name = union.name ~ "_Tag" -%} 3 {%- set enum_name = union.name ~ "_Tag" -%}
4 4
5 // static 5 // static
6 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { 6 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) {
7 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); 7 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
8 } 8 }
9 9
10
yzshen1 2015/02/17 19:34:35 unnecessary empty line.
azani 2015/02/18 00:27:58 Done.
10 // static 11 // static
11 bool {{class_name}}::Validate(const void* data, 12 bool {{class_name}}::Validate(const void* data,
12 mojo::internal::BoundsChecker* bounds_checker) { 13 mojo::internal::BoundsChecker* bounds_checker,
14 bool claim_memory) {
13 if (!data) { 15 if (!data) {
14 return true; 16 return true;
15 } 17 }
16 18
17 if (!mojo::internal::IsAligned(data)) { 19 if (!mojo::internal::IsAligned(data)) {
18 ReportValidationError(mojo::internal::VALIDATION_ERROR_MISALIGNED_OBJECT); 20 ReportValidationError(mojo::internal::VALIDATION_ERROR_MISALIGNED_OBJECT);
19 return false; 21 return false;
20 } 22 }
21 23
22 if (!bounds_checker->ClaimMemory(data, sizeof({{class_name}}))) { 24 // If the union is inlined in another structure its memory was already claimed .
yzshen1 2015/02/17 19:34:35 nit: I understand 80-char limit is not honored in
azani 2015/02/18 00:27:57 Done.
25 // This ONLY applies to the union itself, NOT anything which the union points to.
26 if (claim_memory && !bounds_checker->ClaimMemory(data, sizeof({{class_name}})) ) {
yzshen1 2015/02/17 19:34:35 For non-inlined case, we need to check that the fi
23 ReportValidationError( 27 ReportValidationError(
24 mojo::internal::VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); 28 mojo::internal::VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE);
25 return false; 29 return false;
26 } 30 }
27 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); 31 const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
28 MOJO_ALLOW_UNUSED_LOCAL(object); 32 MOJO_ALLOW_UNUSED_LOCAL(object);
29 33
30 switch (object->tag) { 34 switch (object->tag) {
31 {% for field in union.fields %} 35 {% for field in union.fields %}
32 case {{enum_name}}::{{field.name|upper}}: 36 case {{enum_name}}::{{field.name|upper}}:
(...skipping 15 matching lines...) Expand all
48 52
49 void {{class_name}}::EncodePointersAndHandles( 53 void {{class_name}}::EncodePointersAndHandles(
50 std::vector<mojo::Handle>* handles) { 54 std::vector<mojo::Handle>* handles) {
51 // TODO(azani): Implement pointers and handles. 55 // TODO(azani): Implement pointers and handles.
52 } 56 }
53 57
54 void {{class_name}}::DecodePointersAndHandles( 58 void {{class_name}}::DecodePointersAndHandles(
55 std::vector<mojo::Handle>* handles) { 59 std::vector<mojo::Handle>* handles) {
56 // TODO(azani): Implement pointers and handles. 60 // TODO(azani): Implement pointers and handles.
57 } 61 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698