OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 from model import PropertyType |
| 6 import code |
| 7 import cpp_util |
| 8 |
| 9 class HGenerator(object): |
| 10 """A .h generator for a namespace. |
| 11 """ |
| 12 def __init__(self, namespace, cpp_type_generator): |
| 13 self._cpp_type_generator = cpp_type_generator |
| 14 self._namespace = namespace |
| 15 self._target_namespace = ( |
| 16 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) |
| 17 |
| 18 def Generate(self): |
| 19 """Generates a code.Code object with the .h for a single namespace. |
| 20 """ |
| 21 c = code.Code() |
| 22 (c.Append(cpp_util.CHROMIUM_LICENSE) |
| 23 .Append() |
| 24 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
| 25 .Append() |
| 26 ) |
| 27 |
| 28 ifndef_name = self._GenerateIfndefName() |
| 29 (c.Append('#ifndef %s' % ifndef_name) |
| 30 .Append('#define %s' % ifndef_name) |
| 31 .Append('#pragma once') |
| 32 .Append() |
| 33 .Append('#include <string>') |
| 34 .Append('#include <vector>') |
| 35 .Append() |
| 36 .Append('#include "base/basictypes.h"') |
| 37 .Append('#include "base/memory/scoped_ptr.h"') |
| 38 .Append('#include "base/values.h"') |
| 39 .Append() |
| 40 .Append('using base::Value;') |
| 41 .Append('using base::DictionaryValue;') |
| 42 .Append('using base::ListValue;') |
| 43 .Append() |
| 44 ) |
| 45 |
| 46 includes = self._cpp_type_generator.GenerateCppIncludes() |
| 47 if not includes.IsEmpty(): |
| 48 (c.Concat(includes) |
| 49 .Append() |
| 50 ) |
| 51 |
| 52 (c.Concat(self._cpp_type_generator.GetCppNamespaceStart()) |
| 53 .Append() |
| 54 .Append('//') |
| 55 .Append('// Types') |
| 56 .Append('//') |
| 57 .Append() |
| 58 ) |
| 59 for type_ in self._namespace.types.values(): |
| 60 (c.Concat(self._GenerateType(type_)) |
| 61 .Append() |
| 62 ) |
| 63 (c.Append('//') |
| 64 .Append('// Functions') |
| 65 .Append('//') |
| 66 .Append() |
| 67 ) |
| 68 for function in self._namespace.functions.values(): |
| 69 (c.Concat(self._GenerateFunction(function)) |
| 70 .Append() |
| 71 ) |
| 72 (c.Append() |
| 73 .Append() |
| 74 .Concat(self._cpp_type_generator.GetCppNamespaceEnd()) |
| 75 .Append() |
| 76 .Append('#endif // %s' % ifndef_name) |
| 77 .Append() |
| 78 ) |
| 79 return c |
| 80 |
| 81 def _GenerateType(self, type_, serializable=True): |
| 82 """Generates a struct for a type. |
| 83 """ |
| 84 classname = cpp_util.CppName(type_.name) |
| 85 c = code.Code() |
| 86 if type_.description: |
| 87 c.Comment(type_.description) |
| 88 (c.Sblock('struct %(classname)s {') |
| 89 .Append('~%(classname)s();') |
| 90 .Append('%(classname)s();') |
| 91 .Append() |
| 92 ) |
| 93 |
| 94 for prop in type_.properties.values(): |
| 95 if prop.description: |
| 96 c.Comment(prop.description) |
| 97 if prop.optional: |
| 98 c.Append('scoped_ptr<%s> %s;' % |
| 99 (self._cpp_type_generator.GetType(prop, pad_for_generics=True), |
| 100 prop.name)) |
| 101 else: |
| 102 c.Append('%s %s;' % |
| 103 (self._cpp_type_generator.GetType(prop), prop.name)) |
| 104 c.Append() |
| 105 |
| 106 (c.Comment('Populates a %(classname)s object from a Value. Returns' |
| 107 ' whether |out| was successfully populated.') |
| 108 .Append('static bool Populate(const Value& value, %(classname)s* out);') |
| 109 .Append() |
| 110 ) |
| 111 |
| 112 if serializable: |
| 113 (c.Comment('Returns a new DictionaryValue representing the' |
| 114 ' serialized form of this %(classname)s object. Passes' |
| 115 'ownership to caller.') |
| 116 .Append('DictionaryValue* ToValue() const;') |
| 117 ) |
| 118 (c.Eblock() |
| 119 .Sblock(' private:') |
| 120 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') |
| 121 .Eblock('};') |
| 122 ) |
| 123 c.Substitute({'classname': classname}) |
| 124 return c |
| 125 |
| 126 def _GenerateFunction(self, function): |
| 127 """Generates the structs for a function. |
| 128 """ |
| 129 c = code.Code() |
| 130 (c.Sblock('namespace %s {' % cpp_util.CppName(function.name)) |
| 131 .Concat(self._GenerateFunctionParams(function)) |
| 132 .Append() |
| 133 .Concat(self._GenerateFunctionResult(function)) |
| 134 .Append() |
| 135 .Eblock('};') |
| 136 ) |
| 137 return c |
| 138 |
| 139 def _GenerateFunctionParams(self, function): |
| 140 """Generates the struct for passing parameters into a function. |
| 141 """ |
| 142 c = code.Code() |
| 143 |
| 144 if function.params: |
| 145 c.Sblock('struct Params {') |
| 146 for param in function.params: |
| 147 if param.description: |
| 148 c.Comment(param.description) |
| 149 if param.type_ == PropertyType.OBJECT: |
| 150 c.Concat(self._GenerateType(param, serializable=False)) |
| 151 c.Append() |
| 152 for param in function.params: |
| 153 c.Append('%s %s;' % |
| 154 (self._cpp_type_generator.GetType(param), param.name)) |
| 155 |
| 156 (c.Append() |
| 157 .Append('~Params();') |
| 158 .Append() |
| 159 .Append('static scoped_ptr<Params> Create(const ListValue& args);') |
| 160 .Eblock() |
| 161 .Sblock(' private:') |
| 162 .Append('Params();') |
| 163 .Append() |
| 164 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
| 165 ) |
| 166 |
| 167 c.Eblock('};') |
| 168 |
| 169 return c |
| 170 |
| 171 def _GenerateFunctionResult(self, function): |
| 172 """Generates the struct for passing a function's result back. |
| 173 """ |
| 174 # TODO(calamity): Handle returning an object |
| 175 c = code.Code() |
| 176 |
| 177 param = function.callback.param |
| 178 # TODO(calamity): Put this description comment in less stupid place |
| 179 if param.description: |
| 180 c.Comment(param.description) |
| 181 (c.Append('class Result {') |
| 182 .Sblock(' public:') |
| 183 ) |
| 184 arg = '' |
| 185 # TODO(calamity): handle object |
| 186 if param: |
| 187 if param.type_ == PropertyType.REF: |
| 188 arg = 'const %(type)s& %(name)s' |
| 189 else: |
| 190 arg = 'const %(type)s %(name)s' |
| 191 arg = arg % { |
| 192 'type': self._cpp_type_generator.GetType(param), |
| 193 'name': param.name |
| 194 } |
| 195 (c.Append('static Value* Create(%s);' % arg) |
| 196 .Eblock() |
| 197 .Sblock(' private:') |
| 198 .Append('Result() {};') |
| 199 .Append('DISALLOW_COPY_AND_ASSIGN(Result);') |
| 200 .Eblock('};') |
| 201 ) |
| 202 |
| 203 return c |
| 204 |
| 205 def _GenerateIfndefName(self): |
| 206 """Formats a path and filename as a #define name. |
| 207 |
| 208 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. |
| 209 """ |
| 210 return (('%s/%s_H__' % |
| 211 (self._namespace.source_file_dir, self._target_namespace)) |
| 212 .upper().replace('/', '_')) |
| 213 |
OLD | NEW |