Index: tools/json_schema_compiler/cpp_bundle_generator.py |
diff --git a/tools/json_schema_compiler/cpp_bundle_generator.py b/tools/json_schema_compiler/cpp_bundle_generator.py |
index 453e4466cc03a240007ce3f26c959897c03770a8..cc42a637cb2d647f209c67dd3f299adfdb37081d 100644 |
--- a/tools/json_schema_compiler/cpp_bundle_generator.py |
+++ b/tools/json_schema_compiler/cpp_bundle_generator.py |
@@ -215,14 +215,20 @@ class _SchemasHGenerator(object): |
c.Append() |
c.Append('class GeneratedSchemas {') |
c.Sblock(' public:') |
- c.Append('// Puts all API schemas in |schemas|.') |
- c.Append('static void Get(' |
- 'std::map<std::string, base::StringPiece>* schemas);') |
+ c.Append('// Determines if schema named |name| is generated.') |
+ c.Append('static bool IsGenerated(std::string name);') |
+ c.Append('// Gets the API schema named |name|.') |
not at google - send to devlin
2013/05/23 00:09:40
nit: c.Append() before this
cduvall
2013/05/24 03:13:49
Done.
|
+ c.Append('static base::StringPiece Get(const std::string& name);') |
c.Eblock('};'); |
c.Append() |
c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
return self._bundle._GenerateHeader('generated_schemas', c) |
+def _FormatName(name): |
+ return re.sub('_[a-z]', |
+ lambda m: m.group(0)[1].upper(), |
+ name.replace('.', '_')).capitalize() |
not at google - send to devlin
2013/05/23 00:09:40
what does this do? can you use model.UnixName inst
cduvall
2013/05/24 03:13:49
Formats the name as a constant. I thought it would
not at google - send to devlin
2013/05/24 19:09:18
sgtm, thanks.
|
+ |
class _SchemasCCGenerator(object): |
"""Generates a code.Code object for the generated schemas .cc file""" |
@@ -236,11 +242,9 @@ class _SchemasCCGenerator(object): |
c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, |
'generated_schemas.h'))) |
c.Append() |
- c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
+ c.Append('#include "base/lazy_instance.h"') |
c.Append() |
- c.Append('// static') |
- c.Sblock('void GeneratedSchemas::Get(' |
- 'std::map<std::string, base::StringPiece>* schemas) {') |
+ c.Append('namespace {') |
for api in self._bundle._api_defs: |
namespace = self._bundle._model.namespaces[api.get('namespace')] |
# JSON parsing code expects lists of schemas, so dump a singleton list. |
@@ -249,7 +253,35 @@ class _SchemasCCGenerator(object): |
# Escape all double-quotes and backslashes. For this to output a valid |
# JSON C string, we need to escape \ and ". |
json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
- c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
+ c.Append('const char k%s[] = "%s";' % (_FormatName(namespace.name), |
+ json_content)) |
+ c.Append('}') |
+ c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
+ c.Append() |
+ c.Sblock('struct Static {') |
+ c.Sblock('Static() {') |
+ for api in self._bundle._api_defs: |
+ namespace = self._bundle._model.namespaces[api.get('namespace')] |
+ c.Append('schemas["%s"] = k%s;' % (namespace.name, |
+ _FormatName(namespace.name))) |
+ c.Eblock('}'); |
+ c.Append() |
+ c.Append('std::map<std::string, const char*> schemas;') |
not at google - send to devlin
2013/05/23 00:09:40
would have preferred this whole class to be a lazy
|
+ c.Eblock('};'); |
+ c.Append() |
+ c.Append('base::LazyInstance<Static> g_lazy_instance;') |
+ c.Append() |
+ c.Append('// static') |
+ c.Sblock('base::StringPiece GeneratedSchemas::Get(' |
+ 'const std::string& name) {') |
+ c.Append('if (IsGenerated(name))') |
+ c.Append(' return g_lazy_instance.Get().schemas[name];') |
+ c.Append('return "";'); |
not at google - send to devlin
2013/05/23 00:09:40
ternary?
cduvall
2013/05/24 03:13:49
Done.
|
+ c.Eblock('}') |
+ c.Append() |
+ c.Append('// static') |
+ c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') |
+ c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') |
c.Eblock('}') |
c.Append() |
c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |