1# Copyright 2019 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
6class CodeGenAccumulator(object):
7    """
8    Accumulates a variety of information and helps generate code based on the
9    information.
10    """
11
12    def __init__(self):
13        # Headers to be included
14        self._include_headers = set()
15        # Forward declarations of C++ class
16        self._class_decls = set()
17        # Forward declarations of C++ struct
18        self._struct_decls = set()
19
20    def total_size(self):
21        return (len(self.include_headers) + len(self.class_decls) + len(
22            self.struct_decls))
23
24    @property
25    def include_headers(self):
26        return self._include_headers
27
28    def add_include_headers(self, headers):
29        self._include_headers.update(filter(None, headers))
30
31    @staticmethod
32    def require_include_headers(headers):
33        return lambda accumulator: accumulator.add_include_headers(headers)
34
35    @property
36    def class_decls(self):
37        return self._class_decls
38
39    def add_class_decls(self, class_names):
40        self._class_decls.update(filter(None, class_names))
41
42    @staticmethod
43    def require_class_decls(class_names):
44        return lambda accumulator: accumulator.add_class_decls(class_names)
45
46    @property
47    def struct_decls(self):
48        return self._struct_decls
49
50    def add_struct_decls(self, struct_names):
51        self._struct_decls.update(filter(None, struct_names))
52
53    @staticmethod
54    def require_struct_decls(struct_names):
55        return lambda accumulator: accumulator.add_struct_decls(struct_names)
56