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
)
35 ifndef_name
= cpp_util
.GenerateIfndefName(self
._source
_file
_filename
,
37 (c
.Append('#ifndef %s' % ifndef_name
)
38 .Append('#define %s' % ifndef_name
)
42 (c
.Append('#include <map>')
43 .Append('#include <string>')
45 .Concat(cpp_util
.OpenNamespace(self
._namespace
))
49 (c
.Append('class %s {' % self
._class
_name
)
52 .Concat(self
._GeneratePublicBody
())
56 .Concat(self
._GeneratePrivateBody
())
59 .Cblock(cpp_util
.CloseNamespace(self
._namespace
))
61 (c
.Append('#endif // %s' % ifndef_name
)
66 def _GeneratePublicBody(self
):
69 (c
.Append('%s();' % self
._class
_name
)
72 .Concat(self
._GenerateEnumConstants
())
75 .Append('const char* ToString(ID id) const;')
76 .Append('ID FromString(const std::string& id) const;')
81 def _GeneratePrivateBody(self
):
82 return Code().Append('std::map<std::string, '
83 '%s::ID> features_;' % self
._class
_name
)
85 def _GenerateEnumConstants(self
):
91 for feature
in self
._feature
_defs
:
92 c
.Append('%s,' % cpp_util
.ConstantName(feature
.name
))
93 c
.Append('kEnumBoundary')