1# Copyright 2016 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS-IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14from typing import List
15
16import networkx as nx
17
18
19def generate_files(injection_graph: nx.DiGraph, generate_runtime_bench_code: bool, use_normalized_component: bool=False):
20    if use_normalized_component:
21        assert not generate_runtime_bench_code
22
23    file_content_by_name = dict()
24
25    for node_id in injection_graph.nodes:
26        file_content_by_name['component%s.h' % node_id] = _generate_component_header(node_id)
27        file_content_by_name['component%s.cpp' % node_id] = _generate_component_source(node_id, list(injection_graph.successors(node_id)))
28
29    [toplevel_node] = [node_id
30                       for node_id in injection_graph.nodes
31                       if not any(True for p in injection_graph.predecessors(node_id))]
32    file_content_by_name['main.cpp'] = _generate_main(toplevel_node, generate_runtime_bench_code)
33
34    return file_content_by_name
35
36def _get_component_type(component_index: int):
37    return 'fruit::Component<Interface{component_index}>'.format(**locals())
38
39def _generate_component_header(component_index: int):
40    component_type = _get_component_type(component_index)
41    template = """
42#ifndef COMPONENT{component_index}_H
43#define COMPONENT{component_index}_H
44
45#include <fruit/fruit.h>
46
47// Example include that the code might use
48#include <vector>
49
50struct Interface{component_index} {{
51  virtual ~Interface{component_index}() = default;
52}};
53
54{component_type} getComponent{component_index}();
55
56#endif // COMPONENT{component_index}_H
57"""
58    return template.format(**locals())
59
60def _generate_component_source(component_index: int, deps: List[int]):
61    include_directives = ''.join(['#include "component%s.h"\n' % index for index in deps + [component_index]])
62
63    fields = ''.join(['Interface%s& x%s;\n' % (dep, dep)
64                      for dep in deps])
65
66    component_deps = ', '.join(['Interface%s& x%s' % (dep, dep)
67                                for dep in deps])
68    param_initializers = ', '.join('x%s(x%s)' % (dep, dep)
69                                   for dep in deps)
70    if param_initializers:
71        param_initializers = ': ' + param_initializers
72
73    install_expressions = ''.join(['        .install(getComponent%s)\n' % dep for dep in deps])
74
75    component_type = _get_component_type(component_index)
76
77    template = """
78{include_directives}
79
80namespace {{
81struct X{component_index} : public Interface{component_index} {{
82  {fields}
83
84  INJECT(X{component_index}({component_deps})) {param_initializers} {{}}
85
86  virtual ~X{component_index}() = default;
87}};
88}}
89
90"""
91
92    template += """
93{component_type} getComponent{component_index}() {{
94    return fruit::createComponent(){install_expressions}
95        .bind<Interface{component_index}, X{component_index}>();
96}}
97"""
98
99    return template.format(**locals())
100
101def _generate_main(toplevel_component: int, generate_runtime_bench_code: bool):
102    if generate_runtime_bench_code:
103        template = """
104#include "component{toplevel_component}.h"
105
106#include <ctime>
107#include <iostream>
108#include <cstdlib>
109#include <iomanip>
110#include <chrono>
111
112using namespace std;
113
114fruit::Component<> getEmptyComponent() {{
115  return fruit::createComponent();
116}}
117
118int main(int argc, char* argv[]) {{
119  if (argc != 2) {{
120    std::cout << "Need to specify num_loops as argument." << std::endl;
121    exit(1);
122  }}
123  size_t num_loops = std::atoi(argv[1]);
124
125  fruit::NormalizedComponent<Interface{toplevel_component}> normalizedComponent(getComponent{toplevel_component});
126
127  std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
128  for (size_t i = 0; i < num_loops; i++) {{
129    fruit::Injector<Interface{toplevel_component}> injector(normalizedComponent, getEmptyComponent);
130    injector.get<std::shared_ptr<Interface{toplevel_component}>>();
131  }}
132  double perRequestTime = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start_time).count();
133
134  std::cout << std::fixed;
135  std::cout << std::setprecision(15);
136  std::cout << "Total per request          = " << perRequestTime / num_loops << std::endl;
137  return 0;
138}}
139    """
140    else:
141        template = """
142#include "component{toplevel_component}.h"
143
144#include <iostream>
145
146fruit::Component<> getEmptyComponent() {{
147  return fruit::createComponent();
148}}
149
150int main(void) {{
151  fruit::NormalizedComponent<Interface{toplevel_component}> normalizedComponent(getComponent{toplevel_component});
152  fruit::Injector<Interface{toplevel_component}> injector(normalizedComponent, getEmptyComponent);
153  injector.get<std::shared_ptr<Interface{toplevel_component}>>();
154  std::cout << "Hello, world" << std::endl;
155  return 0;
156}}
157    """
158
159    return template.format(**locals())
160