Home | History | Annotate | Download | only in json_schema_compiler
      1 # Copyright 2013 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 import os.path
      6 
      7 from code import Code
      8 import cpp_util
      9 
     10 
     11 class CCGenerator(object):
     12   def Generate(self, feature_defs, source_file, namespace):
     13     return _Generator(feature_defs, source_file, namespace).Generate()
     14 
     15 
     16 class _Generator(object):
     17   """A .cc generator for features.
     18   """
     19   def __init__(self, feature_defs, source_file, namespace):
     20     self._feature_defs = feature_defs
     21     self._source_file = source_file
     22     self._source_file_filename, _ = os.path.splitext(source_file)
     23     self._class_name = cpp_util.ClassName(self._source_file_filename)
     24     self._namespace = namespace
     25 
     26   def Generate(self):
     27     """Generates a Code object for features.
     28     """
     29     c = Code()
     30     (c.Append(cpp_util.CHROMIUM_LICENSE)
     31       .Append()
     32       .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file)
     33       .Append()
     34       .Append('#include <string>')
     35       .Append()
     36       .Append('#include "%s.h"' % self._source_file_filename)
     37       .Append()
     38       .Append('#include "base/logging.h"')
     39       .Append()
     40       .Concat(cpp_util.OpenNamespace(self._namespace))
     41       .Append()
     42     )
     43 
     44     # Generate the constructor.
     45     (c.Append('%s::%s() {' % (self._class_name, self._class_name))
     46       .Sblock()
     47     )
     48     for feature in self._feature_defs:
     49       c.Append('features_["%s"] = %s;'
     50                        % (feature.name, cpp_util.ConstantName(feature.name)))
     51     (c.Eblock()
     52       .Append('}')
     53       .Append()
     54     )
     55 
     56     # Generate the ToString function.
     57     (c.Append('const char* %s::ToString('
     58                   '%s::ID id) const {' % (self._class_name, self._class_name))
     59       .Sblock()
     60       .Append('switch (id) {')
     61       .Sblock()
     62     )
     63     for feature in self._feature_defs:
     64       c.Append('case %s: return "%s";' %
     65           (cpp_util.ConstantName(feature.name), feature.name))
     66     (c.Append('case kUnknown: break;')
     67       .Append('case kEnumBoundary: break;')
     68       .Eblock()
     69       .Append('}')
     70       .Append('NOTREACHED();')
     71       .Append('return "";')
     72     )
     73     (c.Eblock()
     74       .Append('}')
     75       .Append()
     76     )
     77 
     78     # Generate the FromString function.
     79 
     80     (c.Append('%s::ID %s::FromString('
     81                   'const std::string& id) const {'
     82                   % (self._class_name, self._class_name))
     83       .Sblock()
     84       .Append('std::map<std::string, %s::ID>::const_iterator it'
     85                   ' = features_.find(id);' % self._class_name)
     86       .Append('if (it == features_.end())')
     87       .Append('  return kUnknown;')
     88       .Append('return it->second;')
     89       .Eblock()
     90       .Append('}')
     91       .Append()
     92       .Cblock(cpp_util.CloseNamespace(self._namespace))
     93     )
     94 
     95     return c
     96