1 # Copyright (c) 2012 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 from model
import PropertyType
10 class HGenerator(object):
11 """A .h generator for a namespace.
13 def __init__(self
, namespace
, cpp_type_generator
):
14 self
._cpp
_type
_generator
= cpp_type_generator
15 self
._namespace
= namespace
16 self
._target
_namespace
= (
17 self
._cpp
_type
_generator
.GetCppNamespaceName(self
._namespace
))
20 """Generates a code.Code object with the .h for a single namespace.
23 (c
.Append(cpp_util
.CHROMIUM_LICENSE
)
25 .Append(cpp_util
.GENERATED_FILE_MESSAGE
% self
._namespace
.source_file
)
29 ifndef_name
= self
._GenerateIfndefName
()
30 (c
.Append('#ifndef %s' % ifndef_name
)
31 .Append('#define %s' % ifndef_name
)
32 .Append('#pragma once')
34 .Append('#include <string>')
35 .Append('#include <vector>')
37 .Append('#include "base/basictypes.h"')
38 .Append('#include "base/memory/linked_ptr.h"')
39 .Append('#include "base/memory/scoped_ptr.h"')
40 .Append('#include "base/values.h"')
44 c
.Concat(self
._cpp
_type
_generator
.GetRootNamespaceStart())
45 forward_declarations
= (
46 self
._cpp
_type
_generator
.GenerateForwardDeclarations())
47 if not forward_declarations
.IsEmpty():
49 .Concat(forward_declarations
)
53 (c
.Concat(self
._cpp
_type
_generator
.GetNamespaceStart())
60 for type_
in self
._namespace
.types
.values():
61 (c
.Concat(self
._GenerateType
(type_
))
65 .Append('// Functions')
69 for function
in self
._namespace
.functions
.values():
70 (c
.Concat(self
._GenerateFunction
(function
))
74 .Concat(self
._cpp
_type
_generator
.GetNamespaceEnd())
75 .Concat(self
._cpp
_type
_generator
.GetRootNamespaceEnd())
77 .Append('#endif // %s' % ifndef_name
)
82 def _GenerateFields(self
, props
):
83 """Generates the field declarations when declaring a type.
86 for prop
in self
._cpp
_type
_generator
.GetExpandedChoicesInParams(props
):
88 c
.Comment(prop
.description
)
90 self
._cpp
_type
_generator
.GetType(prop
, wrap_optional
=True),
93 # Generate the enums needed for any fields with "choices"
95 if prop
.type_
== PropertyType
.CHOICES
:
96 c
.Sblock('enum %(enum_type)s {')
97 c
.Append(self
._cpp
_type
_generator
.GetChoiceEnumNoneValue(prop
) + ',')
98 for choice
in prop
.choices
.values():
100 self
._cpp
_type
_generator
.GetChoiceEnumValue( prop
, choice
.type_
)
104 .Append('%(enum_type)s %(name)s_type;')
107 'enum_type': self
._cpp
_type
_generator
.GetChoicesEnumType(prop
),
108 'name': prop
.unix_name
112 def _GenerateType(self
, type_
, serializable
=True):
113 """Generates a struct for a type.
115 classname
= cpp_util
.Classname(type_
.name
)
117 if type_
.description
:
118 c
.Comment(type_
.description
)
119 (c
.Sblock('struct %(classname)s {')
120 .Append('~%(classname)s();')
121 .Append('%(classname)s();')
123 .Concat(self
._GenerateFields
(type_
.properties
.values()))
124 .Comment('Populates a %(classname)s object from a Value. Returns'
125 ' whether |out| was successfully populated.')
126 .Append('static bool Populate(const Value& value, %(classname)s* out);')
131 (c
.Comment('Returns a new DictionaryValue representing the'
132 ' serialized form of this %(classname)s object. Passes '
133 'ownership to caller.')
134 .Append('scoped_ptr<DictionaryValue> ToValue() const;')
138 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
141 c
.Substitute({'classname': classname
})
144 def _GenerateFunction(self
, function
):
145 """Generates the structs for a function.
148 (c
.Sblock('namespace %s {' % cpp_util
.Classname(function
.name
))
149 .Concat(self
._GenerateFunctionParams
(function
))
151 .Concat(self
._GenerateFunctionResult
(function
))
157 def _GenerateFunctionParams(self
, function
):
158 """Generates the struct for passing parameters into a function.
163 c
.Sblock('struct Params {')
164 for param
in function
.params
:
165 if param
.type_
== PropertyType
.OBJECT
:
166 c
.Concat(self
._GenerateType
(param
, serializable
=False))
168 (c
.Concat(self
._GenerateFields
(function
.params
))
169 .Append('~Params();')
171 .Append('static scoped_ptr<Params> Create(const ListValue& args);')
176 .Append('DISALLOW_COPY_AND_ASSIGN(Params);')
182 def _GenerateFunctionResult(self
, function
):
183 """Generates functions for passing a function's result back.
187 c
.Sblock('namespace Result {')
188 params
= function
.callback
.params
190 c
.Append('Value* Create();')
192 # If there is a single parameter, this is straightforward. However, if
193 # the callback parameter is of 'choices', this generates a Create method
194 # for each choice. This works because only 1 choice can be returned at a
196 for param
in self
._cpp
_type
_generator
.GetExpandedChoicesInParams(params
):
197 if param
.description
:
198 c
.Comment(param
.description
)
199 c
.Append('Value* Create(%s);' %
200 cpp_util
.GetConstParameterDeclaration(
201 param
, self
._cpp
_type
_generator
))
206 def _GenerateIfndefName(self
):
207 """Formats a path and filename as a #define name.
209 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
211 return (('%s_%s_H__' %
212 (self
._namespace
.source_file_dir
, self
._target
_namespace
))
213 .upper().replace(os
.sep
, '_'))