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