Represents a C++ namespace, which contains other namespaces and functions. gen_prototypes() generates the code for the namespace.
| 9 | |
| 10 | |
| 11 | class Namespace: |
| 12 | """ |
| 13 | Represents a C++ namespace, which contains other namespaces and functions. |
| 14 | |
| 15 | gen_prototypes() generates the code for the namespace. |
| 16 | """ |
| 17 | |
| 18 | def __init__(self): |
| 19 | self.namespaces = collections.defaultdict(self.__class__) |
| 20 | self.functions = [] |
| 21 | |
| 22 | def add_functionname(self, path): |
| 23 | """ |
| 24 | Adds a function to the namespace. |
| 25 | |
| 26 | Path is the qualified function "path" (e.g., openage::test::foo) |
| 27 | has the path ["openage", "test", "foo"]. |
| 28 | |
| 29 | Descends recursively, creating subnamespaces as required. |
| 30 | """ |
| 31 | if len(path) == 1: |
| 32 | self.functions.append(path[0]) |
| 33 | else: |
| 34 | subnamespace = self.namespaces[path[0]] |
| 35 | subnamespace.add_functionname(path[1:]) |
| 36 | |
| 37 | def gen_prototypes(self): |
| 38 | """ |
| 39 | Generates the actual C++ code for this namespace, |
| 40 | including all sub-namespaces and function prototypes. |
| 41 | """ |
| 42 | for name in self.functions: |
| 43 | yield f"void {name}();\n" |
| 44 | |
| 45 | for namespacename, namespace in sorted(self.namespaces.items()): |
| 46 | yield f"namespace {namespacename} {{\n" |
| 47 | for line in namespace.gen_prototypes(): |
| 48 | yield line |
| 49 | yield f"}} // {namespacename}\n\n" |
| 50 | |
| 51 | def get_functionnames(self): |
| 52 | """ |
| 53 | Yields all function names in this namespace, |
| 54 | as well as all subnamespaces. |
| 55 | """ |
| 56 | for name in self.functions: |
| 57 | yield name |
| 58 | |
| 59 | for namespacename, namespace in sorted(self.namespaces.items()): |
| 60 | for name in namespace.get_functionnames(): |
| 61 | yield namespacename + "::" + name |
| 62 | |
| 63 | |
| 64 | def generate_testlist(projectdir): |
no outgoing calls