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