OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import code | 5 import code |
6 import cpp_util | 6 import cpp_util |
7 from model import Platforms | 7 from model import Platforms |
8 from schema_util import CapitalizeFirstLetter | 8 from schema_util import CapitalizeFirstLetter |
9 from schema_util import JsFunctionNameToClassName | 9 from schema_util import JsFunctionNameToClassName |
10 | 10 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 c = code.Code() | 208 c = code.Code() |
209 c.Append('#include <map>') | 209 c.Append('#include <map>') |
210 c.Append('#include <string>') | 210 c.Append('#include <string>') |
211 c.Append(); | 211 c.Append(); |
212 c.Append('#include "base/strings/string_piece.h"') | 212 c.Append('#include "base/strings/string_piece.h"') |
213 c.Append() | 213 c.Append() |
214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
215 c.Append() | 215 c.Append() |
216 c.Append('class GeneratedSchemas {') | 216 c.Append('class GeneratedSchemas {') |
217 c.Sblock(' public:') | 217 c.Sblock(' public:') |
218 c.Append('// Puts all API schemas in |schemas|.') | 218 c.Append('// Determines if schema named |name| is generated.') |
219 c.Append('static void Get(' | 219 c.Append('static bool IsGenerated(std::string name);') |
220 'std::map<std::string, base::StringPiece>* schemas);') | 220 c.Append() |
221 c.Append('// Gets the API schema named |name|.') | |
222 c.Append('static base::StringPiece Get(const std::string& name);') | |
221 c.Eblock('};'); | 223 c.Eblock('};'); |
222 c.Append() | 224 c.Append() |
223 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 225 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
224 return self._bundle._GenerateHeader('generated_schemas', c) | 226 return self._bundle._GenerateHeader('generated_schemas', c) |
225 | 227 |
228 def _FormatNameAsConstant(name): | |
229 """Formats a name to be a C++ constant of the form kConstantName""" | |
230 return 'k%s' % re.sub('_[a-z]', | |
231 lambda m: m.group(0)[1].upper(), | |
232 name.replace('.', '_').capitalize()) | |
233 | |
226 class _SchemasCCGenerator(object): | 234 class _SchemasCCGenerator(object): |
227 """Generates a code.Code object for the generated schemas .cc file""" | 235 """Generates a code.Code object for the generated schemas .cc file""" |
228 | 236 |
229 def __init__(self, cpp_bundle): | 237 def __init__(self, cpp_bundle): |
230 self._bundle = cpp_bundle | 238 self._bundle = cpp_bundle |
231 | 239 |
232 def Generate(self, namespace): | 240 def Generate(self, namespace): |
233 c = code.Code() | 241 c = code.Code() |
234 c.Append(cpp_util.CHROMIUM_LICENSE) | 242 c.Append(cpp_util.CHROMIUM_LICENSE) |
235 c.Append() | 243 c.Append() |
236 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | 244 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, |
237 'generated_schemas.h'))) | 245 'generated_schemas.h'))) |
238 c.Append() | 246 c.Append() |
239 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 247 c.Append('#include "base/lazy_instance.h"') |
240 c.Append() | 248 c.Append() |
241 c.Append('// static') | 249 c.Append('namespace {') |
242 c.Sblock('void GeneratedSchemas::Get(' | |
243 'std::map<std::string, base::StringPiece>* schemas) {') | |
244 for api in self._bundle._api_defs: | 250 for api in self._bundle._api_defs: |
245 namespace = self._bundle._model.namespaces[api.get('namespace')] | 251 namespace = self._bundle._model.namespaces[api.get('namespace')] |
246 # JSON parsing code expects lists of schemas, so dump a singleton list. | 252 # JSON parsing code expects lists of schemas, so dump a singleton list. |
247 json_content = json.dumps([_RemoveDescriptions(api)], | 253 json_content = json.dumps([_RemoveDescriptions(api)], |
248 separators=(',', ':')) | 254 separators=(',', ':')) |
249 # Escape all double-quotes and backslashes. For this to output a valid | 255 # Escape all double-quotes and backslashes. For this to output a valid |
250 # JSON C string, we need to escape \ and ". | 256 # JSON C string, we need to escape \ and ". |
251 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 257 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
252 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 258 c.Append('const char %s[] = "%s";' % |
259 (_FormatNameAsConstant(namespace.name), json_content)) | |
260 c.Append('}') | |
261 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | |
262 c.Append() | |
263 c.Sblock('struct Static {') | |
264 c.Sblock('Static() {') | |
265 for api in self._bundle._api_defs: | |
266 namespace = self._bundle._model.namespaces[api.get('namespace')] | |
267 c.Append('schemas["%s"] = %s;' % (namespace.name, | |
268 _FormatNameAsConstant(namespace.name))) | |
269 c.Eblock('}'); | |
270 c.Append() | |
271 c.Append('std::map<std::string, const char*> schemas;') | |
272 c.Eblock('};'); | |
273 c.Append() | |
274 c.Append('base::LazyInstance<Static> g_lazy_instance;') | |
275 c.Append() | |
276 c.Append('// static') | |
277 c.Sblock('base::StringPiece GeneratedSchemas::Get(' | |
278 'const std::string& name) {') | |
279 c.Append('return IsGenerated(name) ? ' | |
280 'g_lazy_instance.Get().schemas[name] : "";') | |
281 c.Eblock('}') | |
282 c.Append() | |
283 c.Append('// static') | |
284 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') | |
285 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') | |
not at google - send to devlin
2013/05/24 19:09:18
hey could you post on gist what the output generat
cduvall
2013/05/30 00:50:51
https://gist.github.com/anonymous/5667331
| |
253 c.Eblock('}') | 286 c.Eblock('}') |
254 c.Append() | 287 c.Append() |
255 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 288 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
256 c.Append() | 289 c.Append() |
257 return c | 290 return c |
OLD | NEW |