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.
11 class HGenerator(object):
12 def Generate(self
, features
, source_file
, namespace
):
13 return _Generator(features
, source_file
, namespace
).Generate()
16 class _Generator(object):
17 """A .cc generator for features.
19 def __init__(self
, features
, source_file
, namespace
):
20 self
._feature
_defs
= features
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
27 """Generates a Code object for features.
30 (c
.Append(cpp_util
.CHROMIUM_LICENSE
)
32 .Append(cpp_util
.GENERATED_FEATURE_MESSAGE
% self
._source
_file
)
36 # Hack: for the purpose of gyp the header file will always be the source
37 # file with its file extension replaced by '.h'. Assume so.
38 output_file
= os
.path
.splitext(self
._namespace
.source_file
)[0] + '.h'
39 ifndef_name
= cpp_util
.GenerateIfndefName(output_file
)
41 (c
.Append('#ifndef %s' % ifndef_name
)
42 .Append('#define %s' % ifndef_name
)
46 (c
.Append('#include <map>')
47 .Append('#include <string>')
49 .Concat(cpp_util
.OpenNamespace(self
._namespace
))
53 (c
.Append('class %s {' % self
._class
_name
)
56 .Concat(self
._GeneratePublicBody
())
60 .Concat(self
._GeneratePrivateBody
())
63 .Cblock(cpp_util
.CloseNamespace(self
._namespace
))
65 (c
.Append('#endif // %s' % ifndef_name
)
70 def _GeneratePublicBody(self
):
73 (c
.Append('%s();' % self
._class
_name
)
76 .Concat(self
._GenerateEnumConstants
())
79 .Append('const char* ToString(ID id) const;')
80 .Append('ID FromString(const std::string& id) const;')
85 def _GeneratePrivateBody(self
):
86 return Code().Append('std::map<std::string, '
87 '%s::ID> features_;' % self
._class
_name
)
89 def _GenerateEnumConstants(self
):
95 for feature
in self
._feature
_defs
:
96 c
.Append('%s,' % cpp_util
.ConstantName(feature
.name
))
97 c
.Append('kEnumBoundary')