1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Hechong.xyf
17 * \date Mar 2018
18 * \brief Interface of AiTheta Index Parameters
19 */
20
21#ifndef __AITHETA2_INDEX_PARAMS_H__
22#define __AITHETA2_INDEX_PARAMS_H__
23
24#include <ailego/container/hypercube.h>
25
26namespace aitheta2 {
27
28//! Trying compatible with T
29#define _TRYING_COMPATIBLE(cube, T, out) \
30 if (cube->compatible<T>()) \
31 return ( \
32 *out = static_cast<typename std::remove_pointer<decltype(out)>::type>( \
33 cube->unsafe_cast<T>()), \
34 true)
35
36//! Trying compatible with T (Boolean)
37#define _TRYING_COMPATIBLE_BOOL(cube, T, out) \
38 if (cube->compatible<T>()) return (*out = !!cube->unsafe_cast<T>(), true)
39
40//! Trying compatible with T (String)
41#define _TRYING_COMPATIBLE_STRING(cube, T, out) \
42 if (cube->compatible<T>()) \
43 return (out->assign(std::to_string(cube->unsafe_cast<T>())), true)
44
45//! Trying convert string
46#define _TRYING_CONVERT_STRING(cube, out) \
47 if (cube->compatible<std::string>()) \
48 return ( \
49 *out = \
50 IndexParams::StringCast<std::remove_pointer<decltype(out)>::type>( \
51 cube->unsafe_cast<std::string>()), \
52 true)
53
54/*! Index Params
55 */
56class IndexParams {
57 public:
58 //! Constructor
59 IndexParams(void) : hypercube_() {}
60
61 //! Constructor
62 IndexParams(const IndexParams &rhs) : hypercube_(rhs.hypercube_) {}
63
64 //! Constructor
65 IndexParams(IndexParams &&rhs) : hypercube_() {
66 hypercube_.swap(rhs.hypercube_);
67 }
68
69 //! Destructor
70 ~IndexParams(void) {}
71
72 //! Assignment
73 IndexParams &operator=(const IndexParams &rhs) {
74 hypercube_ = rhs.hypercube_;
75 return *this;
76 }
77
78 //! Assignment
79 IndexParams &operator=(IndexParams &&rhs) {
80 hypercube_.swap(rhs.hypercube_);
81 return *this;
82 }
83
84 //! Overloaded operator []
85 ailego::Cube &operator[](const std::string &key) {
86 return hypercube_[key];
87 }
88
89 //! Overloaded operator []
90 ailego::Cube &operator[](std::string &&key) {
91 return hypercube_[std::move(key)];
92 }
93
94 //! Test if the element is exist
95 bool has(const std::string &key) const {
96 return hypercube_.has(key);
97 }
98
99 //! Test if the map is empty
100 bool empty(void) const {
101 return hypercube_.empty();
102 }
103
104 //! Clear the map
105 void clear(void) {
106 hypercube_.clear();
107 }
108
109 //! Erase the pair via a key
110 bool erase(const std::string &key) {
111 return hypercube_.erase(key);
112 }
113
114 //! Merge another index params
115 void merge(const IndexParams &rhs) {
116 hypercube_.merge(rhs.hypercube_);
117 }
118
119 //! Merge another index params
120 void merge(IndexParams &&rhs) {
121 hypercube_.merge(std::move(rhs.hypercube_));
122 }
123
124 //! Set the value of key in T
125 template <typename T>
126 bool insert(const std::string &key, T &&val) {
127 return hypercube_.insert<T>(key, std::forward<T>(val));
128 }
129
130 //! Set the value of key in T
131 template <typename T>
132 bool insert(std::string &&key, T &&val) {
133 return hypercube_.insert<T>(std::forward<std::string>(key),
134 std::forward<T>(val));
135 }
136
137 //! Set the value of key in T
138 template <typename T>
139 void set(const std::string &key, T &&val) {
140 hypercube_.insert_or_assign<T>(key, std::forward<T>(val));
141 }
142
143 //! Set the value of key in T
144 template <typename T>
145 void set(std::string &&key, T &&val) {
146 hypercube_.insert_or_assign<T>(std::forward<std::string>(key),
147 std::forward<T>(val));
148 }
149
150 //! Retrieve the value in boolean
151 bool get(const std::string &key, bool *out) const {
152 const ailego::Cube *cube = hypercube_.get(key);
153 if (cube) {
154 _TRYING_COMPATIBLE(cube, bool, out);
155 _TRYING_COMPATIBLE_BOOL(cube, char, out);
156 _TRYING_COMPATIBLE_BOOL(cube, unsigned char, out);
157 _TRYING_COMPATIBLE_BOOL(cube, signed char, out);
158 _TRYING_COMPATIBLE_BOOL(cube, short int, out);
159 _TRYING_COMPATIBLE_BOOL(cube, unsigned short int, out);
160 _TRYING_COMPATIBLE_BOOL(cube, int, out);
161 _TRYING_COMPATIBLE_BOOL(cube, unsigned int, out);
162 _TRYING_COMPATIBLE_BOOL(cube, long int, out);
163 _TRYING_COMPATIBLE_BOOL(cube, unsigned long int, out);
164 _TRYING_COMPATIBLE_BOOL(cube, long long int, out);
165 _TRYING_COMPATIBLE_BOOL(cube, unsigned long long int, out);
166 _TRYING_COMPATIBLE_BOOL(cube, float, out);
167 _TRYING_COMPATIBLE_BOOL(cube, double, out);
168 _TRYING_COMPATIBLE_BOOL(cube, long double, out);
169 _TRYING_CONVERT_STRING(cube, out);
170 }
171 return false;
172 }
173
174 //! Retrieve the value in 'char'
175 bool get(const std::string &key, char *out) const {
176 const ailego::Cube *cube = hypercube_.get(key);
177 if (cube) {
178 _TRYING_COMPATIBLE(cube, char, out);
179 _TRYING_COMPATIBLE(cube, unsigned char, out);
180 _TRYING_COMPATIBLE(cube, signed char, out);
181 _TRYING_COMPATIBLE(cube, bool, out);
182 _TRYING_COMPATIBLE(cube, short int, out);
183 _TRYING_COMPATIBLE(cube, unsigned short int, out);
184 _TRYING_COMPATIBLE(cube, int, out);
185 _TRYING_COMPATIBLE(cube, unsigned int, out);
186 _TRYING_COMPATIBLE(cube, long int, out);
187 _TRYING_COMPATIBLE(cube, unsigned long int, out);
188 _TRYING_COMPATIBLE(cube, long long int, out);
189 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
190 _TRYING_COMPATIBLE(cube, float, out);
191 _TRYING_COMPATIBLE(cube, double, out);
192 _TRYING_COMPATIBLE(cube, long double, out);
193 _TRYING_CONVERT_STRING(cube, out);
194 }
195 return false;
196 }
197
198 //! Retrieve the value in 'unsigned char'
199 bool get(const std::string &key, unsigned char *out) const {
200 const ailego::Cube *cube = hypercube_.get(key);
201 if (cube) {
202 _TRYING_COMPATIBLE(cube, unsigned char, out);
203 _TRYING_COMPATIBLE(cube, char, out);
204 _TRYING_COMPATIBLE(cube, signed char, out);
205 _TRYING_COMPATIBLE(cube, bool, out);
206 _TRYING_COMPATIBLE(cube, unsigned short int, out);
207 _TRYING_COMPATIBLE(cube, short int, out);
208 _TRYING_COMPATIBLE(cube, unsigned int, out);
209 _TRYING_COMPATIBLE(cube, int, out);
210 _TRYING_COMPATIBLE(cube, unsigned long int, out);
211 _TRYING_COMPATIBLE(cube, long int, out);
212 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
213 _TRYING_COMPATIBLE(cube, long long int, out);
214 _TRYING_COMPATIBLE(cube, float, out);
215 _TRYING_COMPATIBLE(cube, double, out);
216 _TRYING_COMPATIBLE(cube, long double, out);
217 _TRYING_CONVERT_STRING(cube, out);
218 }
219 return false;
220 }
221
222 //! Retrieve the value in 'signed char'
223 bool get(const std::string &key, signed char *out) const {
224 const ailego::Cube *cube = hypercube_.get(key);
225 if (cube) {
226 _TRYING_COMPATIBLE(cube, signed char, out);
227 _TRYING_COMPATIBLE(cube, char, out);
228 _TRYING_COMPATIBLE(cube, unsigned char, out);
229 _TRYING_COMPATIBLE(cube, bool, out);
230 _TRYING_COMPATIBLE(cube, short int, out);
231 _TRYING_COMPATIBLE(cube, unsigned short int, out);
232 _TRYING_COMPATIBLE(cube, int, out);
233 _TRYING_COMPATIBLE(cube, unsigned int, out);
234 _TRYING_COMPATIBLE(cube, long int, out);
235 _TRYING_COMPATIBLE(cube, unsigned long int, out);
236 _TRYING_COMPATIBLE(cube, long long int, out);
237 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
238 _TRYING_COMPATIBLE(cube, float, out);
239 _TRYING_COMPATIBLE(cube, double, out);
240 _TRYING_COMPATIBLE(cube, long double, out);
241 _TRYING_CONVERT_STRING(cube, out);
242 }
243 return false;
244 }
245
246 //! Retrieve the value in 'short int'
247 bool get(const std::string &key, short int *out) const {
248 const ailego::Cube *cube = hypercube_.get(key);
249 if (cube) {
250 _TRYING_COMPATIBLE(cube, short int, out);
251 _TRYING_COMPATIBLE(cube, unsigned short int, out);
252 _TRYING_COMPATIBLE(cube, char, out);
253 _TRYING_COMPATIBLE(cube, unsigned char, out);
254 _TRYING_COMPATIBLE(cube, signed char, out);
255 _TRYING_COMPATIBLE(cube, bool, out);
256 _TRYING_COMPATIBLE(cube, int, out);
257 _TRYING_COMPATIBLE(cube, unsigned int, out);
258 _TRYING_COMPATIBLE(cube, long int, out);
259 _TRYING_COMPATIBLE(cube, unsigned long int, out);
260 _TRYING_COMPATIBLE(cube, long long int, out);
261 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
262 _TRYING_COMPATIBLE(cube, float, out);
263 _TRYING_COMPATIBLE(cube, double, out);
264 _TRYING_COMPATIBLE(cube, long double, out);
265 _TRYING_CONVERT_STRING(cube, out);
266 }
267 return false;
268 }
269
270 //! Retrieve the value in 'unsigned short int'
271 bool get(const std::string &key, unsigned short int *out) const {
272 const ailego::Cube *cube = hypercube_.get(key);
273 if (cube) {
274 _TRYING_COMPATIBLE(cube, unsigned short int, out);
275 _TRYING_COMPATIBLE(cube, short int, out);
276 _TRYING_COMPATIBLE(cube, unsigned char, out);
277 _TRYING_COMPATIBLE(cube, char, out);
278 _TRYING_COMPATIBLE(cube, signed char, out);
279 _TRYING_COMPATIBLE(cube, bool, out);
280 _TRYING_COMPATIBLE(cube, unsigned int, out);
281 _TRYING_COMPATIBLE(cube, int, out);
282 _TRYING_COMPATIBLE(cube, unsigned long int, out);
283 _TRYING_COMPATIBLE(cube, long int, out);
284 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
285 _TRYING_COMPATIBLE(cube, long long int, out);
286 _TRYING_COMPATIBLE(cube, float, out);
287 _TRYING_COMPATIBLE(cube, double, out);
288 _TRYING_COMPATIBLE(cube, long double, out);
289 _TRYING_CONVERT_STRING(cube, out);
290 }
291 return false;
292 }
293
294 //! Retrieve the value in 'int'
295 bool get(const std::string &key, int *out) const {
296 const ailego::Cube *cube = hypercube_.get(key);
297 if (cube) {
298 _TRYING_COMPATIBLE(cube, int, out);
299 _TRYING_COMPATIBLE(cube, unsigned int, out);
300 _TRYING_COMPATIBLE(cube, short int, out);
301 _TRYING_COMPATIBLE(cube, unsigned short int, out);
302 _TRYING_COMPATIBLE(cube, char, out);
303 _TRYING_COMPATIBLE(cube, unsigned char, out);
304 _TRYING_COMPATIBLE(cube, signed char, out);
305 _TRYING_COMPATIBLE(cube, bool, out);
306 _TRYING_COMPATIBLE(cube, long int, out);
307 _TRYING_COMPATIBLE(cube, unsigned long int, out);
308 _TRYING_COMPATIBLE(cube, long long int, out);
309 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
310 _TRYING_COMPATIBLE(cube, float, out);
311 _TRYING_COMPATIBLE(cube, double, out);
312 _TRYING_COMPATIBLE(cube, long double, out);
313 _TRYING_CONVERT_STRING(cube, out);
314 }
315 return false;
316 }
317
318 //! Retrieve the value in 'unsigned int'
319 bool get(const std::string &key, unsigned int *out) const {
320 const ailego::Cube *cube = hypercube_.get(key);
321 if (cube) {
322 _TRYING_COMPATIBLE(cube, unsigned int, out);
323 _TRYING_COMPATIBLE(cube, int, out);
324 _TRYING_COMPATIBLE(cube, unsigned short int, out);
325 _TRYING_COMPATIBLE(cube, short int, out);
326 _TRYING_COMPATIBLE(cube, unsigned char, out);
327 _TRYING_COMPATIBLE(cube, char, out);
328 _TRYING_COMPATIBLE(cube, signed char, out);
329 _TRYING_COMPATIBLE(cube, bool, out);
330 _TRYING_COMPATIBLE(cube, unsigned long int, out);
331 _TRYING_COMPATIBLE(cube, long int, out);
332 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
333 _TRYING_COMPATIBLE(cube, long long int, out);
334 _TRYING_COMPATIBLE(cube, float, out);
335 _TRYING_COMPATIBLE(cube, double, out);
336 _TRYING_COMPATIBLE(cube, long double, out);
337 _TRYING_CONVERT_STRING(cube, out);
338 }
339 return false;
340 }
341
342 //! Retrieve the value in 'long int'
343 bool get(const std::string &key, long int *out) const {
344 const ailego::Cube *cube = hypercube_.get(key);
345 if (cube) {
346 _TRYING_COMPATIBLE(cube, long int, out);
347 _TRYING_COMPATIBLE(cube, unsigned long int, out);
348 _TRYING_COMPATIBLE(cube, int, out);
349 _TRYING_COMPATIBLE(cube, unsigned int, out);
350 _TRYING_COMPATIBLE(cube, short int, out);
351 _TRYING_COMPATIBLE(cube, unsigned short int, out);
352 _TRYING_COMPATIBLE(cube, char, out);
353 _TRYING_COMPATIBLE(cube, unsigned char, out);
354 _TRYING_COMPATIBLE(cube, signed char, out);
355 _TRYING_COMPATIBLE(cube, bool, out);
356 _TRYING_COMPATIBLE(cube, long long int, out);
357 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
358 _TRYING_COMPATIBLE(cube, float, out);
359 _TRYING_COMPATIBLE(cube, double, out);
360 _TRYING_COMPATIBLE(cube, long double, out);
361 _TRYING_CONVERT_STRING(cube, out);
362 }
363 return false;
364 }
365
366 //! Retrieve the value in 'unsigned long int'
367 bool get(const std::string &key, unsigned long int *out) const {
368 const ailego::Cube *cube = hypercube_.get(key);
369 if (cube) {
370 _TRYING_COMPATIBLE(cube, unsigned long int, out);
371 _TRYING_COMPATIBLE(cube, long int, out);
372 _TRYING_COMPATIBLE(cube, unsigned int, out);
373 _TRYING_COMPATIBLE(cube, int, out);
374 _TRYING_COMPATIBLE(cube, unsigned short int, out);
375 _TRYING_COMPATIBLE(cube, short int, out);
376 _TRYING_COMPATIBLE(cube, unsigned char, out);
377 _TRYING_COMPATIBLE(cube, char, out);
378 _TRYING_COMPATIBLE(cube, signed char, out);
379 _TRYING_COMPATIBLE(cube, bool, out);
380 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
381 _TRYING_COMPATIBLE(cube, long long int, out);
382 _TRYING_COMPATIBLE(cube, float, out);
383 _TRYING_COMPATIBLE(cube, double, out);
384 _TRYING_COMPATIBLE(cube, long double, out);
385 _TRYING_CONVERT_STRING(cube, out);
386 }
387 return false;
388 }
389
390 //! Retrieve the value in 'long long int'
391 bool get(const std::string &key, long long int *out) const {
392 const ailego::Cube *cube = hypercube_.get(key);
393 if (cube) {
394 _TRYING_COMPATIBLE(cube, long long int, out);
395 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
396 _TRYING_COMPATIBLE(cube, long int, out);
397 _TRYING_COMPATIBLE(cube, unsigned long int, out);
398 _TRYING_COMPATIBLE(cube, int, out);
399 _TRYING_COMPATIBLE(cube, unsigned int, out);
400 _TRYING_COMPATIBLE(cube, short int, out);
401 _TRYING_COMPATIBLE(cube, unsigned short int, out);
402 _TRYING_COMPATIBLE(cube, char, out);
403 _TRYING_COMPATIBLE(cube, unsigned char, out);
404 _TRYING_COMPATIBLE(cube, signed char, out);
405 _TRYING_COMPATIBLE(cube, bool, out);
406 _TRYING_COMPATIBLE(cube, float, out);
407 _TRYING_COMPATIBLE(cube, double, out);
408 _TRYING_COMPATIBLE(cube, long double, out);
409 _TRYING_CONVERT_STRING(cube, out);
410 }
411 return false;
412 }
413
414 //! Retrieve the value in 'unsigned long long int'
415 bool get(const std::string &key, unsigned long long int *out) const {
416 const ailego::Cube *cube = hypercube_.get(key);
417 if (cube) {
418 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
419 _TRYING_COMPATIBLE(cube, long long int, out);
420 _TRYING_COMPATIBLE(cube, unsigned long int, out);
421 _TRYING_COMPATIBLE(cube, long int, out);
422 _TRYING_COMPATIBLE(cube, unsigned int, out);
423 _TRYING_COMPATIBLE(cube, int, out);
424 _TRYING_COMPATIBLE(cube, unsigned short int, out);
425 _TRYING_COMPATIBLE(cube, short int, out);
426 _TRYING_COMPATIBLE(cube, unsigned char, out);
427 _TRYING_COMPATIBLE(cube, char, out);
428 _TRYING_COMPATIBLE(cube, signed char, out);
429 _TRYING_COMPATIBLE(cube, bool, out);
430 _TRYING_COMPATIBLE(cube, float, out);
431 _TRYING_COMPATIBLE(cube, double, out);
432 _TRYING_COMPATIBLE(cube, long double, out);
433 _TRYING_CONVERT_STRING(cube, out);
434 }
435 return false;
436 }
437
438 //! Retrieve the value in 'float'
439 bool get(const std::string &key, float *out) const {
440 const ailego::Cube *cube = hypercube_.get(key);
441 if (cube) {
442 _TRYING_COMPATIBLE(cube, float, out);
443 _TRYING_COMPATIBLE(cube, double, out);
444 _TRYING_COMPATIBLE(cube, long double, out);
445 _TRYING_COMPATIBLE(cube, long long int, out);
446 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
447 _TRYING_COMPATIBLE(cube, long int, out);
448 _TRYING_COMPATIBLE(cube, unsigned long int, out);
449 _TRYING_COMPATIBLE(cube, int, out);
450 _TRYING_COMPATIBLE(cube, unsigned int, out);
451 _TRYING_COMPATIBLE(cube, short int, out);
452 _TRYING_COMPATIBLE(cube, unsigned short int, out);
453 _TRYING_COMPATIBLE(cube, char, out);
454 _TRYING_COMPATIBLE(cube, unsigned char, out);
455 _TRYING_COMPATIBLE(cube, signed char, out);
456 _TRYING_COMPATIBLE(cube, bool, out);
457 _TRYING_CONVERT_STRING(cube, out);
458 }
459 return false;
460 }
461
462 //! Retrieve the value in 'double'
463 bool get(const std::string &key, double *out) const {
464 const ailego::Cube *cube = hypercube_.get(key);
465 if (cube) {
466 _TRYING_COMPATIBLE(cube, double, out);
467 _TRYING_COMPATIBLE(cube, float, out);
468 _TRYING_COMPATIBLE(cube, long double, out);
469 _TRYING_COMPATIBLE(cube, long long int, out);
470 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
471 _TRYING_COMPATIBLE(cube, long int, out);
472 _TRYING_COMPATIBLE(cube, unsigned long int, out);
473 _TRYING_COMPATIBLE(cube, int, out);
474 _TRYING_COMPATIBLE(cube, unsigned int, out);
475 _TRYING_COMPATIBLE(cube, short int, out);
476 _TRYING_COMPATIBLE(cube, unsigned short int, out);
477 _TRYING_COMPATIBLE(cube, char, out);
478 _TRYING_COMPATIBLE(cube, unsigned char, out);
479 _TRYING_COMPATIBLE(cube, signed char, out);
480 _TRYING_COMPATIBLE(cube, bool, out);
481 _TRYING_CONVERT_STRING(cube, out);
482 }
483 return false;
484 }
485
486 //! Retrieve the value in 'long double'
487 bool get(const std::string &key, long double *out) const {
488 const ailego::Cube *cube = hypercube_.get(key);
489 if (cube) {
490 _TRYING_COMPATIBLE(cube, long double, out);
491 _TRYING_COMPATIBLE(cube, double, out);
492 _TRYING_COMPATIBLE(cube, float, out);
493 _TRYING_COMPATIBLE(cube, long long int, out);
494 _TRYING_COMPATIBLE(cube, unsigned long long int, out);
495 _TRYING_COMPATIBLE(cube, long int, out);
496 _TRYING_COMPATIBLE(cube, unsigned long int, out);
497 _TRYING_COMPATIBLE(cube, int, out);
498 _TRYING_COMPATIBLE(cube, unsigned int, out);
499 _TRYING_COMPATIBLE(cube, short int, out);
500 _TRYING_COMPATIBLE(cube, unsigned short int, out);
501 _TRYING_COMPATIBLE(cube, char, out);
502 _TRYING_COMPATIBLE(cube, unsigned char, out);
503 _TRYING_COMPATIBLE(cube, signed char, out);
504 _TRYING_COMPATIBLE(cube, bool, out);
505 _TRYING_CONVERT_STRING(cube, out);
506 }
507 return false;
508 }
509
510 //! Retrieve the value in string
511 bool get(const std::string &key, std::string *out) const {
512 const ailego::Cube *cube = hypercube_.get(key);
513 if (cube) {
514 _TRYING_COMPATIBLE(cube, std::string, out);
515 _TRYING_COMPATIBLE_STRING(cube, bool, out);
516 _TRYING_COMPATIBLE_STRING(cube, char, out);
517 _TRYING_COMPATIBLE_STRING(cube, unsigned char, out);
518 _TRYING_COMPATIBLE_STRING(cube, signed char, out);
519 _TRYING_COMPATIBLE_STRING(cube, short int, out);
520 _TRYING_COMPATIBLE_STRING(cube, unsigned short int, out);
521 _TRYING_COMPATIBLE_STRING(cube, int, out);
522 _TRYING_COMPATIBLE_STRING(cube, unsigned int, out);
523 _TRYING_COMPATIBLE_STRING(cube, long int, out);
524 _TRYING_COMPATIBLE_STRING(cube, unsigned long int, out);
525 _TRYING_COMPATIBLE_STRING(cube, long long int, out);
526 _TRYING_COMPATIBLE_STRING(cube, unsigned long long int, out);
527 _TRYING_COMPATIBLE_STRING(cube, float, out);
528 _TRYING_COMPATIBLE_STRING(cube, double, out);
529 _TRYING_COMPATIBLE_STRING(cube, long double, out);
530 }
531 return false;
532 }
533
534 //! Retrieve the value in T
535 template <typename T>
536 bool get(const std::string &key, T *out) const {
537 const ailego::Cube *cube = hypercube_.get(key);
538 if (cube) {
539 _TRYING_COMPATIBLE(cube, T, out);
540 }
541 return false;
542 }
543
544 //! Retrieve the value in boolean
545 bool get_as_bool(const std::string &key) const {
546 bool result = false;
547 this->get(key, &result);
548 return result;
549 }
550
551 //! Retrieve the value in int8
552 int8_t get_as_int8(const std::string &key) const {
553 int8_t result = 0;
554 this->get(key, &result);
555 return result;
556 }
557
558 //! Retrieve the value in int16
559 int16_t get_as_int16(const std::string &key) const {
560 int16_t result = 0;
561 this->get(key, &result);
562 return result;
563 }
564
565 //! Retrieve the value in int32
566 int32_t get_as_int32(const std::string &key) const {
567 int32_t result = 0;
568 this->get(key, &result);
569 return result;
570 }
571
572 //! Retrieve the value in int64
573 int64_t get_as_int64(const std::string &key) const {
574 int64_t result = 0;
575 this->get(key, &result);
576 return result;
577 }
578
579 //! Retrieve the value in uint8
580 uint8_t get_as_uint8(const std::string &key) const {
581 uint8_t result = 0;
582 this->get(key, &result);
583 return result;
584 }
585
586 //! Retrieve the value in uint16
587 uint16_t get_as_uint16(const std::string &key) const {
588 uint16_t result = 0;
589 this->get(key, &result);
590 return result;
591 }
592
593 //! Retrieve the value in uint32
594 uint32_t get_as_uint32(const std::string &key) const {
595 uint32_t result = 0;
596 this->get(key, &result);
597 return result;
598 }
599
600 //! Retrieve the value in uint64
601 uint64_t get_as_uint64(const std::string &key) const {
602 uint64_t result = 0;
603 this->get(key, &result);
604 return result;
605 }
606
607 //! Retrieve the value in float
608 float get_as_float(const std::string &key) const {
609 float result = 0.0f;
610 this->get(key, &result);
611 return result;
612 }
613
614 //! Retrieve the value in double
615 double get_as_double(const std::string &key) const {
616 double result = 0.0f;
617 this->get(key, &result);
618 return result;
619 }
620
621 //! Retrieve the value in string
622 std::string get_as_string(const std::string &key) const {
623 std::string result;
624 this->get(key, &result);
625 return result;
626 }
627
628 //! Retrieve the debug string
629 std::string debug_string(void) const {
630 std::string str;
631 SerializeToBuffer(*this, &str);
632 return str;
633 }
634
635 //! Retrieve the map of parameters
636 const ailego::Hypercube &hypercube(void) const {
637 return hypercube_;
638 }
639
640 //! Retrieve the map of parameters
641 ailego::Hypercube *mutable_hypercube(void) {
642 return &hypercube_;
643 }
644
645 //! Parse parameters from buffer (Json format)
646 static bool ParseFromBuffer(const std::string &buf, IndexParams *params);
647
648 //! Parse parameters from OS environment
649 static void ParseFromEnvironment(IndexParams *params);
650
651 //! Serialize parameters into buffer
652 static void SerializeToBuffer(const IndexParams &params, std::string *buf);
653
654 protected:
655 //! Convert string type to another type
656 template <typename T>
657 static auto StringCast(const std::string &str) ->
658 typename std::enable_if<std::is_same<T, float>::value, T>::type {
659 return std::strtof(str.c_str(), nullptr);
660 }
661
662 //! Convert string type to another type
663 template <typename T>
664 static auto StringCast(const std::string &str) ->
665 typename std::enable_if<std::is_same<T, double>::value, T>::type {
666 return std::strtod(str.c_str(), nullptr);
667 }
668
669 //! Convert string type to another type
670 template <typename T>
671 static auto StringCast(const std::string &str) ->
672 typename std::enable_if<std::is_same<T, long double>::value, T>::type {
673 return std::strtold(str.c_str(), nullptr);
674 }
675
676 //! Convert string type to another type
677 template <typename T>
678 static auto StringCast(const std::string &str) ->
679 typename std::enable_if<std::is_same<T, char>::value, T>::type {
680 return static_cast<char>(std::strtol(str.c_str(), nullptr, 0));
681 }
682
683 //! Convert string type to another type
684 template <typename T>
685 static auto StringCast(const std::string &str) ->
686 typename std::enable_if<std::is_same<T, signed char>::value, T>::type {
687 return static_cast<signed char>(std::strtol(str.c_str(), nullptr, 0));
688 }
689
690 //! Convert string type to another type
691 template <typename T>
692 static auto StringCast(const std::string &str) ->
693 typename std::enable_if<std::is_same<T, unsigned char>::value, T>::type {
694 return static_cast<unsigned char>(std::strtoul(str.c_str(), nullptr, 0));
695 }
696
697 //! Convert string type to another type
698 template <typename T>
699 static auto StringCast(const std::string &str) ->
700 typename std::enable_if<std::is_same<T, short int>::value, T>::type {
701 return static_cast<short int>(std::strtol(str.c_str(), nullptr, 0));
702 }
703
704 //! Convert string type to another type
705 template <typename T>
706 static auto StringCast(const std::string &str) ->
707 typename std::enable_if<std::is_same<T, int>::value, T>::type {
708 return static_cast<int>(std::strtol(str.c_str(), nullptr, 0));
709 }
710
711 //! Convert string type to another type
712 template <typename T>
713 static auto StringCast(const std::string &str) ->
714 typename std::enable_if<std::is_same<T, long int>::value, T>::type {
715 return static_cast<long int>(std::strtol(str.c_str(), nullptr, 0));
716 }
717
718 //! Convert string type to another type
719 template <typename T>
720 static auto StringCast(const std::string &str) ->
721 typename std::enable_if<std::is_same<T, long long int>::value, T>::type {
722 return static_cast<long long int>(std::strtoll(str.c_str(), nullptr, 0));
723 }
724
725 //! Convert string type to another type
726 template <typename T>
727 static auto StringCast(const std::string &str) ->
728 typename std::enable_if<std::is_same<T, unsigned short int>::value,
729 T>::type {
730 return static_cast<unsigned short int>(
731 std::strtoul(str.c_str(), nullptr, 0));
732 }
733
734 //! Convert string type to another type
735 template <typename T>
736 static auto StringCast(const std::string &str) ->
737 typename std::enable_if<std::is_same<T, unsigned int>::value, T>::type {
738 return static_cast<unsigned int>(std::strtoul(str.c_str(), nullptr, 0));
739 }
740
741 //! Convert string type to another type
742 template <typename T>
743 static auto StringCast(const std::string &str) ->
744 typename std::enable_if<std::is_same<T, unsigned long int>::value,
745 T>::type {
746 return static_cast<unsigned long int>(
747 std::strtoul(str.c_str(), nullptr, 0));
748 }
749
750 //! Convert string type to another type
751 template <typename T>
752 static auto StringCast(const std::string &str) ->
753 typename std::enable_if<std::is_same<T, unsigned long long int>::value,
754 T>::type {
755 return static_cast<unsigned long long int>(
756 std::strtoull(str.c_str(), nullptr, 0));
757 }
758
759 //! Convert string type to another type
760 template <typename T>
761 static auto StringCast(const std::string &str) ->
762 typename std::enable_if<std::is_same<T, bool>::value, T>::type {
763 if (str.empty()) {
764 return false;
765 }
766 char c = str[0];
767 if (c == 'Y' || c == 'T' || c == 'y' || c == 't') {
768 return true;
769 }
770 return !!std::strtof(str.c_str(), nullptr);
771 }
772
773 private:
774 ailego::Hypercube hypercube_;
775};
776
777#undef _TRYING_COMPATIBLE
778#undef _TRYING_COMPATIBLE
779#undef _TRYING_COMPATIBLE_BOOL
780#undef _TRYING_COMPATIBLE_STRING
781#undef _TRYING_CONVERT_STRING
782
783} // namespace aitheta2
784
785#endif // __AITHETA2_INDEX_PARAMS_H__
786