1#include <rice/rice.hpp>
12 Rice::detail::Convertible is_convertible(VALUE value)
14 return Rice::detail::From_Ruby<_T_>().is_convertible(value);
16 _T_* convert(VALUE value)
18 this->converted_ = Rice::detail::From_Ruby<_T_>().convert(value);
19 return &this->converted_;
25 template<
typename _T_>
34 Rice::detail::Convertible is_convertible(VALUE value)
36 return Rice::detail::From_Ruby<_T_>().is_convertible(value);
39 _T_& convert(VALUE value)
41 if(value == Qnil && this->arg_ && this->arg_->hasDefaultValue())
43 return this->arg_->template defaultValue<_T_>();
47 this->converted_ = Rice::detail::From_Ruby<_T_>().convert(value);
48 return this->converted_;
52 Rice::Arg* arg_ =
nullptr;
65 static bool verify() {
return true; }
69 class To_Ruby<QString>
72 VALUE convert(QString
const& x) {
return To_Ruby<std::string>().convert(x.toStdString()); }
76 class To_Ruby<QString&>
79 VALUE convert(QString
const& x) {
return To_Ruby<std::string>().convert(x.toStdString()); }
83 class From_Ruby<QString>
86 From_Ruby() =
default;
88 explicit From_Ruby(Arg* arg) : arg_(arg) {}
90 Rice::detail::Convertible is_convertible(VALUE value)
92 return From_Ruby<std::string>().is_convertible(value);
95 QString convert(VALUE value)
97 if(value == Qnil && this->arg_ && this->arg_->hasDefaultValue())
99 return this->arg_->defaultValue<QString>();
103 return QString::fromStdString(From_Ruby<std::string>().convert(value));
137 static bool verify() {
return true; }
141 VALUE qlistToArray(
const QList<T>& value)
143 VALUE r_arr = detail::protect(rb_ary_new);
144 for(
const T& t : value)
146 detail::protect(rb_ary_push, r_arr, detail::To_Ruby<T>().convert(t));
155 VALUE convert(
QList<T> const& x) {
return qlistToArray(x); }
162 VALUE convert(
QList<T> const& x) {
return qlistToArray(x); }
165 QList<T> qlistFromArray(VALUE value)
167 size_t length = protect(rb_array_len, value);
169 result.reserve(length);
171 for(
size_t i = 0; i < length; i++)
173 VALUE element = protect(rb_ary_entry, value, i);
174 result.append(From_Ruby<T>().convert(element));
184 From_Ruby() =
default;
186 explicit From_Ruby(Arg* arg) : arg_(arg) {}
188 Convertible is_convertible(VALUE value)
190 switch(rb_type(value))
193 return Convertible::Exact;
196 return Convertible::Cast;
199 return Convertible::None;
205 switch(rb_type(value))
210 return *Data_Object<QList<T>>::from_ruby(value);
215 if constexpr(std::is_default_constructible_v<T>)
217 return qlistFromArray<T>(value);
222 if(this->arg_ && this->arg_->hasDefaultValue())
224 return this->arg_->template defaultValue<QList<T>>();
230 throw Exception(rb_eTypeError,
"wrong argument type %s (expected % s)",
231 detail::protect(rb_obj_classname, value),
"QList");
253 template<
typename K,
typename V>
256 static bool verify() {
return Type<K>::verify() and Type<V>::verify(); }
259 template<
typename K,
typename V>
262 VALUE r_hash = detail::protect(rb_hash_new);
263 for(
auto it = value.begin(); it != value.end(); ++it)
265 detail::protect(rb_hash_aset, r_hash, detail::To_Ruby<K>().convert(it.key()),
266 detail::To_Ruby<V>().convert(it.value()));
271 template<
typename K,
typename V>
275 VALUE convert(
QHash<K, V> const& x) {
return qhashToHash(x); }
278 template<
typename K,
typename V>
282 VALUE convert(
QHash<K, V> const& x) {
return qhashToHash(x); }
285 template<
typename K,
typename V>
286 int qhashFromHash_convertPair(VALUE key, VALUE value, VALUE user_data)
292 result->operator[](From_Ruby<K>().convert(key)) = From_Ruby<V>().convert(value);
297 template<
typename K,
typename V>
301 VALUE user_data = (VALUE)(&result);
304 using Rb_Hash_ForEach_T = void (*)(VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
305 detail::protect<Rb_Hash_ForEach_T>(rb_hash_foreach, value, qhashFromHash_convertPair<K, V>,
311 template<
typename K,
typename V>
315 From_Ruby() =
default;
317 explicit From_Ruby(Arg* arg) : arg_(arg) {}
319 Convertible is_convertible(VALUE value)
321 switch(rb_type(value))
324 return Convertible::Exact;
327 return Convertible::Exact;
330 return Convertible::Cast;
333 return Convertible::None;
339 switch(rb_type(value))
344 return *Data_Object<QHash<K, V>>::from_ruby(value);
349 if constexpr(std::is_default_constructible_v<V>)
351 return qhashFromHash<K, V>(value);
356 if(this->arg_ && this->arg_->hasDefaultValue())
358 return this->arg_->template defaultValue<QHash<K, V>>();
363 throw Exception(rb_eTypeError,
"wrong argument type %s (expected % s)",
364 detail::protect(rb_obj_classname, value),
"QHash");
372 template<
typename K,
typename V>
378 template<
typename K,
typename V>