Debian Codegraph @

src/internal/types-model.cpp

raw symbols SCIP
1 /*
2 * %injeqt copyright begin%
3 * Copyright 2014 Rafał Malinowski ([email protected])
4 * %injeqt copyright end%
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "types-model.h"
22
23 #include <injeqt/exception/ambiguous-types.h>
24 #include <injeqt/exception/unresolvable-dependencies.h>
25
26 #include "type-relations.h"
27
28 #include <cassert>
29
30 namespace injeqt { namespace internal {
31
32 types_model::types_model()
33 {
34 }
35
36 types_model::types_model(implemented_by_mapping available_types, types_dependencies mapped_dependencies) :
37 _available_types{std::move(available_types)},
38 _mapped_dependencies{std::move(mapped_dependencies)}
39 {
40 }
41
42 const implemented_by_mapping & types_model::available_types() const
43 {
44 return _available_types;
45 }
46
47 const types_dependencies & types_model::mapped_dependencies() const
48 {
49 return _mapped_dependencies;
50 }
51
52 bool types_model::contains(const type &interface_type) const
53 {
54 return _available_types.get(interface_type) != end(_available_types);
55 }
56
57 std::vector<dependency> types_model::get_unresolvable_dependencies() const
58 {
59 auto result = std::vector<dependency>{};
60 for (auto &&mapped_type_dependency : _mapped_dependencies)
61 for (auto &&dependency : mapped_type_dependency.dependency_list())
62 if (!contains(dependency.required_type()))
63 result.push_back(dependency);
64 return result;
65 }
66
67 types_model make_types_model(const types_by_name &known_types, const std::vector<type> &all_types, const std::vector<type> &need_dependencies)
68 {
69 auto relations = make_type_relations(all_types);
70 validate_non_ambiguous(all_types, relations);
71
72 auto all_dependencies = std::vector<type_dependencies>{};
73 std::transform(std::begin(need_dependencies), std::end(need_dependencies), std::back_inserter(all_dependencies),
74 [&](const type &t){ return make_type_dependencies(known_types, t); });
75
76 auto available_types = relations.unique();
77 auto mapped_dependencies = types_dependencies{all_dependencies};
78 auto result = types_model(available_types, mapped_dependencies);
79 validate_non_unresolvable(result);
80
81 return result;
82 }
83
84 void validate_non_unresolvable(const types_model &model)
85 {
86 auto unresolvable_dependencies = model.get_unresolvable_dependencies();
87
88 if (!unresolvable_dependencies.empty())
89 {
90 auto message = std::string{};
91 for (auto &&unresolvable_dependency : unresolvable_dependencies)
92 {
93 message.append(unresolvable_dependency.required_type().name());
94 message.append(": ");
95 message.append(unresolvable_dependency.setter().signature());
96 message.append("\n");
97 }
98 throw exception::unresolvable_dependencies{message};
99 }
100 }
101
102 }}