Debian Codegraph @

src/internal/type-relations.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 "type-relations.h"
22
23 #include <injeqt/exception/ambiguous-types.h>
24
25 #include "interfaces-utils.h"
26
27 #include <map>
28
29 namespace injeqt { namespace internal {
30
31 type_relations::type_relations()
32 {
33 }
34
35 type_relations::type_relations(implemented_by_mapping unique, types ambiguous) :
36 _unique{std::move(unique)},
37 _ambiguous{std::move(ambiguous)}
38 {
39 }
40
41 const implemented_by_mapping & type_relations::unique() const
42 {
43 return _unique;
44 }
45
46 const types & type_relations::ambiguous() const
47 {
48 return _ambiguous;
49 }
50
51 type_relations make_type_relations(const std::vector<type> &main_types)
52 {
53 auto type_count = std::map<type, std::size_t>{};
54 auto implemented_by_type = std::map<type, type>{};
55
56 for (auto &&main_type : main_types)
57 {
58 auto interface_types = extract_interfaces(main_type);
59 for (auto &&interface_type : interface_types)
60 {
61 type_count[interface_type]++;
62 implemented_by_type.insert({interface_type, main_type});
63 }
64 }
65
66 auto unique = std::vector<implemented_by>{};
67 auto ambiguous = std::vector<type>{};
68 for (auto &&counted_type : type_count)
69 if (counted_type.second == 1)
70 unique.push_back(implemented_by{counted_type.first, implemented_by_type.at(counted_type.first)});
71 else if (counted_type.second > 1)
72 ambiguous.push_back(counted_type.first);
73
74 return type_relations{implemented_by_mapping{unique}, types{ambiguous}};
75 }
76
77 void validate_non_ambiguous(const std::vector<type> &types, const type_relations &relations)
78 {
79 for (auto &&t : types)
80 {
81 auto message = std::string{};
82 if (relations.ambiguous().contains(t))
83 {
84 message.append(t.name());
85 message.append("\n");
86 }
87
88 if (!message.empty())
89 throw exception::ambiguous_types{message};
90 }
91 }
92
93 }}