AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
types.h
1#ifndef PLUGINS_TYPES_H
2#define PLUGINS_TYPES_H
3
4#include "registry_types.h"
5
6#include "downward/utils/strings.h"
7
8#include <algorithm>
9#include <any>
10#include <cassert>
11#include <limits>
12#include <memory>
13#include <string>
14#include <typeindex>
15#include <unordered_map>
16#include <vector>
17
18namespace utils {
19class Context;
20}
21
22namespace downward::cli::plugins {
23class CategoryPlugin;
24
25class Type {
26public:
27 virtual ~Type() = default;
28
29 virtual bool operator==(const Type& other) const = 0;
30 bool operator!=(const Type& other) const;
31
32 virtual bool is_basic_type() const;
33 virtual const std::type_index& get_basic_type_index() const;
34
35 virtual bool is_feature_type() const;
36 virtual bool supports_variable_binding() const;
37 virtual std::string get_synopsis() const;
38
39 virtual bool is_list_type() const;
40 virtual bool has_nested_type() const;
41 virtual const Type& get_nested_type() const;
42
43 virtual bool is_enum_type() const;
44 virtual int get_enum_index(const std::string&, ::utils::Context&) const;
45 virtual const EnumInfo& get_documented_enum_values() const;
46
47 virtual bool is_symbol_type() const;
48
49 virtual bool can_convert_into(const Type& other) const;
50
51 virtual std::string name() const = 0;
52 virtual size_t get_hash() const = 0;
53};
54
55class BasicType : public Type {
56 std::type_index type;
57 std::string class_name;
58
59public:
60 explicit BasicType(std::type_index type, const std::string& class_name);
61 virtual bool operator==(const Type& other) const override;
62 virtual bool is_basic_type() const override;
63 virtual const std::type_index& get_basic_type_index() const override;
64 virtual bool can_convert_into(const Type& other) const override;
65 virtual std::string name() const override;
66 virtual size_t get_hash() const override;
67};
68
69class FeatureType : public Type {
70 std::type_index pointer_type;
71 std::string type_name;
72 std::string synopsis;
73 bool can_be_bound_to_variable;
74
75public:
76 FeatureType(
77 std::type_index pointer_type,
78 const std::string& type_name,
79 const std::string& synopsis,
80 bool supports_variable_binding);
81 virtual bool operator==(const Type& other) const override;
82 virtual bool is_feature_type() const override;
83 virtual bool supports_variable_binding() const override;
84 virtual std::string get_synopsis() const override;
85 virtual std::string name() const override;
86 virtual size_t get_hash() const override;
87};
88
89class ListType : public Type {
90 const Type& nested_type;
91
92public:
93 ListType(const Type& nested_type);
94 virtual bool operator==(const Type& other) const override;
95 virtual bool is_list_type() const override;
96 virtual bool has_nested_type() const override;
97 virtual const Type& get_nested_type() const override;
98 virtual bool can_convert_into(const Type& other) const override;
99 virtual std::string name() const override;
100 virtual size_t get_hash() const override;
101};
102
103class EmptyListType : public Type {
104public:
105 virtual bool operator==(const Type& other) const override;
106 virtual bool is_list_type() const override;
107 virtual bool can_convert_into(const Type& other) const override;
108 virtual std::string name() const override;
109 virtual size_t get_hash() const override;
110};
111
112class EnumType : public Type {
113 std::type_index type;
114 std::vector<std::string> values;
115 EnumInfo documented_values;
116
117public:
118 EnumType(std::type_index type, const EnumInfo& documented_values);
119 virtual bool operator==(const Type& other) const override;
120 virtual bool is_enum_type() const override;
121 virtual int
122 get_enum_index(const std::string& value, ::utils::Context& context)
123 const override;
124 virtual const EnumInfo& get_documented_enum_values() const override;
125 virtual std::string name() const override;
126 virtual size_t get_hash() const override;
127};
128
129class SymbolType : public Type {
130public:
131 virtual bool operator==(const Type& other) const override;
132 virtual bool is_symbol_type() const override;
133 bool can_convert_into(const Type& other) const override;
134 virtual std::string name() const override;
135 virtual size_t get_hash() const override;
136};
137
138class TypeRegistry {
139 template <typename T>
140 struct TypeOf {
141 static const Type& value(TypeRegistry& registry);
142 };
143
144 template <typename T>
145 struct TypeOf<std::vector<T>> {
146 static const Type& value(TypeRegistry& registry);
147 };
148
149 struct SemanticHash {
150 size_t operator()(const Type* t) const
151 {
152 if (!t) {
153 return 0;
154 }
155 return t->get_hash();
156 }
157 };
158 struct SemanticEqual {
159 size_t operator()(const Type* t1, const Type* t2) const
160 {
161 if (!t1 || !t2) {
162 return t1 == t2;
163 }
164 return *t1 == *t2;
165 }
166 };
167
168 std::unordered_map<std::type_index, std::unique_ptr<Type>> registered_types;
169 std::unordered_map<
170 const Type*,
171 std::unique_ptr<ListType>,
172 SemanticHash,
173 SemanticEqual>
174 registered_list_types;
175 template <typename T>
176 void insert_basic_type();
177 const Type& get_nonlist_type(std::type_index type) const;
178
179public:
180 static BasicType NO_TYPE;
181 static SymbolType SYMBOL_TYPE;
182 static EmptyListType EMPTY_LIST_TYPE;
183
184 TypeRegistry();
185
186 const FeatureType& create_feature_type(const CategoryPlugin& plugin);
187 const EnumType& create_enum_type(const EnumPlugin& plugin);
188 const ListType& create_list_type(const Type& element_type);
189
190 template <typename T>
191 const Type& get_type();
192
193 static TypeRegistry* instance()
194 {
195 static TypeRegistry instance_;
196 return &instance_;
197 }
198};
199
200template <typename T>
201const Type& TypeRegistry::TypeOf<T>::value(TypeRegistry& registry)
202{
203 return registry.get_nonlist_type(typeid(T));
204}
205
206template <typename T>
207const Type& TypeRegistry::TypeOf<std::vector<T>>::value(TypeRegistry& registry)
208{
209 return registry.create_list_type(registry.get_type<T>());
210}
211
212template <typename T>
213const Type& TypeRegistry::get_type()
214{
215 return TypeOf<T>::value(*this);
216}
217
218extern std::any convert(
219 const std::any& value,
220 const Type& from_type,
221 const Type& to_type,
222 ::utils::Context& context);
223} // namespace downward::cli::plugins
224
225#endif
STL namespace.