Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_value 5 #define tools_value 6 7 #include "array" 8 #include "typedefs" 9 #include "num2s" 10 #include "b2s" 11 12 #ifdef TOOLS_MEM 13 #include "mem" 14 #endif 15 16 namespace tools { 17 18 class value { 19 static const std::string& s_class() { 20 static const std::string s_v("tools::value"); 21 return s_v; 22 } 23 public: 24 enum e_type { 25 NONE = 0, 26 // integers : 27 //UNSIGNED_CHAR = 10, 28 //CHAR = 11, 29 UNSIGNED_SHORT = 12, 30 SHORT = 13, 31 UNSIGNED_INT = 14, 32 INT = 15, 33 UNSIGNED_INT64 = 16, 34 INT64 = 17, 35 // reals : 36 FLOAT = 30, 37 DOUBLE = 31, 38 // else : 39 BOOL = 50, 40 STRING = 51, 41 // pointers : 42 VOID_STAR = 100, 43 DOUBLE_STAR = 101, 44 FLOAT_STAR = 102, 45 INT_STAR = 103, 46 47 // multidimensional vectors (1000+base type) : 48 //ARRAY_UNSIGNED_CHAR = 1010, 49 //ARRAY_CHAR = 1011, 50 ARRAY_UNSIGNED_SHORT = 1012, 51 ARRAY_SHORT = 1013, 52 ARRAY_UNSIGNED_INT = 1014, 53 ARRAY_INT = 1015, 54 ARRAY_UNSIGNED_INT64 = 1016, 55 ARRAY_INT64 = 1017, 56 ARRAY_FLOAT = 1030, 57 ARRAY_DOUBLE = 1031, 58 ARRAY_BOOL = 1050, 59 ARRAY_STRING = 1051 60 }; 61 62 /* 63 static e_type type(unsigned short) {return UNSIGNED_SHORT;} 64 static e_type type(short) {return SHORT;} 65 static e_type type(unsigned int) {return UNSIGNED_INT;} 66 static e_type type(int) {return INT;} 67 static e_type type(uint64) {return UNSIGNED_INT64;} 68 static e_type type(int64) {return INT64;} 69 static e_type type(float) {return FLOAT;} 70 static e_type type(double) {return DOUBLE;} 71 static e_type type(bool) {return BOOL;} 72 static e_type type(void*) {return VOID_STAR;} 73 static e_type type(double*) {return DOUBLE_STAR;} 74 static e_type type(float*) {return FLOAT_STAR;} 75 static e_type type(int*) {return INT_STAR;} 76 //static e_type type(const std::string&) {return STRING;} 77 */ 78 public: 79 value():m_label(0),m_itag(0){ 80 #ifdef TOOLS_MEM 81 mem::increment(s_class().c_str()); 82 #endif 83 m_type = NONE; 84 u.m_unsigned_int64 = 0; 85 } 86 87 value(bool a_value):m_label(0),m_itag(0) { 88 #ifdef TOOLS_MEM 89 mem::increment(s_class().c_str()); 90 #endif 91 m_type = BOOL; 92 u.m_bool = a_value; 93 } 94 // value(char a_value):m_label(0),m_itag(0) { 95 //#ifdef TOOLS_MEM 96 // mem::increment(s_class().c_str()); 97 //#endif 98 // m_type = CHAR; 99 // u.m_char = a_value; 100 // } 101 value(short a_value):m_label(0),m_itag(0) { 102 #ifdef TOOLS_MEM 103 mem::increment(s_class().c_str()); 104 #endif 105 m_type = SHORT; 106 u.m_short = a_value; 107 } 108 value(int a_value):m_label(0),m_itag(0) { 109 #ifdef TOOLS_MEM 110 mem::increment(s_class().c_str()); 111 #endif 112 m_type = INT; 113 u.m_int = a_value; 114 } 115 value(int64 a_value):m_label(0),m_itag(0) { 116 #ifdef TOOLS_MEM 117 mem::increment(s_class().c_str()); 118 #endif 119 m_type = INT64; 120 u.m_int64 = a_value; 121 } 122 value(uint64 a_value):m_label(0),m_itag(0) { 123 #ifdef TOOLS_MEM 124 mem::increment(s_class().c_str()); 125 #endif 126 m_type = UNSIGNED_INT64; 127 u.m_unsigned_int64 = a_value; 128 } 129 value(float a_value):m_label(0),m_itag(0) { 130 #ifdef TOOLS_MEM 131 mem::increment(s_class().c_str()); 132 #endif 133 m_type = FLOAT; 134 u.m_float = a_value; 135 } 136 value(double a_value):m_label(0),m_itag(0) { 137 #ifdef TOOLS_MEM 138 mem::increment(s_class().c_str()); 139 #endif 140 m_type = DOUBLE; 141 u.m_double = a_value; 142 } 143 144 // value(unsigned char a_value):m_label(0),m_itag(0) { 145 //#ifdef TOOLS_MEM 146 // mem::increment(s_class().c_str()); 147 //#endif 148 // m_type = UNSIGNED_CHAR; 149 // u.m_unsigned_char = a_value; 150 // } 151 value(unsigned short a_value):m_label(0),m_itag(0) { 152 #ifdef TOOLS_MEM 153 mem::increment(s_class().c_str()); 154 #endif 155 m_type = UNSIGNED_SHORT; 156 u.m_unsigned_short = a_value; 157 } 158 value(unsigned int a_value):m_label(0),m_itag(0) { 159 #ifdef TOOLS_MEM 160 mem::increment(s_class().c_str()); 161 #endif 162 m_type = UNSIGNED_INT; 163 u.m_unsigned_int = a_value; 164 } 165 value(void* a_value):m_label(0),m_itag(0) { 166 #ifdef TOOLS_MEM 167 mem::increment(s_class().c_str()); 168 #endif 169 m_type = VOID_STAR; 170 u.m_void_star = a_value; 171 } 172 value(double* a_value):m_label(0),m_itag(0) { 173 #ifdef TOOLS_MEM 174 mem::increment(s_class().c_str()); 175 #endif 176 m_type = DOUBLE_STAR; 177 u.m_double_star = a_value; 178 } 179 value(float* a_value):m_label(0),m_itag(0) { 180 #ifdef TOOLS_MEM 181 mem::increment(s_class().c_str()); 182 #endif 183 m_type = FLOAT_STAR; 184 u.m_float_star = a_value; 185 } 186 value(int* a_value):m_label(0),m_itag(0) { 187 #ifdef TOOLS_MEM 188 mem::increment(s_class().c_str()); 189 #endif 190 m_type = INT_STAR; 191 u.m_int_star = a_value; 192 } 193 194 value(const char* a_value):m_label(0),m_itag(0) { 195 #ifdef TOOLS_MEM 196 mem::increment(s_class().c_str()); 197 #endif 198 m_type = STRING; 199 u.m_string = new std::string(a_value?a_value:""); 200 } 201 value(const std::string& a_value):m_label(0),m_itag(0) { 202 #ifdef TOOLS_MEM 203 mem::increment(s_class().c_str()); 204 #endif 205 m_type = STRING; 206 u.m_string = new std::string(a_value); 207 } 208 209 #ifdef TOOLS_MEM 210 #define TOOLS_VALUE_CSTOR(a_what,a_m_what,a_type) \ 211 value(const std::vector<a_what>& a_v):m_label(0),m_itag(0){\ 212 mem::increment(s_class().c_str());\ 213 m_type = a_type;\ 214 std::vector<unsigned int> is(1);\ 215 is[0] = (unsigned int)a_v.size();\ 216 u.a_m_what = new array<a_what>(is);\ 217 u.a_m_what->fill(a_v);\ 218 } 219 #else 220 #define TOOLS_VALUE_CSTOR(a_what,a_m_what,a_type) \ 221 value(const std::vector<a_what>& a_v):m_label(0),m_itag(0){\ 222 m_type = a_type;\ 223 std::vector<unsigned int> is(1);\ 224 is[0] = (unsigned int)a_v.size();\ 225 u.a_m_what = new array<a_what>(is);\ 226 u.a_m_what->fill(a_v);\ 227 } 228 #endif 229 230 //TOOLS_VALUE_CSTOR(unsigned char,m_array_unsigned_char,ARRAY_UNSIGNED_CHAR) 231 //TOOLS_VALUE_CSTOR(char,m_array_char,ARRAY_CHAR) 232 TOOLS_VALUE_CSTOR(unsigned short,m_array_unsigned_short,ARRAY_UNSIGNED_SHORT) 233 TOOLS_VALUE_CSTOR(short,m_array_short,ARRAY_SHORT) 234 TOOLS_VALUE_CSTOR(unsigned int,m_array_unsigned_int,ARRAY_UNSIGNED_INT) 235 TOOLS_VALUE_CSTOR(int,m_array_int,ARRAY_INT) 236 TOOLS_VALUE_CSTOR(uint64,m_array_unsigned_int64,ARRAY_UNSIGNED_INT64) 237 TOOLS_VALUE_CSTOR(int64,m_array_int64,ARRAY_INT64) 238 TOOLS_VALUE_CSTOR(float,m_array_float,ARRAY_FLOAT) 239 TOOLS_VALUE_CSTOR(double,m_array_double,ARRAY_DOUBLE) 240 TOOLS_VALUE_CSTOR(bool,m_array_bool,ARRAY_BOOL) 241 TOOLS_VALUE_CSTOR(std::string,m_array_string,ARRAY_STRING) 242 243 #undef TOOLS_VALUE_CSTOR 244 245 #ifdef TOOLS_MEM 246 #define TOOLS_VALUE_CSTORA(a_what,a_m_what,a_type) \ 247 value(const array<a_what>& a_a):m_label(0),m_itag(0){\ 248 mem::increment(s_class().c_str());\ 249 m_type = a_type;\ 250 u.a_m_what = new array<a_what>(a_a);\ 251 } 252 #else 253 #define TOOLS_VALUE_CSTORA(a_what,a_m_what,a_type) \ 254 value(const array<a_what>& a_a):m_label(0),m_itag(0){\ 255 m_type = a_type;\ 256 u.a_m_what = new array<a_what>(a_a);\ 257 } 258 #endif 259 260 //TOOLS_VALUE_CSTORA(unsigned char,m_array_unsigned_char,ARRAY_UNSIGNED_CHAR) 261 //TOOLS_VALUE_CSTORA(char,m_array_char,ARRAY_CHAR) 262 TOOLS_VALUE_CSTORA(unsigned short,m_array_unsigned_short,ARRAY_UNSIGNED_SHORT) 263 TOOLS_VALUE_CSTORA(short,m_array_short,ARRAY_SHORT) 264 TOOLS_VALUE_CSTORA(unsigned int,m_array_unsigned_int,ARRAY_UNSIGNED_INT) 265 TOOLS_VALUE_CSTORA(int,m_array_int,ARRAY_INT) 266 TOOLS_VALUE_CSTORA(uint64,m_array_unsigned_int64,ARRAY_UNSIGNED_INT64) 267 TOOLS_VALUE_CSTORA(int64,m_array_int64,ARRAY_INT64) 268 TOOLS_VALUE_CSTORA(float,m_array_float,ARRAY_FLOAT) 269 TOOLS_VALUE_CSTORA(double,m_array_double,ARRAY_DOUBLE) 270 TOOLS_VALUE_CSTORA(bool,m_array_bool,ARRAY_BOOL) 271 TOOLS_VALUE_CSTORA(std::string,m_array_string,ARRAY_STRING) 272 273 #undef TOOLS_VALUE_CSTORA 274 275 virtual ~value() { 276 delete m_label; 277 reset(); 278 #ifdef TOOLS_MEM 279 mem::decrement(s_class().c_str()); 280 #endif 281 } 282 public: 283 value(const value& a_from):m_label(0),m_itag(a_from.m_itag){ 284 #ifdef TOOLS_MEM 285 mem::increment(s_class().c_str()); 286 #endif 287 288 if(a_from.m_label) m_label = new std::string(*a_from.m_label); 289 m_type = a_from.m_type; 290 291 if(a_from.m_type==STRING) { 292 u.m_string = new std::string(*a_from.u.m_string); 293 294 //} else if(a_from.m_type==ARRAY_UNSIGNED_CHAR) { 295 // u.m_array_unsigned_char = 296 // new array<unsigned char>(*a_from.u.m_array_unsigned_char); 297 298 //} else if(a_from.m_type==ARRAY_CHAR) { 299 // u.m_array_char = new array<char>(*a_from.u.m_array_char); 300 301 } else if(a_from.m_type==ARRAY_UNSIGNED_SHORT) { 302 u.m_array_unsigned_short = 303 new array<unsigned short>(*a_from.u.m_array_unsigned_short); 304 305 } else if(a_from.m_type==ARRAY_SHORT) { 306 u.m_array_short = new array<short>(*a_from.u.m_array_short); 307 308 } else if(a_from.m_type==ARRAY_UNSIGNED_INT) { 309 u.m_array_unsigned_int = new array<unsigned int>(*a_from.u.m_array_unsigned_int); 310 311 } else if(a_from.m_type==ARRAY_INT) { 312 u.m_array_int = new array<int>(*a_from.u.m_array_int); 313 314 } else if(a_from.m_type==ARRAY_UNSIGNED_INT64) { 315 u.m_array_unsigned_int64 = new array<uint64>(*a_from.u.m_array_unsigned_int64); 316 317 } else if(a_from.m_type==ARRAY_INT64) { 318 u.m_array_int64 = new array<int64>(*a_from.u.m_array_int64); 319 320 } else if(a_from.m_type==ARRAY_FLOAT) { 321 u.m_array_float = new array<float>(*a_from.u.m_array_float); 322 323 } else if(a_from.m_type==ARRAY_DOUBLE) { 324 u.m_array_double = new array<double>(*a_from.u.m_array_double); 325 326 } else if(a_from.m_type==ARRAY_BOOL) { 327 u.m_array_bool = new array<bool>(*a_from.u.m_array_bool); 328 329 } else if(a_from.m_type==ARRAY_STRING) { 330 u.m_array_string = new array<std::string>(*a_from.u.m_array_string); 331 332 } else { 333 u = a_from.u; 334 } 335 } 336 337 value& operator=(const value& a_from) { 338 if(&a_from==this) return *this; 339 340 delete m_label;m_label = 0; 341 if(a_from.m_label) m_label = new std::string(*a_from.m_label); 342 343 m_itag = a_from.m_itag; 344 345 reset(); 346 347 m_type = a_from.m_type; 348 349 if(a_from.m_type==STRING) { 350 u.m_string = new std::string(*a_from.u.m_string); 351 352 //} else if(a_from.m_type==ARRAY_UNSIGNED_CHAR) { 353 // u.m_array_unsigned_char = 354 // new array<unsigned char>(*a_from.u.m_array_unsigned_char); 355 356 //} else if(a_from.m_type==ARRAY_CHAR) { 357 // u.m_array_char = new array<char>(*a_from.u.m_array_char); 358 359 } else if(a_from.m_type==ARRAY_UNSIGNED_SHORT) { 360 u.m_array_unsigned_short = new array<unsigned short>(*a_from.u.m_array_unsigned_short); 361 362 } else if(a_from.m_type==ARRAY_SHORT) { 363 u.m_array_short = new array<short>(*a_from.u.m_array_short); 364 365 } else if(a_from.m_type==ARRAY_UNSIGNED_INT) { 366 u.m_array_unsigned_int = new array<unsigned int>(*a_from.u.m_array_unsigned_int); 367 368 } else if(a_from.m_type==ARRAY_INT) { 369 u.m_array_int = new array<int>(*a_from.u.m_array_int); 370 371 } else if(a_from.m_type==ARRAY_UNSIGNED_INT64) { 372 u.m_array_unsigned_int64 = new array<uint64>(*a_from.u.m_array_unsigned_int64); 373 374 } else if(a_from.m_type==ARRAY_INT64) { 375 u.m_array_int64 = new array<int64>(*a_from.u.m_array_int64); 376 377 } else if(a_from.m_type==ARRAY_FLOAT) { 378 u.m_array_float = new array<float>(*a_from.u.m_array_float); 379 380 } else if(a_from.m_type==ARRAY_DOUBLE) { 381 u.m_array_double = new array<double>(*a_from.u.m_array_double); 382 383 } else if(a_from.m_type==ARRAY_BOOL) { 384 u.m_array_bool = new array<bool>(*a_from.u.m_array_bool); 385 386 } else if(a_from.m_type==ARRAY_STRING) { 387 u.m_array_string = new array<std::string>(*a_from.u.m_array_string); 388 389 } else { 390 u = a_from.u; 391 } 392 393 return *this; 394 } 395 /* for tests/symbolic.cpp. 396 public: 397 value operator*(const value& a_from) const { 398 value tmp(*this); 399 std::string serr; 400 multiply(tmp,a_from,serr); 401 return tmp; 402 } 403 value operator+(const value& a_from) const { 404 value tmp(*this); 405 std::string serr; 406 add(tmp,a_from,serr); 407 return tmp; 408 } 409 value operator-(const value& a_from) const { 410 value tmp(*this); 411 std::string serr; 412 subtract(tmp,a_from,serr); 413 return tmp; 414 } 415 value operator+=(const value& a_from) { 416 value _s = operator+(a_from); 417 *this = _s; 418 return _s; 419 } 420 value operator-=(const value& a_from) { 421 value _s = operator-(a_from); 422 *this = _s; 423 return _s; 424 } 425 */ 426 private: 427 static const std::string& s_empty() { 428 static const std::string s_v(""); 429 return s_v; 430 } 431 public: 432 void set_itag(unsigned int a_itag) {m_itag = a_itag;} //gopaw 433 unsigned int itag() const {return m_itag;} //gopaw 434 void set_label(const std::string& a_s) { 435 delete m_label; 436 m_label = new std::string(a_s); 437 } 438 const std::string& label() const {return m_label?(*m_label):s_empty();} 439 440 e_type type() const {return m_type;} 441 void set_type(e_type a_type) { 442 reset(); 443 m_type = a_type; 444 switch(a_type) { 445 case NONE: u.m_unsigned_int64 = 0;break; 446 //case UNSIGNED_CHAR: u.m_unsigned_char = 0;break; 447 //case CHAR: u.m_char = 0;break; 448 case UNSIGNED_SHORT: u.m_unsigned_short = 0;break; 449 case SHORT: u.m_short = 0;break; 450 case UNSIGNED_INT: u.m_unsigned_int = 0;break; 451 case INT: u.m_int = 0;break; 452 case UNSIGNED_INT64: u.m_unsigned_int64 =0;break; 453 case INT64: u.m_int64 = 0;break; 454 case FLOAT: u.m_float = 0;break; 455 case DOUBLE: u.m_double = 0;break; 456 case BOOL: u.m_bool = false;break; 457 case VOID_STAR: u.m_void_star = 0;break; 458 case DOUBLE_STAR: u.m_double_star = 0;break; 459 case FLOAT_STAR: u.m_float_star = 0;break; 460 case INT_STAR: u.m_int_star = 0;break; 461 case STRING: u.m_string = new std::string("");break; 462 //case ARRAY_UNSIGNED_CHAR: 463 // u.m_array_unsigned_char = new array<unsigned char>();break; 464 //case ARRAY_CHAR:u.m_array_char = new array<char>();break; 465 466 case ARRAY_UNSIGNED_SHORT: 467 u.m_array_unsigned_short = new array<unsigned short>();break; 468 case ARRAY_SHORT:u.m_array_short = new array<short>();break; 469 470 case ARRAY_UNSIGNED_INT: 471 u.m_array_unsigned_int = new array<unsigned int>();break; 472 case ARRAY_INT:u.m_array_int = new array<int>();break; 473 case ARRAY_UNSIGNED_INT64: 474 u.m_array_unsigned_int64 = new array<uint64>();break; 475 case ARRAY_INT64:u.m_array_int64 = new array<int64>();break; 476 477 case ARRAY_FLOAT:u.m_array_float = new array<float>();break; 478 case ARRAY_DOUBLE:u.m_array_double = new array<double>();break; 479 case ARRAY_BOOL:u.m_array_bool = new array<bool>();break; 480 case ARRAY_STRING:u.m_array_string = new array<std::string>();break; 481 } 482 } 483 484 void set_none() { 485 reset(); 486 m_type = NONE; 487 u.m_unsigned_int64 = 0; 488 } 489 //void set(char a_value) { 490 // reset(); 491 // m_type = CHAR; 492 // u.m_char = a_value; 493 //} 494 //void set(unsigned char a_value) { 495 // reset(); 496 // m_type = UNSIGNED_CHAR; 497 // u.m_unsigned_char = a_value; 498 //} 499 void set(short a_value) { 500 reset(); 501 m_type = SHORT; 502 u.m_short = a_value; 503 } 504 void set(unsigned short a_value) { 505 reset(); 506 m_type = UNSIGNED_SHORT; 507 u.m_unsigned_short = a_value; 508 } 509 void set(int a_value) { 510 reset(); 511 m_type = INT; 512 u.m_int = a_value; 513 } 514 void set(unsigned int a_value) { 515 reset(); 516 m_type = UNSIGNED_INT; 517 u.m_unsigned_int = a_value; 518 } 519 void set(int64 a_value) { 520 reset(); 521 m_type = INT64; 522 u.m_int64 = a_value; 523 } 524 void set(uint64 a_value) { 525 reset(); 526 m_type = UNSIGNED_INT64; 527 u.m_unsigned_int64 = a_value; 528 } 529 void set(float a_value) { 530 reset(); 531 m_type = FLOAT; 532 u.m_float = a_value; 533 } 534 void set(double a_value) { 535 reset(); 536 m_type = DOUBLE; 537 u.m_double = a_value; 538 } 539 void set(bool a_value) { 540 reset(); 541 m_type = BOOL; 542 u.m_bool = a_value; 543 } 544 545 void set(const std::string& a_value) { 546 reset(); 547 m_type = STRING; 548 u.m_string = new std::string(a_value); 549 } 550 void set(const char* a_value) { 551 // To avoid the bool, const std::string& and passing "text" problem. 552 reset(); 553 m_type = STRING; 554 u.m_string = new std::string(a_value); 555 } 556 557 void set(void* a_value) { 558 reset(); 559 m_type = VOID_STAR; 560 u.m_void_star = a_value; 561 } 562 563 void set(double* a_value) { 564 reset(); 565 m_type = DOUBLE_STAR; 566 u.m_double_star = a_value; 567 } 568 void set(float* a_value) { 569 reset(); 570 m_type = FLOAT_STAR; 571 u.m_float_star = a_value; 572 } 573 void set(int* a_value) { 574 reset(); 575 m_type = INT_STAR; 576 u.m_int_star = a_value; 577 } 578 579 //unsigned char get_unsigned_char() const {return u.m_unsigned_char;} 580 //char get_char() const {return u.m_char;} 581 unsigned int get_unsigned_int() const {return u.m_unsigned_int;} 582 int get_int() const {return u.m_int;} 583 584 int64 get_int64() const {return u.m_int64;} 585 uint64 get_unsigned_int64() const {return u.m_unsigned_int64;} 586 587 unsigned short get_unsigned_short() const{return u.m_unsigned_short;} 588 short get_short() const {return u.m_short;} 589 float get_float() const {return u.m_float;} 590 double get_double() const {return u.m_double;} 591 void* get_void_star() const {return u.m_void_star;} 592 double* get_double_star() const {return u.m_double_star;} 593 float* get_float_star() const {return u.m_float_star;} 594 int* get_int_star() const {return u.m_int_star;} 595 bool get_bool() const {return u.m_bool;} 596 597 const std::string& get_string() const {return *u.m_string;} 598 599 #define TOOLS_VALUE_SET(a_what,a_m_what,a_type) \ 600 void set(const std::vector<unsigned int>& a_orders,const std::vector<a_what>& a_v){\ 601 reset();\ 602 m_type = a_type;\ 603 u.a_m_what = new array<a_what>(a_orders);\ 604 u.a_m_what->fill(a_v);\ 605 } 606 607 //TOOLS_VALUE_SET(unsigned char,m_array_unsigned_char,ARRAY_UNSIGNED_CHAR) 608 //TOOLS_VALUE_SET(char,m_array_char,ARRAY_CHAR) 609 TOOLS_VALUE_SET(unsigned short,m_array_unsigned_short,ARRAY_UNSIGNED_SHORT) 610 TOOLS_VALUE_SET(short,m_array_short,ARRAY_SHORT) 611 TOOLS_VALUE_SET(unsigned int,m_array_unsigned_int,ARRAY_UNSIGNED_INT) 612 TOOLS_VALUE_SET(int,m_array_int,ARRAY_INT) 613 TOOLS_VALUE_SET(uint64,m_array_unsigned_int64,ARRAY_UNSIGNED_INT64) 614 TOOLS_VALUE_SET(int64,m_array_int64,ARRAY_INT64) 615 TOOLS_VALUE_SET(float,m_array_float,ARRAY_FLOAT) 616 TOOLS_VALUE_SET(double,m_array_double,ARRAY_DOUBLE) 617 TOOLS_VALUE_SET(bool,m_array_bool,ARRAY_BOOL) 618 TOOLS_VALUE_SET(std::string,m_array_string,ARRAY_STRING) 619 620 #undef TOOLS_VALUE_SET 621 622 #define TOOLS_VALUE_SET2(a_what,a_m_what,a_type) \ 623 void set(const std::vector<a_what>& a_v){\ 624 reset();\ 625 m_type = a_type;\ 626 std::vector<unsigned int> is(1);\ 627 is[0] = (unsigned int)a_v.size();\ 628 u.a_m_what = new array<a_what>(is);\ 629 u.a_m_what->fill(a_v);\ 630 } 631 632 //TOOLS_VALUE_SET2(unsigned char,m_array_unsigned_char,ARRAY_UNSIGNED_CHAR) 633 //TOOLS_VALUE_SET2(char,m_array_char,ARRAY_CHAR) 634 TOOLS_VALUE_SET2(unsigned short,m_array_unsigned_short,ARRAY_UNSIGNED_SHORT) 635 TOOLS_VALUE_SET2(short,m_array_short,ARRAY_SHORT) 636 TOOLS_VALUE_SET2(unsigned int,m_array_unsigned_int,ARRAY_UNSIGNED_INT) 637 TOOLS_VALUE_SET2(int,m_array_int,ARRAY_INT) 638 TOOLS_VALUE_SET2(uint64,m_array_unsigned_int64,ARRAY_UNSIGNED_INT64) 639 TOOLS_VALUE_SET2(int64,m_array_int64,ARRAY_INT64) 640 TOOLS_VALUE_SET2(float,m_array_float,ARRAY_FLOAT) 641 TOOLS_VALUE_SET2(double,m_array_double,ARRAY_DOUBLE) 642 TOOLS_VALUE_SET2(bool,m_array_bool,ARRAY_BOOL) 643 TOOLS_VALUE_SET2(std::string,m_array_string,ARRAY_STRING) 644 645 #undef TOOLS_VALUE_SET2 646 647 #define TOOLS_VALUE_GET(a_what,a_get_what,a_m_what) \ 648 const std::vector<a_what>& a_get_what(std::vector<unsigned int>& a_orders) const {\ 649 a_orders = u.a_m_what->orders();\ 650 return u.a_m_what->vector();\ 651 } 652 653 //TOOLS_VALUE_GET(unsigned char,get_array_unsigned_char,m_array_unsigned_char) 654 //TOOLS_VALUE_GET(char,get_array_char,m_array_char) 655 TOOLS_VALUE_GET(unsigned short,get_array_unsigned_short,m_array_unsigned_short) 656 TOOLS_VALUE_GET(short,get_array_short,m_array_short) 657 TOOLS_VALUE_GET(unsigned int,get_array_unsigned_int,m_array_unsigned_int) 658 TOOLS_VALUE_GET(int,get_array_int,m_array_int) 659 TOOLS_VALUE_GET(uint64,get_array_unsigned_int64,m_array_unsigned_int64) 660 TOOLS_VALUE_GET(int64,get_array_int64,m_array_int64) 661 TOOLS_VALUE_GET(float,get_array_float,m_array_float) 662 TOOLS_VALUE_GET(double,get_array_double,m_array_double) 663 TOOLS_VALUE_GET(bool,get_array_bool,m_array_bool) 664 TOOLS_VALUE_GET(std::string,get_array_string,m_array_string) 665 666 #undef TOOLS_VALUE_GET 667 668 669 public: 670 bool s_type(std::string& a_s) const {return s_type(m_type,a_s);} 671 672 static bool s_type(value::e_type a_type,std::string& a_s){ 673 switch(a_type) { 674 case NONE:a_s = "NONE";return true; 675 case INT:a_s = "INT";return true; 676 case INT64:a_s = "INT64";return true; 677 case DOUBLE:a_s = "DOUBLE";return true; 678 case STRING:a_s = "STRING";return true; 679 case VOID_STAR:a_s = "VOID_STAR";return true; 680 case DOUBLE_STAR:a_s = "DOUBLE_STAR";return true; 681 case FLOAT_STAR:a_s = "FLOAT_STAR";return true; 682 case INT_STAR:a_s = "INT_STAR";return true; 683 case BOOL:a_s = "BOOL";return true; 684 case SHORT:a_s = "SHORT";return true; 685 case FLOAT:a_s = "FLOAT";return true; 686 //case CHAR:a_s = "CHAR";return true; 687 //case UNSIGNED_CHAR:a_s = "UNSIGNED_CHAR";return true; 688 case UNSIGNED_SHORT:a_s = "UNSIGNED_SHORT";return true; 689 case UNSIGNED_INT:a_s = "UNSIGNED_INT";return true; 690 case UNSIGNED_INT64:a_s = "UNSIGNED_INT64";return true; 691 //case ARRAY_UNSIGNED_CHAR:a_s = "ARRAY_UNSIGNED_CHAR";return true; 692 //case ARRAY_CHAR:a_s = "ARRAY_CHAR";return true; 693 case ARRAY_UNSIGNED_SHORT:a_s = "ARRAY_UNSIGNED_SHORT";return true; 694 case ARRAY_SHORT:a_s = "ARRAY_SHORT";return true; 695 case ARRAY_UNSIGNED_INT:a_s = "ARRAY_UNSIGNED_INT";return true; 696 case ARRAY_INT:a_s = "ARRAY_INT";return true; 697 case ARRAY_UNSIGNED_INT64:a_s = "ARRAY_UNSIGNED_INT64";return true; 698 case ARRAY_INT64:a_s = "ARRAY_INT64";return true; 699 case ARRAY_FLOAT:a_s = "ARRAY_FLOAT";return true; 700 case ARRAY_DOUBLE:a_s = "ARRAY_DOUBLE";return true; 701 case ARRAY_BOOL:a_s = "ARRAY_BOOL";return true; 702 case ARRAY_STRING:a_s = "ARRAY_STRING";return true; 703 default:a_s.clear();return false; 704 } 705 706 } 707 bool tos(std::string& a_s) const {return tos(*this,a_s);} 708 709 static bool tos(const value& a_v,std::string& a_s){ 710 switch(a_v.m_type) { 711 case NONE: 712 sprintf(a_s,5,"(nil)"); 713 return true; 714 case BOOL: 715 sprintf(a_s,5,"%s",a_v.u.m_bool?"true":"false"); 716 return true; 717 //case UNSIGNED_CHAR: 718 // sprintf(a_s,32,"%c",a_v.u.m_unsigned_char); 719 // return true; 720 //case CHAR: 721 // sprintf(a_s,32,"%c",a_v.u.m_char); 722 // return true; 723 case UNSIGNED_SHORT: 724 sprintf(a_s,32,"%u",a_v.u.m_unsigned_short); 725 return true; 726 case SHORT: 727 sprintf(a_s,32,"%d",a_v.u.m_short); 728 return true; 729 case UNSIGNED_INT: 730 sprintf(a_s,32,"%u",a_v.u.m_unsigned_int); 731 return true; 732 case INT: 733 sprintf(a_s,32,"%d",a_v.u.m_int); 734 return true; 735 case UNSIGNED_INT64: 736 sprintf(a_s,32,int64_format(),a_v.u.m_unsigned_int64); 737 return true; 738 case INT64: 739 sprintf(a_s,32,int64_format(),a_v.u.m_int64); 740 return true; 741 case FLOAT: 742 sprintf(a_s,32,"%g",a_v.u.m_float); 743 return true; 744 case DOUBLE: 745 sprintf(a_s,32,"%g",a_v.u.m_double); 746 return true; 747 case VOID_STAR: 748 sprintf(a_s,32,upointer_format_x(),(upointer)a_v.u.m_void_star); 749 return true; 750 case DOUBLE_STAR: 751 sprintf(a_s,32,upointer_format_x(),(upointer)a_v.u.m_double_star); 752 return true; 753 case FLOAT_STAR: 754 sprintf(a_s,32,upointer_format_x(),(upointer)a_v.u.m_float_star); 755 return true; 756 case INT_STAR: 757 sprintf(a_s,32,upointer_format_x(),(upointer)a_v.u.m_int_star); 758 return true; 759 case STRING: 760 a_s = *a_v.u.m_string; 761 return true; 762 763 //case ARRAY_UNSIGNED_CHAR: 764 // a_s = tos<unsigned char>(a_v.u.m_array_unsigned_char->vector()); 765 // return true; 766 //case ARRAY_CHAR: 767 // a_s = tos<char>(a_v.u.m_array_char->vector()); 768 // return true; 769 770 case ARRAY_UNSIGNED_SHORT: 771 return nums2s<unsigned short>(a_v.u.m_array_unsigned_short->vector(),a_s); 772 case ARRAY_SHORT: 773 return nums2s<short>(a_v.u.m_array_short->vector(),a_s); 774 775 case ARRAY_UNSIGNED_INT: 776 return nums2s<unsigned int>(a_v.u.m_array_unsigned_int->vector(),a_s); 777 case ARRAY_INT: 778 return nums2s<int>(a_v.u.m_array_int->vector(),a_s); 779 780 case ARRAY_UNSIGNED_INT64: 781 return nums2s<uint64>(a_v.u.m_array_unsigned_int64->vector(),a_s); 782 case ARRAY_INT64: 783 return nums2s<int64>(a_v.u.m_array_int64->vector(),a_s); 784 785 case ARRAY_FLOAT: 786 return nums2s<float>(a_v.u.m_array_float->vector(),a_s); 787 case ARRAY_DOUBLE: 788 return nums2s<double>(a_v.u.m_array_double->vector(),a_s); 789 case ARRAY_BOOL: 790 b2s(a_v.u.m_array_bool->vector(),a_s); 791 return true; 792 case ARRAY_STRING: 793 return nums2s<std::string>(a_v.u.m_array_string->vector(),a_s); 794 default: 795 a_s.clear(); 796 return false; 797 } 798 } 799 800 /* 801 size_t header_size() const {return header_size(*this);} //used in base_evaluator::dump() 802 static size_t header_size(const value& a_v){ 803 switch(a_v.m_type) { 804 case NONE: return 5; 805 806 case BOOL: return 5; 807 //case UNSIGNED_CHAR: return 1; 808 //case CHAR: return 1; 809 810 case INT: 811 case UNSIGNED_SHORT: 812 case SHORT: 813 case FLOAT: 814 case UNSIGNED_INT: return 8; 815 816 817 case DOUBLE: 818 case INT64: 819 case UNSIGNED_INT64: 820 case VOID_STAR: 821 case DOUBLE_STAR: 822 case FLOAT_STAR: 823 case INT_STAR: 824 case STRING: return 12; 825 826 //case ARRAY_UNSIGNED_CHAR: 827 //case ARRAY_CHAR: 828 829 case ARRAY_UNSIGNED_SHORT: 830 case ARRAY_SHORT: 831 case ARRAY_UNSIGNED_INT: 832 case ARRAY_INT: 833 case ARRAY_UNSIGNED_INT64: 834 case ARRAY_INT64: 835 case ARRAY_FLOAT: 836 case ARRAY_DOUBLE: 837 case ARRAY_BOOL: 838 case ARRAY_STRING: return 5; //size("array") 839 default: return 8; 840 } 841 } 842 */ 843 844 bool to_double(double& a_d) const { 845 switch(m_type) { 846 case INT: 847 a_d = u.m_int; 848 return true; 849 case DOUBLE: 850 a_d = u.m_double; 851 return true; 852 case UNSIGNED_SHORT: 853 a_d = u.m_unsigned_short; 854 return true; 855 case UNSIGNED_INT: 856 a_d = u.m_unsigned_int; 857 return true; 858 case SHORT: 859 a_d = u.m_short; 860 return true; 861 case INT64: 862 a_d = (double)u.m_int64; 863 return true; 864 case UNSIGNED_INT64: 865 a_d = (double)u.m_unsigned_int64; 866 return true; 867 case FLOAT: 868 a_d = u.m_float; 869 return true; 870 //case UNSIGNED_CHAR: 871 // a_d = u.m_unsigned_char; 872 // return true; 873 //case CHAR: 874 // a_d = u.m_char; 875 // return true; 876 case BOOL: 877 a_d = u.m_bool?1:0; 878 return true; 879 case NONE: 880 case STRING: 881 case VOID_STAR: 882 case DOUBLE_STAR: 883 case FLOAT_STAR: 884 case INT_STAR: 885 //case ARRAY_UNSIGNED_CHAR: 886 //case ARRAY_CHAR: 887 case ARRAY_UNSIGNED_SHORT: 888 case ARRAY_SHORT: 889 case ARRAY_UNSIGNED_INT: 890 case ARRAY_INT: 891 case ARRAY_UNSIGNED_INT64: 892 case ARRAY_INT64: 893 case ARRAY_FLOAT: 894 case ARRAY_DOUBLE: 895 case ARRAY_BOOL: 896 case ARRAY_STRING: 897 break; 898 } 899 a_d = 0; 900 return false; 901 } 902 903 bool is_array() const { 904 switch(m_type) { 905 case INT: 906 case DOUBLE: 907 case UNSIGNED_SHORT: 908 case UNSIGNED_INT: 909 case SHORT: 910 case INT64: 911 case UNSIGNED_INT64: 912 case FLOAT: 913 //case UNSIGNED_CHAR: 914 //case CHAR: 915 case BOOL: 916 case NONE: 917 case STRING: 918 case VOID_STAR: 919 case DOUBLE_STAR: 920 case FLOAT_STAR: 921 case INT_STAR: 922 return false; 923 924 //case ARRAY_UNSIGNED_CHAR: 925 //case ARRAY_CHAR: 926 case ARRAY_UNSIGNED_SHORT: 927 case ARRAY_SHORT: 928 case ARRAY_UNSIGNED_INT: 929 case ARRAY_INT: 930 case ARRAY_UNSIGNED_INT64: 931 case ARRAY_INT64: 932 case ARRAY_FLOAT: 933 case ARRAY_DOUBLE: 934 case ARRAY_BOOL: 935 case ARRAY_STRING: 936 return true; 937 } 938 return false; 939 } 940 941 size_t array_size() const { 942 switch(m_type) { 943 case INT: 944 case DOUBLE: 945 case UNSIGNED_SHORT: 946 case UNSIGNED_INT: 947 case SHORT: 948 case INT64: 949 case UNSIGNED_INT64: 950 case FLOAT: 951 //case UNSIGNED_CHAR: 952 //case CHAR: 953 case BOOL: 954 case NONE: 955 case STRING: 956 case VOID_STAR: 957 case DOUBLE_STAR: 958 case FLOAT_STAR: 959 case INT_STAR: 960 return 0; 961 962 //case ARRAY_UNSIGNED_CHAR: 963 //case ARRAY_CHAR: 964 case ARRAY_UNSIGNED_SHORT: 965 return u.m_array_unsigned_short->size(); 966 case ARRAY_SHORT: 967 return u.m_array_short->size(); 968 case ARRAY_UNSIGNED_INT: 969 return u.m_array_unsigned_int->size(); 970 case ARRAY_INT: 971 return u.m_array_int->size(); 972 case ARRAY_UNSIGNED_INT64: 973 return u.m_array_unsigned_int64->size(); 974 case ARRAY_INT64: 975 return u.m_array_int64->size(); 976 case ARRAY_FLOAT: 977 return u.m_array_float->size(); 978 case ARRAY_DOUBLE: 979 return u.m_array_double->size(); 980 case ARRAY_BOOL: 981 return u.m_array_bool->size(); 982 case ARRAY_STRING: 983 return u.m_array_string->size(); 984 } 985 return 0; 986 } 987 988 #define TOOLS_ARRAY_VEC(a_what,a_get_what,a_m_what) \ 989 const std::vector<a_what>& a_get_what() const {return u.a_m_what->vector();}\ 990 std::vector<a_what>& a_get_what() {return u.a_m_what->vector();} 991 992 //TOOLS_ARRAY_VEC(unsigned char,array_unsigned_char,m_array_unsigned_char) 993 //TOOLS_ARRAY_VEC(char,array_char,m_array_char) 994 TOOLS_ARRAY_VEC(unsigned short,array_unsigned_short,m_array_unsigned_short) 995 TOOLS_ARRAY_VEC(short,array_short,m_array_short) 996 TOOLS_ARRAY_VEC(unsigned int,array_unsigned_int,m_array_unsigned_int) 997 TOOLS_ARRAY_VEC(int,array_int,m_array_int) 998 TOOLS_ARRAY_VEC(uint64,array_unsigned_int64,m_array_unsigned_int64) 999 TOOLS_ARRAY_VEC(int64,array_int64,m_array_int64) 1000 TOOLS_ARRAY_VEC(float,array_float,m_array_float) 1001 TOOLS_ARRAY_VEC(double,array_double,m_array_double) 1002 TOOLS_ARRAY_VEC(bool,array_bool,m_array_bool) 1003 TOOLS_ARRAY_VEC(std::string,array_string,m_array_string) 1004 1005 #undef TOOLS_ARRAY_VEC 1006 1007 public: // for valop : 1008 1009 std::string stype() const { 1010 std::string _s; 1011 if(!s_type(_s)) return "unknown"; 1012 return _s; 1013 } 1014 1015 static std::string stype(e_type a_type) { 1016 std::string _s; 1017 if(!s_type(a_type,_s)) return "unknown"; 1018 return _s; 1019 } 1020 1021 static std::string error_div_zero() {return "divide by zero.";} 1022 1023 static bool assign(value&,const value&,std::string&); 1024 1025 // operations in Expression.cxx : 1026 static bool minus(value&,std::string&); 1027 static bool do_not(value&,std::string&); 1028 1029 static bool add(value&,const value&,std::string&); 1030 static bool subtract(value&,const value&,std::string&); 1031 static bool multiply(value&,const value&,std::string&); 1032 static bool divide(value&,const value&,std::string&); 1033 1034 static bool if_gt(value&,const value&,std::string&); 1035 static bool if_eq(value&,const value&,std::string&); 1036 1037 static bool if_ne(value& aThis,const value& aV, 1038 std::string& aError) { 1039 if(!if_eq(aThis,aV,aError)) return false; 1040 aThis.u.m_bool = (aThis.u.m_bool?false:true); 1041 return true; 1042 } 1043 static bool if_ge(value& aThis,const value& aV, 1044 std::string& aError){ 1045 value tmp(aThis); 1046 if(!if_eq(aThis,aV,aError)) return false; 1047 if(aThis.u.m_bool) return true; 1048 // then not equal, check gt : 1049 if(!if_gt(tmp,aV,aError)) return false; 1050 aThis.u.m_bool = tmp.u.m_bool; 1051 return true; 1052 } 1053 1054 static bool if_lt(value& aThis,const value& aV, 1055 std::string& aError){ 1056 if(!if_ge(aThis,aV,aError)) return false; 1057 aThis.u.m_bool = (aThis.u.m_bool?false:true); 1058 return true; 1059 } 1060 1061 static bool if_le(value& aThis,const value& aV, 1062 std::string& aError){ 1063 if(!if_gt(aThis,aV,aError)) return false; 1064 aThis.u.m_bool = (aThis.u.m_bool?false:true); 1065 return true; 1066 } 1067 1068 static bool if_and(value&,const value&,std::string&); 1069 static bool if_or(value&,const value&,std::string&); 1070 1071 static bool to_double(const value&,double&); 1072 static bool cxx_type(const value&,std::string&); 1073 1074 //static bool i_set(value&,const Slash::Core::Ivalue&); 1075 static std::string to_string(const value&); 1076 1077 protected: 1078 void reset() { 1079 if(m_type==STRING) { 1080 delete u.m_string; 1081 u.m_string = 0; 1082 1083 //} else if(m_type==ARRAY_UNSIGNED_CHAR) { 1084 // delete u.m_array_unsigned_char;u.m_array_unsigned_char = 0; 1085 1086 //} else if(m_type==ARRAY_CHAR) { 1087 // delete u.m_array_char;u.m_array_char = 0; 1088 1089 } else if(m_type==ARRAY_UNSIGNED_SHORT) { 1090 delete u.m_array_unsigned_short;u.m_array_unsigned_short = 0; 1091 1092 } else if(m_type==ARRAY_SHORT) { 1093 delete u.m_array_short;u.m_array_short = 0; 1094 1095 } else if(m_type==ARRAY_UNSIGNED_INT) { 1096 delete u.m_array_unsigned_int;u.m_array_unsigned_int = 0; 1097 1098 } else if(m_type==ARRAY_INT) { 1099 delete u.m_array_int;u.m_array_int = 0; 1100 1101 } else if(m_type==ARRAY_UNSIGNED_INT64) { 1102 delete u.m_array_unsigned_int64;u.m_array_unsigned_int64 = 0; 1103 1104 } else if(m_type==ARRAY_INT64) { 1105 delete u.m_array_int64;u.m_array_int64 = 0; 1106 1107 } else if(m_type==ARRAY_FLOAT) { 1108 delete u.m_array_float;u.m_array_float = 0; 1109 1110 } else if(m_type==ARRAY_DOUBLE) { 1111 delete u.m_array_double;u.m_array_double = 0; 1112 1113 } else if(m_type==ARRAY_BOOL) { 1114 delete u.m_array_bool;u.m_array_bool = 0; 1115 1116 } else if(m_type==ARRAY_STRING) { 1117 delete u.m_array_string;u.m_array_string = 0; 1118 1119 } else { 1120 u.m_unsigned_int64 = 0; 1121 } 1122 } 1123 protected: 1124 std::string* m_label; 1125 unsigned int m_itag; 1126 protected: 1127 e_type m_type; 1128 union { 1129 bool m_bool; 1130 //char m_char; 1131 int m_int; 1132 short m_short; 1133 int64 m_int64; 1134 float m_float; 1135 double m_double; 1136 //unsigned char m_unsigned_char; 1137 unsigned short m_unsigned_short; 1138 unsigned int m_unsigned_int; 1139 uint64 m_unsigned_int64; 1140 void* m_void_star; 1141 double* m_double_star; 1142 float* m_float_star; 1143 int* m_int_star; 1144 std::string* m_string; 1145 1146 array<unsigned char>* m_array_unsigned_char; 1147 array<char>* m_array_char; 1148 array<unsigned short>* m_array_unsigned_short; 1149 array<short>* m_array_short; 1150 array<uint64>* m_array_unsigned_int64; 1151 array<int64>* m_array_int64; 1152 array<unsigned int>* m_array_unsigned_int; 1153 array<int>* m_array_int; 1154 array<float>* m_array_float; 1155 array<double>* m_array_double; 1156 array<bool>* m_array_bool; 1157 array<std::string>* m_array_string; 1158 } u; 1159 }; 1160 1161 } 1162 1163 #include "value.icc" 1164 1165 #endif