knowL: Knowledge Libraries
Loading...
Searching...
No Matches
knowCore.h
1#include <knowCore/BigNumber.h>
2#include <knowCore/UriList.h>
3#include <knowCore/ValueHash.h>
4
5#include <knowRuby/ConverterManager.h>
6#include <knowRuby/HandleResult.h>
7
8#include "qt.h"
9
10namespace Rice::detail
11{
12
13 // cres_qresult
14 template<typename _T_>
15 struct Type<cres_qresult<_T_>>
16 {
17 static bool verify() { return Type<_T_>::verify(); }
18 };
19 template<typename _T_>
20 struct To_Ruby<cres_qresult<_T_>>
21 {
22 VALUE convert(const cres_qresult<_T_>& x)
23 {
24 return To_Ruby<_T_>().convert(knowRuby::handle(x));
25 }
26 };
27 template<>
28 struct Type<cres_qresult<void>>
29 {
30 static bool verify() { return true; }
31 };
32 template<>
33 struct To_Ruby<cres_qresult<void>>
34 {
35 VALUE convert(const cres_qresult<void>& x)
36 {
37 if(x.is_successful())
38 {
39 return Qnil;
40 }
41 else
42 {
43 throw std::runtime_error(x.get_error().get_message().toStdString());
44 }
45 }
46 };
47
48 // knowCore::BigNumber
49 template<>
50 struct Type<knowCore::BigNumber>
51 {
52 static bool verify() { return true; }
53 };
54
55 template<>
56 class To_Ruby<knowCore::BigNumber>
57 {
58 public:
59 VALUE convert(knowCore::BigNumber const& x)
60 {
61 if(x.isFloating())
62 {
63 return To_Ruby<double>().convert(x.toDouble());
64 }
65 else
66 {
67 return rb_cstr_to_inum(qPrintable(x.toString()), 10, 1);
68 }
69 }
70 };
71
72 template<>
73 class To_Ruby<knowCore::BigNumber&> : To_Ruby<knowCore::BigNumber>
74 {
75 };
76
77 template<>
78 class From_Ruby<knowCore::BigNumber>
79 {
80 public:
81 From_Ruby() = default;
82
83 explicit From_Ruby(Arg* arg) : arg_(arg) {}
84 Rice::detail::Convertible is_convertible(VALUE value)
85 {
86 ruby_value_type vt = rb_type(value);
87 return (vt == RUBY_T_FLOAT or vt == RUBY_T_FIXNUM or vt == RUBY_T_BIGNUM)
88 ? Rice::detail::Convertible::Exact
89 : Rice::detail::Convertible::None;
90 }
91 knowCore::BigNumber convert(VALUE value)
92 {
93 if(value == Qnil && this->arg_ && this->arg_->hasDefaultValue())
94 {
95 return this->arg_->defaultValue<knowCore::BigNumber>();
96 }
97 else
98 {
99 switch(rb_type(value))
100 {
101 case RUBY_T_FLOAT:
102 return knowCore::BigNumber(protect(rb_num2dbl, value));
103 case RUBY_T_FIXNUM:
104 return knowCore::BigNumber(protect(rb_num2short_inline, value));
105 case RUBY_T_BIGNUM:
106 {
107 auto const& [success, VAL, errorMessage]
108 = knowCore::BigNumber::fromString(From_Ruby<QString>().convert(rb_big2str(value, 10)));
109 if(success)
110 {
111 return VAL.value();
112 }
113 else
114 {
115 throw std::runtime_error(errorMessage.value().get_message().toStdString());
116 }
117 }
118 default:
119 {
120 throw Exception(rb_eTypeError, "wrong argument type %s (expected % s)",
121 detail::protect(rb_obj_classname, value), "number");
122 }
123 }
124 }
125 }
126 private:
127 Arg* arg_ = nullptr;
128 };
129
130 template<>
131 struct From_Ruby<knowCore::BigNumber*> : public rice_qt::From_Ruby_Ptr<knowCore::BigNumber>
132 {
134 };
135
136 template<>
137 struct From_Ruby<knowCore::BigNumber&> : public rice_qt::From_Ruby_Ref<knowCore::BigNumber>
138 {
140 };
141 // knowCore::Value
142 template<>
143 struct Type<knowCore::Value>
144 {
145 static bool verify() { return true; }
146 };
147
148 VALUE knowCoreValueToVALUE(knowCore::Value const& _value)
149 {
150 auto const& [success, VAL, error] = knowRuby::ConverterManager::toRubyValue(_value);
151 if(success)
152 {
153 return VAL.value();
154 }
155 else
156 {
157 throw std::runtime_error(error.value().get_message().toStdString());
158 }
159 }
160
161 template<>
162 class To_Ruby<knowCore::Value>
163 {
164 public:
165 VALUE convert(knowCore::Value const& x) { return knowCoreValueToVALUE(x); }
166 };
167
168 template<>
169 class To_Ruby<knowCore::Value&>
170 {
171 public:
172 VALUE convert(knowCore::Value const& x) { return knowCoreValueToVALUE(x); }
173 };
174
175 template<>
176 class From_Ruby<knowCore::Value>
177 {
178 public:
179 From_Ruby() = default;
180
181 explicit From_Ruby(Arg* arg) : arg_(arg) {}
182 Rice::detail::Convertible is_convertible(VALUE) { return Rice::detail::Convertible::Exact; }
183
184 knowCore::Value convert(VALUE value)
185 {
186 if(value == Qnil && this->arg_ && this->arg_->hasDefaultValue())
187 {
188 return this->arg_->defaultValue<knowCore::Value>();
189 }
190 else
191 {
192 auto const& [success, VAL, error] = knowRuby::ConverterManager::toKnowCoreValue(value);
193 if(success)
194 {
195 return VAL.value();
196 }
197 else
198 {
199 throw std::runtime_error(error.value().get_message().toStdString());
200 }
201 }
202 }
203 private:
204 Arg* arg_ = nullptr;
205 };
206
207 template<>
208 struct From_Ruby<knowCore::Value*> : public rice_qt::From_Ruby_Ptr<knowCore::Value>
209 {
210 using From_Ruby_Ptr_T::From_Ruby_Ptr;
211 };
212
213 template<>
214 struct From_Ruby<knowCore::Value&> : public rice_qt::From_Ruby_Ref<knowCore::Value>
215 {
216 using From_Ruby_Ref_T::From_Ruby_Ref;
217 };
218 // knowCore::ValueHash
219 template<>
220 struct Type<knowCore::ValueHash>
221 {
222 static bool verify() { return true; }
223 };
224
225 template<>
226 class To_Ruby<knowCore::ValueHash> : public To_Ruby<QHash<QString, knowCore::Value>>
227 {
228 };
229 // template<>
230 // class To_Ruby<knowCore::ValueHash&> : public To_Ruby<QHash<QString, knowCore::Value>&>{};
231 template<>
232 class From_Ruby<knowCore::ValueHash> : public From_Ruby<QHash<QString, knowCore::Value>>
233 {
234 };
235 template<>
236 struct From_Ruby<knowCore::ValueHash*> : public rice_qt::From_Ruby_Ptr<knowCore::ValueHash>
237 {
238 using From_Ruby_Ptr_T::From_Ruby_Ptr;
239 };
240
241 template<>
242 struct From_Ruby<knowCore::ValueHash&> : public rice_qt::From_Ruby_Ref<knowCore::ValueHash>
243 {
244 using From_Ruby_Ref_T::From_Ruby_Ref;
245 };
246 // knowCore::UriList
247 template<>
248 struct Type<knowCore::UriList>
249 {
250 static bool verify() { return true; }
251 };
252
253 template<>
254 class To_Ruby<knowCore::UriList> : public To_Ruby<QList<knowCore::Uri>>
255 {
256 };
257 // template<>
258 // class To_Ruby<knowCore::UriList&> : public To_Ruby<QList<knowCore::Uri>>{};
259 template<>
260 class From_Ruby<knowCore::UriList> : public From_Ruby<QList<knowCore::Uri>>
261 {
262 };
263 template<>
264 struct From_Ruby<knowCore::UriList*> : public rice_qt::From_Ruby_Ptr<knowCore::UriList>
265 {
266 using From_Ruby_Ptr_T::From_Ruby_Ptr;
267 };
268
269 template<>
270 struct From_Ruby<knowCore::UriList&> : public rice_qt::From_Ruby_Ref<knowCore::UriList>
271 {
272 using From_Ruby_Ref_T::From_Ruby_Ref;
273 };
274
275} // namespace Rice::detail
Class that can contains large numeric value.
Definition BigNumber.h:51
bool isFloating() const
Definition BigNumber.cpp:248
Definition ValueHash.h:13
Definition Value.h:21
Definition qt.h:9
Definition qt.h:27