• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 
5 package org.chromium.mojo.bindings;
6 
7 import org.chromium.mojo.system.Core;
8 
9 /**
10  * Base class for all mojo unions.
11  */
12 public abstract class Union {
13     /** They type of object that has been set. */
14     protected int mTag;
15 
16     /** Returns the type of object being held by this union. */
which()17     public int which() {
18         return mTag;
19     }
20 
21     /** Returns whether the type of object in this union is known. */
isUnknown()22     public boolean isUnknown() {
23         return mTag == -1;
24     }
25 
26     /**
27      * Returns the serialization of the union. This method can close Handles.
28      *
29      * @param core the |Core| implementation used to generate handles. Only used if the data
30      *            structure being encoded contains interfaces, can be |null| otherwise.
31      */
serialize(Core core)32     public Message serialize(Core core) {
33         Encoder encoder = new Encoder(core, BindingsHelper.UNION_SIZE);
34         encoder.claimMemory(16);
35         encode(encoder, 0);
36         return encoder.getMessage();
37     }
38 
39     /**
40      * Serializes this data structure using the given encoder.
41      */
encode(Encoder encoder, int offset)42     protected abstract void encode(Encoder encoder, int offset);
43 }
44