4#include "registry_types.h"
6#include "downward/utils/strings.h"
15#include <unordered_map>
22namespace downward::cli::plugins {
27 virtual ~Type() =
default;
29 virtual bool operator==(
const Type& other)
const = 0;
30 bool operator!=(
const Type& other)
const;
32 virtual bool is_basic_type()
const;
33 virtual const std::type_index& get_basic_type_index()
const;
35 virtual bool is_feature_type()
const;
36 virtual bool supports_variable_binding()
const;
37 virtual std::string get_synopsis()
const;
39 virtual bool is_list_type()
const;
40 virtual bool has_nested_type()
const;
41 virtual const Type& get_nested_type()
const;
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;
47 virtual bool is_symbol_type()
const;
49 virtual bool can_convert_into(
const Type& other)
const;
51 virtual std::string name()
const = 0;
52 virtual size_t get_hash()
const = 0;
55class BasicType :
public Type {
57 std::string class_name;
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;
69class FeatureType :
public Type {
70 std::type_index pointer_type;
71 std::string type_name;
73 bool can_be_bound_to_variable;
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;
89class ListType :
public Type {
90 const Type& nested_type;
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;
103class EmptyListType :
public Type {
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;
112class EnumType :
public Type {
113 std::type_index type;
114 std::vector<std::string> values;
115 EnumInfo documented_values;
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;
122 get_enum_index(
const std::string& value, ::utils::Context& context)
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;
129class SymbolType :
public Type {
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;
139 template <
typename T>
141 static const Type& value(TypeRegistry& registry);
144 template <
typename T>
145 struct TypeOf<
std::vector<T>> {
146 static const Type& value(TypeRegistry& registry);
149 struct SemanticHash {
150 size_t operator()(
const Type* t)
const
155 return t->get_hash();
158 struct SemanticEqual {
159 size_t operator()(
const Type* t1,
const Type* t2)
const
168 std::unordered_map<std::type_index, std::unique_ptr<Type>> registered_types;
171 std::unique_ptr<ListType>,
174 registered_list_types;
175 template <
typename T>
176 void insert_basic_type();
177 const Type& get_nonlist_type(std::type_index type)
const;
180 static BasicType NO_TYPE;
181 static SymbolType SYMBOL_TYPE;
182 static EmptyListType EMPTY_LIST_TYPE;
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);
190 template <
typename T>
191 const Type& get_type();
193 static TypeRegistry* instance()
195 static TypeRegistry instance_;
201const Type& TypeRegistry::TypeOf<T>::value(TypeRegistry& registry)
203 return registry.get_nonlist_type(
typeid(T));
207const Type& TypeRegistry::TypeOf<std::vector<T>>::value(TypeRegistry& registry)
209 return registry.create_list_type(registry.get_type<T>());
213const Type& TypeRegistry::get_type()
215 return TypeOf<T>::value(*
this);
218extern std::any convert(
219 const std::any& value,
220 const Type& from_type,
222 ::utils::Context& context);