Called whenever we encounter a struct, union, sequence, map, or array. When that happens, create a new instance of SerializedSizeState that stores the states of the byte stream and the information of the type encountered, including its kind and extensibility that helps decide when a header, e.g. Dheader or Emheader, is required. If a header is needed, a position in the size cache is also stored so
| 20 | // required. If a header is needed, a position in the size cache is also stored so |
| 21 | // that the actual size will be written when it is determined (see end_ssize_complex). |
| 22 | bool Xcdr2ValueWriter::begin_ssize_complex(Extensibility extensibility, CollectionKind coll_kind) |
| 23 | { |
| 24 | SerializedSizeState state(extensibility, coll_kind); |
| 25 | bool must_cache_size = true; |
| 26 | |
| 27 | if (!size_states_.empty()) { |
| 28 | const Extensibility enclosing_ek = size_states_.top().extensibility; |
| 29 | if (enclosing_ek == FINAL || enclosing_ek == APPENDABLE) { |
| 30 | if (extensibility == FINAL) { |
| 31 | // We don't need to compute and cache the size for final extensibility type. |
| 32 | // Instead, what we need is the total size of the closest ancestor type |
| 33 | // for which the size is required, for example, by a Dheader or a Emheader. |
| 34 | // Copy the current size from the enclosing type so that the total size |
| 35 | // can be built up cumulatively by this type. |
| 36 | state.total_size = size_states_.top().total_size; |
| 37 | if (coll_kind == SEQUENCE_KIND || coll_kind == MAP_KIND) { // Sequence/map length |
| 38 | primitive_serialized_size_ulong(encoding_, state.total_size); |
| 39 | } |
| 40 | must_cache_size = false; |
| 41 | } else { |
| 42 | // The alignment in front of the Dheader is accounted to the size of the |
| 43 | // containing type. Then we can compute the size of this type separately. |
| 44 | encoding_.align(size_states_.top().total_size, uint32_cdr_size); |
| 45 | serialized_size_delimiter(encoding_, state.total_size); |
| 46 | if (coll_kind == SEQUENCE_KIND) { // Sequence length |
| 47 | primitive_serialized_size_ulong(encoding_, state.total_size); |
| 48 | } |
| 49 | } |
| 50 | } else { // Enclosing type is mutable. |
| 51 | // Regardless of the extensibility of this type, we must track its size |
| 52 | // for the corresponding Emheader/Nextint in the enclosing type. |
| 53 | // Since this is a member of a mutable type, there was a call to |
| 54 | // serialized_size_parameter_id before this which already accounts for |
| 55 | // any alignment in front of the Emheader for this member. |
| 56 | // For members which are not struct, union, sequence, map, or array, the size |
| 57 | // of the member is cached directly in the write_* methods. |
| 58 | if (extensibility == APPENDABLE || extensibility == MUTABLE) { |
| 59 | serialized_size_delimiter(encoding_, state.total_size); |
| 60 | } |
| 61 | if (coll_kind == SEQUENCE_KIND || // Sequence/map length |
| 62 | (coll_kind == MAP_KIND && extensibility == FINAL)) { |
| 63 | primitive_serialized_size_ulong(encoding_, state.total_size); |
| 64 | } |
| 65 | } |
| 66 | } else { |
| 67 | // We are starting from the top-level type. |
| 68 | // Clear the size cache in case it has data from a previous vwrite call. |
| 69 | size_cache_.clear(); |
| 70 | if (extensibility == APPENDABLE || extensibility == MUTABLE) { |
| 71 | serialized_size_delimiter(encoding_, state.total_size); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (must_cache_size) { |
| 76 | size_cache_.push_back(0); |
| 77 | state.cache_pos = size_cache_.size() - 1; |
| 78 | } |
| 79 | size_states_.push(state); |