1#include <rice/rice.hpp>
13 _T_* convert(VALUE value)
15 this->converted_ = Rice::detail::From_Ruby<_T_>().convert(value);
16 return &this->converted_;
23 template<
typename _T_>
34 _T_& convert(VALUE value)
36 if (value == Qnil && this->arg_ && this->arg_->hasDefaultValue())
38 return this->arg_->defaultValue<_T_>();
42 this->converted_ = Rice::detail::From_Ruby<_T_>().convert(value);
43 return this->converted_;
48 Rice::Arg* arg_ =
nullptr;
69 class To_Ruby<QString>
72 VALUE convert(QString
const& x)
74 return To_Ruby<std::string>().convert(x.toStdString());
79 class To_Ruby<QString&>
82 VALUE convert(QString
const& x)
84 return To_Ruby<std::string>().convert(x.toStdString());
89 class From_Ruby<QString>
92 From_Ruby() =
default;
94 explicit From_Ruby(Arg* arg) : arg_(arg)
98 bool is_convertible(VALUE value)
100 return From_Ruby<std::string>().is_convertible(value);
103 QString convert(VALUE value)
105 if (value == Qnil && this->arg_ && this->arg_->hasDefaultValue())
107 return this->arg_->defaultValue<QString>();
111 return QString::fromStdString(From_Ruby<std::string>().convert(value));
144 struct Type<QStringList>
153 VALUE qlistToArray(
const QList<T>& value)
155 VALUE r_arr = detail::protect(rb_ary_new);
156 for(
const T& t : value)
158 detail::protect(rb_ary_push, r_arr, detail::To_Ruby<T>().convert(t));
169 return qlistToArray(x);
179 return qlistToArray(x);
184 class To_Ruby<QStringList> :
public To_Ruby<QList<QString>>
188 class To_Ruby<QStringList&> :
public To_Ruby<QList<QString>&>
193 QList<T> qlistFromArray(VALUE value)
195 size_t length = protect(rb_array_len, value);
197 result.reserve(length);
199 for (
size_t i = 0; i < length; i++)
201 VALUE element = protect(rb_ary_entry, value, i);
202 result.append(From_Ruby<T>().convert(element));
212 From_Ruby() =
default;
214 explicit From_Ruby(Arg * arg) : arg_(arg)
220 switch (rb_type(value))
225 return *Data_Object<QList<T>>::from_ruby(value);
230 if constexpr (std::is_default_constructible_v<T>)
232 return qlistFromArray<T>(value);
237 if (this->arg_ && this->arg_->hasDefaultValue())
239 return this->arg_->template defaultValue<QList<T>>();
245 throw Exception(rb_eTypeError,
"wrong argument type %s (expected % s)",
246 detail::protect(rb_obj_classname, value),
"QList");
268 class From_Ruby<QStringList> :
public From_Ruby<QList<QString>>
272 class From_Ruby<QStringList*> :
public From_Ruby<QList<QString>*>
276 class From_Ruby<QStringList&> :
public From_Ruby<QList<QString>&>
282 template<
typename K,
typename V>
287 return Type<K>::verify() and Type<V>::verify();
292 template<
typename K,
typename V>
295 VALUE r_hash = detail::protect(rb_hash_new);
296 for(
auto it = value.begin(); it != value.end(); ++it)
298 detail::protect(rb_hash_aset, r_hash, detail::To_Ruby<K>().convert(it.key()), detail::To_Ruby<V>().convert(it.value()));
303 template<
typename K,
typename V>
309 return qhashToHash(x);
313 template<
typename K,
typename V>
319 return qhashToHash(x);
323 template<
typename K,
typename V>
324 int qhashFromHash_convertPair(VALUE key, VALUE value, VALUE user_data)
327 return cpp_protect([&]
329 result->operator[](From_Ruby<K>().convert(key)) = From_Ruby<V>().convert(value);
336 template<
typename K,
typename V>
340 VALUE user_data = (VALUE)(&result);
343 using Rb_Hash_ForEach_T = void(*)(VALUE, int(*)(VALUE, VALUE, VALUE), VALUE);
344 detail::protect<Rb_Hash_ForEach_T>(rb_hash_foreach, value, qhashFromHash_convertPair<K, V>, user_data);
349 template<
typename K,
typename V>
353 From_Ruby() =
default;
355 explicit From_Ruby(Arg * arg) : arg_(arg)
361 switch (rb_type(value))
366 return *Data_Object<QHash<K, V>>::from_ruby(value);
371 if constexpr (std::is_default_constructible_v<V>)
373 return qhashFromHash<K, V>(value);
378 if (this->arg_ && this->arg_->hasDefaultValue())
380 return this->arg_->template defaultValue<QHash<K, V>>();
385 throw Exception(rb_eTypeError,
"wrong argument type %s (expected % s)",
386 detail::protect(rb_obj_classname, value),
"QHash");
395 template<
typename K,
typename V>
401 template<
typename K,
typename V>