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