Add structure packing support for mojo modules
[chromium-blink-merge.git] / mojo / public / bindings / generators / mojom.py
bloba1007e3de24bfe8ec746a53064133c5d1e09670f
1 # Copyright (c) 2013 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.
5 # mojom's classes provide an interface to mojo modules. Modules are collections
6 # of interfaces and structs to be used by mojo ipc clients and servers.
8 # A simple interface would be created this way:
9 # module = mojom.Module('Foo')
10 # interface = module.AddInterface('Bar')
11 # method = interface.AddMethod('Tat', 0)
12 # method.AddParameter('baz', 0, mojom.INT32)
14 class Kind(object):
15 def __init__(self, spec = None):
16 self.spec = spec
19 # Initialize the set of primitive types. These can be accessed by clients.
20 BOOL = Kind('b')
21 INT8 = Kind('i8')
22 INT16 = Kind('i16')
23 INT32 = Kind('i32')
24 INT64 = Kind('i64')
25 UINT8 = Kind('u8')
26 UINT16 = Kind('u16')
27 UINT32 = Kind('u32')
28 UINT64 = Kind('u64')
29 FLOAT = Kind('f')
30 DOUBLE = Kind('d')
31 STRING = Kind('s')
34 # Collection of all Primitive types
35 PRIMITIVES = [
36 BOOL,
37 INT8,
38 INT16,
39 INT32,
40 INT64,
41 UINT8,
42 UINT16,
43 UINT32,
44 UINT64,
45 FLOAT,
46 DOUBLE,
47 STRING,
48 # TODO(davemoore): Add HANDLE.
52 class Field(object):
53 def __init__(self, name = None, kind = None, ordinal = None, default = None):
54 self.name = name
55 self.kind = kind
56 self.ordinal = ordinal
57 self.default = default
60 class Struct(Kind):
61 def __init__(self, name = None):
62 self.name = name
63 if name != None:
64 spec = 'x:' + name
65 else:
66 spec = None
67 Kind.__init__(self, spec)
68 self.fields = []
70 def AddField(self, name, kind, ordinal = None, default = None):
71 field = Field(name, kind, ordinal, default)
72 self.fields.append(field)
73 return field
76 class Array(Kind):
77 def __init__(self, kind = None):
78 self.kind = kind
79 if kind != None:
80 Kind.__init__(self, 'a:' + kind.spec)
81 else:
82 Kind.__init__(self)
85 class Parameter(object):
86 def __init__(self, name = None, kind = None, ordinal = None, default = None):
87 self.name = name
88 self.ordinal = ordinal
89 self.kind = kind
90 self.default = default
93 class Method(object):
94 def __init__(self, name = None, ordinal = None):
95 self.name = name
96 self.ordinal = ordinal
97 self.parameters = []
99 def AddParameter(self, name, kind, ordinal = None, default = None):
100 parameter = Parameter(name, kind, ordinal, default)
101 self.parameters.append(parameter)
102 return parameter
105 class Interface(object):
106 def __init__(self, name = None):
107 self.name = name
108 self.methods = []
110 def AddMethod(self, name, ordinal = None):
111 method = Method(name, ordinal)
112 self.methods.append(method)
113 return method;
116 class Module(object):
117 def __init__(self, name = None, namespace = None):
118 self.name = name
119 self.namespace = namespace
120 self.structs = []
121 self.interfaces = []
123 def AddInterface(self, name):
124 interface = Interface(name);
125 self.interfaces.append(interface)
126 return interface;
128 def AddStruct(self, name):
129 struct = Struct(name)
130 self.structs.append(struct)
131 return struct;